1
votes

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?
1
Rename io to something else. May be it's clashing with the inbuilt io package. - Mohammad Yusuf
@MohammadYusufGhazi Your suggestion works. So, as I stated, I'm coming from a Java world, and my question is, just because there's already a package using io, no one else can create a package starting with that namespace? e.g. io.mydomain.project? In Java, you can share an io namespace, though you'd want to get more specific than that, but it's ok to use io. - Jane Wayne
Try to import like this. from .io.me.model.car import Car. See if that works. It will import the io package from the current directory. - Mohammad Yusuf

1 Answers

1
votes

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.