This project has moved. For the latest updates, please go here.


os.environ['PYTHONPATH'] is always empty


1) Set PYTHONPATH in your system environment variables to some path.
2) Open Visual Studio, and edit the Environment Options for installed Python Interpreter via Options -> Python Tools -> Environment Options, and set the "Path Environment Variable" field to PYTHONPATH.
3) Create an Interactive Window for this prompt.
4) import os, and type os.environ['PYTHONPATH']

The result is always the empty string, no matter what your system environment variable is. It seems like the contents of this environment variable should be appended to sys.path and left intact. Instead it seems the contents of the variable are cleared, and not appended to sys.path.

It may be more intuitive and flexible from the user's point of view if the Python Environment options simply allowed you to add an arbitrary list of directories, and not deal with the system environment variable at all.
Closed Feb 9, 2015 at 11:23 PM by Zooba


Zooba wrote Jul 17, 2014 at 8:13 PM

This is deliberate, since it's so easy to get into trouble when PYTHONPATH is set globally (for example, you can hijack the standard library or try/fail to load a Python 3.x package into Python 2.x).

Projects can have Search Paths added, which are propagated into PYTHONPATH when you do anything with the project. There is also an option (Tools-Options-Python Tools-General, IIRC) to allow PYTHONPATH to be retained everywhere in the latest beta release.

We have a separate feature on per-environment search paths, but it's not going to happen for PTVS 2.1 at this point.

zturner wrote Jul 17, 2014 at 8:56 PM

I guess the confusing part is that if you look at the properties of the default Python Environment, the "Path Environment Variable" field is set to PYTHONPATH and is read-only. So it's natural to think that, when creating a custom python environment, you should also set the Path Environment Variable to PYTHONPATH. Then you do that, and it doesn't work as expected. So maybe there's some kind of UI or other change that could make this more clear?

Either way, thanks for the explanation.

Zooba wrote Jul 17, 2014 at 10:12 PM

We constantly run into confusion with that field... it's actually specifying the name of the variable that we should put search paths into when we run. For IronPython, it's IRONPYTHONPATH, but for anything based on CPython it's going to be PYTHONPATH. Mainly it's there for other implementations of Python, but since anyone can make a C++ application that uses a different variable, it's going to be necessary.

We probably need some sort of tooltip that says "PTVS never reads this variable; it will set it when running your Python interpreter".