Sreejith Menon Sreejith Menon - 5 months ago 48
Python Question

Setting a default sys.path for IPython Notebook

I have all my .py files inside a folder script and all my IPython-notebooks under a folder named Notebook.

There are multiple cross dependencies for each notebook file on one or more files on script.

Having

sys.path.append
on top of every notebook seems cumbersome and I am hoping there is a way to add a default lookup path just like we add
PYTHONPATH
to
.bash_profile
.

Now I do the following:

import sys
sys.path.append("<path where DeriveFinalResultSet.py exists>)
import DeriveFinalResultSet as drs


I wish to have a setting where I can do the below:

import DeriveFinalResultSet as drs

Answer

After some research I realized changing PYTHONPATH in .bash_profile should do the trick.

Here are the two lines that I added to my .bash_profile

PYTHONPATH="<path where DeriveFinalResultSet.py exists>:$PYTHONPATH"
export PYTHONPATH

To verify, I did the following after opening a fresh IPython Notebook.

import sys
print(sys.path)
['', '**<path where DeriveFinalResultSet.py exists>**', '<some path>Google Drive/Project/AnimalPhotoBias/Notebooks', '<some path>anaconda/lib/python35.zip', '<some path>anaconda/lib/python3.5', '<some path>anaconda/lib/python3.5/plat-darwin', '<some path>anaconda/lib/python3.5/lib-dynload', '<some path>anaconda/lib/python3.5/site-packages/Sphinx-1.3.5-py3.5.egg', '<some path>anaconda/lib/python3.5/site-packages/setuptools-20.3-py3.5.egg', '<some path>anaconda/lib/python3.5/site-packages', '<some path>anaconda/lib/python3.5/site-packages/aeosa', '<some path>anaconda/lib/python3.5/site-packages/IPython/extensions', '<some path>.ipython']