4
votes

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.

3
Damn, sorry for the layout, but I seem unable to add newlines for some reason. I hope the question is clear like this.saroele
How did you start IPython? What do you get for os.environ['PYTHONPATH'] in IPython?minrk
I get 'C:\\JModelica.org-1.8\\Python' @minrk : you found the clue. When I start IPython by typing 'ipython' in a command window, the sys.path is the same. I started IPython from a shortcut in windows/start window... But I still don't understand what's going on.saroele
How do you set PYTHONPATH? setuptools builds the start menu items, so maybe it doesn't start them in a way that inherits your environment. IPython doesn't actually control those things.minrk
I set PYTHONPATH via the windows control panel/environment variables. I tried both the settings for my user profile and system.saroele

3 Answers

2
votes

The .exe launchers linked from the Start Menu are made by setuptools, and it is possible that they do not properly setup your environment (I don't know enough about Windows environments to say for sure, or if that is fixable).

But if you start IPython from the command-line, it will definitely inherit your environment properly.

4
votes

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.

1
votes

I just solved a similar problem on Linux running Python 2.6.

It turns out I had my virtual environment set up to ignore the system path.

It was fixed by closing all python programs and running:

virtualenv --system-site-packages ~