I have a python application with the following directory structure:
src
|
+---- main
|
+---- util
|
+---- gen_py
|
+---- lib
In the package main, I have a python module named MyServer.py which has an import statement like:
from gen_py.lib import MyService
In order for this statement to work, I placed the following line at the beginning of MyServer.py:
import sys
sys.path.append('../gen_py/lib')
When I run MyServer.py in the terminal, I get the following error:
ImportError: No module named gen_py.lib
What I am missing here?
Best Answer
Your modification of
sys.path
assumes the current working directory is always inmain/
. This is not the case. Instead, just add the parent directory tosys.path
:Don't forget to include a file
__init__.py
in gen_py and lib - otherwise, they won't be recognized as Python modules.