Friday 26 August 2016

python - running module within a package getting no module error




I want to organize my python project like so:



/proj

/test
__init__.py
test.py
/utils
__init__.py
util.py


Inside test.py I have want to import a class from utils.util




from utils.util import classA



However, I get an error when I run test.py when I am in the /proj directory:



$ python test/test.py 
Traceback (most recent call last):
File "test/test.py", line 1, in
from utils.util import classA
ImportError: No module named utils.util



However, if I make these changes, the code runs fine (python test.py)



/proj
test.py
/utils
__init__.py
util.py



I would really like to be able to organize my code like I did at the first example without getting errors. How can I enable myself to do this?


Answer



You can run it as a package.



python -m test.test

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...