In the past, in order for me to use/import my own custom packages in python all i've had to do is edit my .bash_profile so that they are appended to my $PATH environmental variable.
>> echo $PATH
/Users/MYNAME/google-cloud-sdk/bin:/Users/MYNAME/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/Users/MYNAME/Documents/Python_Programs/MYPACKAGE1:/Users/MYNAME/Documents/GitRepos/MYPACKAGE2
as you can see my path variable contains MYPACKAGE1 and MYPACKAGE2. The former is literally just a folder with a single .py file in it. The latter follows the conventional package folder structure.
One thing to note is my PYTHONPATH is apparently empty
>> echo $PYTHONPATH
>>
but if, in python, I look at sys.path
:
>> import sys
>> sys.path
['',
'/Users/MYNAME/anaconda/bin',
'/Users/MYNAME/anaconda/lib/python36.zip',
'/Users/MYNAME/anaconda/lib/python3.6',
'/Users/MYNAME/anaconda/lib/python3.6/lib-dynload',
'/Users/MYNAME/anaconda/lib/python3.6/site-packages',
'/Users/MYNAME/Documents/Python_Programs/MYPACKAGE1',
'/Users/MYNAME/Documents/GitRepos/MYPACKAGE2',
'/Users/MYNAME/anaconda/lib/python3.6/site-packages/Sphinx-1.5.6-py3.6.egg',
'/Users/MYNAME/anaconda/lib/python3.6/site-packages/aeosa',
'/Users/MYNAME/anaconda/lib/python3.6/site-packages/IPython/extensions',
'/Users/MYNAME/.ipython']
Great, no problem importing from either package.
So I made a third package which has the same structure as MYPACKAGE2
MYPACKAGE3/
bin/
README.md
CHANGES.txt
setup.py
MANIFEST.IN
my_module/
__init__.py
sub_module1.py
sub_module2.py
added it to my path
>> echo $PATH
/Users/MYNAME/google-cloud-sdk/bin:/Users/MYNAME/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/Users/MYNAME/Documents/Python_Programs/MYPACKAGE1:/Users/MYNAME/Documents/GitRepos/MYPACKAGE2:/Users/MYNAME/Documents/GitRepos/MYPACKAGE3
but for some reason i can't import it in python and it's not in my sys.path (sys.path is exactly the same as before).
>> import my_module
ModuleNotFoundError: No module named 'my_module'
I have no idea what's changed. What am I missing?
Thanks in advance.
The answer is that my $PATH
variable has nothing to do with it. To clarify, I'm using python 3.6, so it may be different in 2.7.
It turns out there was a .pth
file in my anaconda/.../site-packages directory that I must have created a long time ago. It looks like the sys.path
gets populated not only by the .../site-packages directory, but also by all directories listed in any .pth
files within that directory (or it looks like any directory in sys.path re the hitchikers guide to packaging).
Python file layout
A Python installation has a site-packages directory inside the module directory. This directory is where user installed packages are dropped. A .pth file in this directory is maintained which contains paths to the directories where the extra packages are installed.
I know this question is somewhat esoteric - but hope it helps other stragglers trying to understand the import system.