问题
I have a module with the following folder structure
Module
-__init__.py
-analyzer.py
-lib/
-lib/models
-lib/data/
However when used from the parent directory I get an IOError for a file used in analyzer.py which is in lib/models. How can I get this fixed without copying the models and data to the parent directory
回答1:
Every Python module must have own __init__.py
file:
Module
-__init__.py
-analyzer.py
-lib/
-lib/__init__.py
-lib/models/
-lib/models/__init__.py
-lib/data/
-lib/data/__init__.py
The __init__.py
files are required to make Python treat the directories as containing packages. In the simplest case, __init__.py
can just be an empty file.
See: https://docs.python.org/2/tutorial/modules.html#packages
来源:https://stackoverflow.com/questions/33801189/ioerror-in-imported-python-module