Python – Ipython has different sys.path than python and does not use PYTHONPATH

ipythonpythonpythonpath

It seems that IPython does not take into account my PYTHONPATH, while a normal python interpreter does it. I'm on windows 7.

My PYTHONPATH:
C:\workspace\python;
C:\Python27\Lib\site-packages\spyderlib;
C:\Workspace\Python\awesim\awesim

Printing the sys.path:

import sys
for i in sorted(sys.path):
    print i

Here's what I obtain in IPython:

C:\JModelica.org-1.8\Python
C:\Python27
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\lib-tk
C:\Python27\lib\plat-win
C:\Python27\lib\site-packages
C:\Python27\lib\site-packages\PIL
C:\Python27\lib\site-packages\Pythonwin
C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg
C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg\IPython\extensions
C:\Python27\lib\site-packages\numpy-1.6.2-py2.7-win32.egg
C:\Python27\lib\site-packages\openpyxl-1.5.8-py2.7.egg
C:\Python27\lib\site-packages\pandas-0.8.1-py2.7-win32.egg
C:\Python27\lib\site-packages\pyzmq-2.2.0.1-py2.7-win32.egg
C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info
C:\Python27\lib\site-packages\sphinx-1.1.3-py2.7.egg
C:\Python27\lib\site-packages\statsmodels-0.4.0-py2.7-win32.egg
C:\Python27\lib\site-packages\tornado-2.3-py2.7.egg
C:\Python27\lib\site-packages\win32
C:\Python27\lib\site-packages\win32\lib
C:\Python27\lib\site-packages\wx-2.8-msw-unicode
C:\Python27\scripts
C:\windows\system32\python27.zip

And the same in a python console:

C:\Python27
C:\Python27\DLLs
C:\Python27\Lib\site-packages\spyderlib
C:\Python27\lib
C:\Python27\lib\lib-tk
C:\Python27\lib\plat-win
C:\Python27\lib\site-packages
C:\Python27\lib\site-packages\PIL
C:\Python27\lib\site-packages\Pythonwin
C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg
C:\Python27\lib\site-packages\numpy-1.6.2-py2.7-win32.egg
C:\Python27\lib\site-packages\openpyxl-1.5.8-py2.7.egg
C:\Python27\lib\site-packages\pandas-0.8.1-py2.7-win32.egg
C:\Python27\lib\site-packages\pyzmq-2.2.0.1-py2.7-win32.egg
C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info
C:\Python27\lib\site-packages\sphinx-1.1.3-py2.7.egg
C:\Python27\lib\site-packages\statsmodels-0.4.0-py2.7-win32.egg
C:\Python27\lib\site-packages\tornado-2.3-py2.7.egg
C:\Python27\lib\site-packages\win32
C:\Python27\lib\site-packages\win32\lib
C:\Python27\lib\site-packages\wx-2.8-msw-unicode
C:\Workspace\Python\awesim\awesim
C:\windows\system32\python27.zip
C:\workspace\python

You can see that the normal python console reflects the PYTHONPATH, but the IPython output does not.

Thanks on beforehand for your clues.

Best Answer

Apparently this happens when the sys.paths of Python and IPython are different.

For IPython some quick temporary solution would be:

import sys
sys.path.append('your paths')

Personally I like to put this in scripts I'm working on in order to include my modules that are organised in a project directory including their sub-directories. (PS. don't forget: sub-dirs are included by python in the path if the main directory and the desired sub-dir contain an (empty) __init__.py file.)

A permanent solution would be to create a new IPython profile:

ipython profile create
ipython locate
/Users/username/.ipython

go to the ipython profile and edit: profile_default/ipython_config.py

Add the following

c.InteractiveShellApp.exec_lines = [
     'import sys; sys.path.append("you paths")'
 ]

This works on Linux and should be able to work on Windows also I guess.

Related Topic