PYTHONPATH, virtualenv, sys.path, modules, packages and project structure
Trying to get to the bottom of the best way to structure some code while working in virtualenv.
I needed to be clear on the use of modules:
Concretely, the import modu statement will look for the proper file, which is modu.py in the same directory as the caller if it exists. If it is not found, the Python interpreter will search for modu.py in the โpathโ recursively and raise an ImportError exception if it is not found. Once modu.py is found, the Python interpreter will execute the module in an isolated scope. Any top-level statement in modu.py will be executed, including other imports if any.
Then I needed to be clear that a bundle of modules may be referred to as a package:
Python provides a very straightforward packaging system, which is simply an extension of the module mechanism to a directory.
. However, some imports were failing although I had everything in packages as they should be. (Note "package" has two meanings in python, one being a directory of modules, the directory is named as the package, containing at least a blank __init__.py and submodules, but more usefullly may also contain subdirectories, each with their own __init__.py and submodules. (EDIT: found this good Mike Grouchy post about the use of __init__.py.) The other meaninghandy rule of thumb: import package-sense1, but pip install package-sense2
stackoverflow on editing PYTHONPATH in virtualenvs.
A post by mrcoles gives a pretty good idea of what you need to do re activate
Similar issue experienced by a stackoverflow user due to corruption of the name of
__init__.py
during SCP transfer
I ended up testing this via a tutorialspoint example.
A detailed look at how to structure a python project touches on: modules, packages and OO.



















