I know that this branch is a little outdated, but it took me a while to understand its essence, so I wanted to share it.
In my project, I had the main script in the parent directory, and to differentiate the modules, I put all the supporting modules in a subfolder called "modules". In my main script, I import these modules as follows (for a module called report.py):
from modules.report import report, reportError
If I name my main script, this will work. HOWEVER, I wanted to test each module by including main()
in them, and each of them called each, like:
python modules/report.py
Now Python complains that it cannot find a "module called modules". The key point here is that, by default, Python includes a script folder in its search path, but not CWD. So this error says, in fact: "I can not find the subfolder of the modules." This is because the subdirectory "modules" does not exist from the directory in which the report.py module is located.
I find the easiest solution for this is to add CWD to the Python search path by including it at the top:
import sys sys.path.append(".")
Now Python searches for CWD (current directory), finds the subfolder "modules", and all is well.
Tom Gordon Oct 11 '17 at 15:12 2017-10-11 15:12
source share