Search code examples
pythonpython-2.7python-3.xpython-module

Import error in Python 3 but works with Python 2


I want to work with Python v3.5.2, but my laptop also has Python 2.7.10 installed (it's a MacBook). I have a simple Python project structure like the following. Note, there might be artifacts that are showing because I am using IntelliJ as the IDE (e.g. *.pyc files and *.iml file).

.
├── io
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   └── __init__.cpython-35.pyc
│   └── me
│       ├── __init__.py
│       ├── __init__.pyc
│       └── model
│           ├── __init__.py
│           ├── __init__.pyc
│           ├── car.py
│           └── car.pyc
├── start.py
└── test-python.iml

My start.py script looks like the following.

from io.me.model.car import Car

car = Car("honda", "civic", 2005)
print(car.model)

In a terminal, if I type in python3 start.py then I get the following error.

Traceback (most recent call last):
  File "start.py", line 1, in 
    from io.me.model.car import Car
ImportError: No module named 'io.me'; 'io' is not a package

However, I decided to type in python start.py and I actually do get an output: civic.

Any ideas on what I'm doing wrong here?

Also, is there a guideline on a project structure for Python? Coming from a Java world, I'd like to know if there is a recommended best-practice or a highly-opinionated approach to a Python project's structure (e.g. like a typical Java Maven project).

  • Where do I put my sources?
  • Where do I put in my tests?
  • Is there a build tool (like Maven for Java) for Python that would facilitate and guide the directory structure?

Solution

  • There is an inbuild module in python called io. Also add __init__.py in the directory where folder io exists.

    Output in Python2

    >>>
    >>> import io
    >>>
    >>>
    >>> dir(io)
    ['BlockingIOError', 'BufferedIOBase', 'BufferedRWPair', 'BufferedRandom', 'BufferedReader', 'BufferedWriter', 'BytesIO', 'DEFAULT_BUFFER_SIZE', 'FileIO', 'IOBase', 'IncrementalNewlineDecoder', 'OpenWrapper', 'RawIOBase', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'StringIO', 'TextIOBase', 'TextIOWrapper', 'UnsupportedOperation', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_io', 'abc', 'open']
    >>>
    

    Output in Python3

    Python 3.4.5 (default, Oct 10 2016, 14:41:48)
    [GCC 5.4.0] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import io
    >>>
    >>> dir(io)
    ['BlockingIOError', 'BufferedIOBase', 'BufferedRWPair', 'BufferedRandom', 'BufferedReader', 'BufferedWriter', 'BytesIO', 'DEFAULT_BUFFER_SIZE', 'FileIO', 'IOBase', 'IncrementalNewlineDecoder', 'OpenWrapper', 'RawIOBase', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'StringIO', 'TextIOBase', 'TextIOWrapper', 'UnsupportedOperation', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_io', 'abc', 'open']
    >>>
    

    Rename your io package to something else.