开发者

__init__.py seemingly not working

开发者 https://www.devze.com 2023-01-27 15:01 出处:网络
New to Python.I\'m on Windows and Python2.7. I have confirmed that my directory is setup in PYTHONPATH by looking in sys.path from IDLE.

New to Python. I'm on Windows and Python2.7.

I have confirmed that my directory is setup in PYTHONPATH by looking in sys.path from IDLE.

import sys
print(sys.path)

This shows me that c:\users\owner\documents\PythonProjects is definitely in the path. Furthermore in that directory I have a .py file which can be impo开发者_运维技巧rted into IDLE.

The trouble is c:\user\owner\documents\PythonProjects\testPackage\test.py is not loading. The error is ImportError:No module named testPackage.test.

I have tried an empty __init__.py as well as an __init__.py with some trivial code inside. The __init__.py is in the PythonProjects folder as it should be. Why am I getting this error?


Because it should be in testPackage\.


There must be an __init__.py file in every directory, so if your structure is:

projects/
   TestProject/
       __init__.py
       test.py

And the path to projects is on your PYTHONPATH, then you should be able to import the test module.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号