1

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?
4
  • Rename io to something else. May be it's clashing with the inbuilt io package. Commented Dec 10, 2016 at 4:08
  • @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. Commented Dec 10, 2016 at 4:36
  • 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. Commented Dec 10, 2016 at 4:41
  • You could read this opinionated guide Commented Dec 10, 2016 at 6:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

There is already a __init__.py file in the sub-dir io (see output from tree command above). However, this __init__.py file is empty. Is that problematic?
__init__.py is just a way to make it as a package. There is no problem if is empty.
So, it seems that I cannot use that io namespace, right? Because when I simply renamed that directory io to something else, e.g. ioioio, then running the Python 3 command against start.py works.
It is not recommended to use the package which already exist in the python inbuilts.
Ok, I understand now. Could you also please answer my other questions? I'm just starting out in Python and I want to do it right with my project structure and build management.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.