PYTHONPATH when calling from ipython

Cecil Westerhof Cecil at decebal.nl
Sat May 23 13:30:55 EDT 2015


Op Saturday 23 May 2015 18:09 CEST schreef Peter Otten:

> Cecil Westerhof wrote:
>
>> Op Saturday 23 May 2015 15:25 CEST schreef Peter Otten:
>>
>>> Cecil Westerhof wrote:
>>>
>>>> Op Saturday 23 May 2015 11:12 CEST schreef Mark Lawrence:
>>>>
>>>>> On 22/05/2015 06:20, Cecil Westerhof wrote:
>>>>>> I am looking into using ipython instead of bash. But when I
>>>>>> call a python program from ipython PYTHONPATH is not set. So
>>>>>> pythonscripts that need a module through PYTHONPATH will not
>>>>>> work.
>>>>>>
>>>>>> I could do something like:
>>>>>> !PYTHONPATH=~/Python/PythonLibrary python2 …
>>>>>>
>>>>>> But I find that a little bit cumbersome. Is there a better way
>>>>>> to do it?
>>>>>>
>>>>>
>>>>> What makes you think this?  Have you tried:-
>>>>>
>>>>>>>> import os
>>>>>>>> os.environ['PYTHONPATH']
>>>>> 'C:\\Users\\Mark\\Documents\\Cash\\Python;C:
>>> \\Users\\Mark\\Documents\\MyPython'
>>>>>
>>>>> That might be from the command line interpreter but it also
>>>>> works the same from iPython for me on Windows 8.1.
>>>>
>>>> That does not change anything. The modules are not found. Also
>>>> not when using %run.
>>>
>>>
>>> That may be because ~ is not expanded.
>>>
>>> Try
>>>
>>> os.environ["PYTHONPATH"] =
>>> os.path.expanduser("~/Python/PythonLibary")
>>
>> That is not the problem:
>> os.environ['PYTHONPATH']
>> gives:
>> .:/home/cecil/Python'
>>
>> As I interpret it is that the very handy shell variable is not used
>> in ipython.
>>
>
> I can't confirm that finding. For test purposes I created
> foo/bar/hello.py. Then I verified that the hello.py module is not
> found when invoking the python3 interpreter from within ipython3.
> Once I update os.environ["PYTHONPATH"] the module is successfully
> imported. The complete session:

I should have checked better. I think I found a bug that made it look
like PYTHONPATH does not work.

In bash I give:
    echo $PYTHONPATH
this gives:
    .:/home/cecil/Python/PythonLibrary

Then I start ipython3 and get/do the following:
    Python 3.4.1 (default, May 23 2014, 17:48:28) [GCC]
    Type "copyright", "credits" or "license" for more information.

    IPython 2.2.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.

    In [1]: import os

    In [2]: os.environ['PYTHONPATH']
    Out[2]: '.:/home/cecil/Python'

And PYTHONPATH has a different value. That is why my module is not
found.

When I set PYTHONPATH to the correct value, everything works as
expected:
    In [3]: os.environ['PYTHONPATH'] = '.:/home/cecil/Python/PythonLibrary/'

    In [4]: !python2 postOnTwitter.py --used
    Citation has 50 saved messages of 126:
    [6, 55, 43, 82, 28, 116, 2, 50, 100, 5, 0, 122, 75, 51, 121, 60, 114, 13, 102, 78, 31, 107, 73, 109, 54, 119, 72, 90, 89, 113, 118, 41, 11, 27, 48, 77, 19, 111, 62, 98, 110, 9, 10, 115, 63, 15, 53, 101, 94, 92]

    Tips has 30 saved messages of 94:
    [20, 37, 7, 59, 45, 49, 40, 87, 79, 78, 31, 14, 15, 25, 84, 18, 91, 53, 8, 35, 80, 92, 34, 42, 74, 69, 64, 22, 86, 62]

That begs the question: what is the reason for the corruption of
PYTHONPATH?

I already answered it partly myself. When I change PYTHONPATH in bash
and call ipython3 again, it has the same value as before, so it does
not take its value from the calling bash as I would expect.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list