Title: PYTHONPATH is overwritten on Windows
Type: Severity: normal
Components: Versions: Jython 2.7
Milestone: Jython 2.7.2
Status: pending Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: jeff.allen, pekka.klarck, zyasoft
Priority: Keywords:

Created on 2015-04-26.19:28:31 by pekka.klarck, last changed 2018-02-24.23:05:48 by jeff.allen.

msg9971 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-04-26.19:28:31
To reproduce:

    C:\>set PYTHONPATH=foo
    C:\>jython -c "import os; print os.getenv('PYTHONPATH')"

Tested with Jython 2.7 RC 3. Wasn't a problem with earlier pre-releases (don't remember exactly what I tested last on Windows). Most likely an issue in new jython.exe launcher. 

Overriding general purpose environment variables like that is definitely not good. This is problem at least for Robot Framework that uses PYTHONPATH to find test libraries regardless the Python interpreter that is used.
msg9974 (view) Author: Jim Baker (zyasoft) Date: 2015-04-26.21:37:01
Related to this issue in PyInstaller, which is how we build jython.exe, so it's been true ever since we started using jython.exe as of RC1:

We could look at alternative packaging with py2exe and cx_Freeze
msg9975 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-04-26.22:00:48
Auch. Changing the tool to create jython.exe at this point would not be fun. Could Jython somehow see the original PYTHONPATH via Java APIs or otherwise?
msg9976 (view) Author: Jim Baker (zyasoft) Date: 2015-04-26.23:36:04
Unfortunately PYTHONPATH is no longer available, since it was reset by the launcher due to the issues with PyInstaller. Your best bet may be to simply directly execute the result of calling bin\jython --print. So my Windows VM, this looks like:

C:\jython2.7rc3>bin\jython --print
"C:\Program Files\Java\jdk1.7.0_55\bin\java" -Xmx512m -Xss1024k -classpath C:\jython2.7rc3\jython.jar;. -Dpython.home=C:\jython2.7rc3 -Dpython.executable=C:\jython2.7rc3\bin\jython.exe -Dpython.launcher.uname=windows -Dpython.launcher.tty=true org.python.util.jython

You can even directly execute this on the command line, using an appropriate somewhat familiar to standard shell programming on Unix-like systems (see First, let's set up the PYTHONPATH:

C:\jython2.7rc3>set PYTHONPATH=foo

Then try out:

C:\jython2.7rc3>for /f "delims=" %i in ('bin\jython --print') do set call_jython=%i
C:\jython2.7rc3>call %call_jython% -c "import os; print os.environ['PYTHONPATH']"

It's possible to put this extra level of indirection in a minimal jython.bat that also calls the args. Here's my first attempt at such a wrapper script, which is likely to have errors, since I have never written a Windows batch script before:

@echo off
for /f "delims=" %%i in ('%~f0\..\jython.exe --print') do call %%i %*

(thanks to

However, you have to explicitly call it jython.bat (or name of your choice), since I don't think you can simply rename jython.exe to get it out of its way, due to how other installed scripts like pip.exe will bind jython.exe. Still it does seem to work:

C:\jython2.7rc3>bin\jython.bat -c "import os; print os.environ['PYTHONPATH']"
msg9981 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-04-27.10:41:51
Creating jython.bat wrapper is definitely an option, but I don't think that's a solution I want to present to our Windows/Jython users. A workaround in our code to see the original PYTHONPATH value somehow would be much better. Not sure is there any easy way to accomplish that, though.

If there is no easy way to fix or workaround this so that users don't see the change, I think we should change Robot Framework to not do anything special with PYTHONPATH:
msg10409 (view) Author: Jim Baker (zyasoft) Date: 2015-10-29.22:36:00
Hopefully we can fix with different launcher exe wrapper generator, such as by using cxFreeze
msg11705 (view) Author: Jeff Allen (jeff.allen) Date: 2018-02-24.23:04:17
I don't understand some of this discussion. In particular, seems to be about something else.

However, the behaviour currently is:

PS 272a1> $env:PYTHONPATH="foo"
PS 272a1> jython -c "import os; print os.getenv('PYTHONPATH')"

Of course PYTHONPATH makes no difference to the Jython path, but if you were to launch Python from within Jython, it should contribute to sys.path.

PS 272a1> jython
Jython 2.7.2a1+ (default:d74f8c2cd56f, Feb 24 2018, 17:18:53)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_151
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> os.getenv('PYTHONPATH')
>>> sys.path[:2]
['', 'C:\\Jython\\2.7.2a1\\Lib']
>>> import subprocess
>>>['python', '-c', 'import sys; print sys.path[:2]'])
['', 'C:\\Users\\Jeff\\Documents\\Jython\\272a1\\foo']

Does that mean this is fixed by work on the launcher that has happend since?
Date User Action Args
2018-02-24 23:05:48jeff.allensetstatus: open -> pending
resolution: fixed
2018-02-24 23:04:18jeff.allensetnosy: + jeff.allen
messages: + msg11705
2015-10-29 22:36:00zyasoftsetmessages: + msg10409
milestone: Jython 2.7.2
2015-04-27 10:41:51pekka.klarcksetmessages: + msg9981
2015-04-26 23:36:04zyasoftsetmessages: + msg9976
2015-04-26 22:00:49pekka.klarcksetmessages: + msg9975
2015-04-26 21:37:01zyasoftsetnosy: + zyasoft
messages: + msg9974
2015-04-26 19:28:31pekka.klarckcreate