ChatGPT解决这个技术问题 Extra ChatGPT

Does `anaconda` create a separate PYTHONPATH variable for each new environment?

I am starting to work with the Python Anaconda distribution from Continuum.io to do scipy work.
I have been able to get Anaconda up and running, but I cannot tell whether Anaconda creates a new PYTHONPATH environment variable for each new environment it creates, or whether it relies on the common system PYTHONPATH.

I could not find any information on this in the documentation.

Further, when I did a printenv, I did not see a PYTHONPATH variable in the newly created environment --though I did find a few new anaconda created environment variables.

The best I can find is that Anaconda added some Anaconda directories and the new environment directory to the head of PATH variable --but this does not necessarily isolate the new package from the system environment but it is close.

Does anyone know the answer to this question or found a way to deal with this concern?

But not every Python package is available in Anaconda, so what about when you also use pip to install packages, as is common? Then you do need a (Python-version-specific) PYTHONPATH, so you need to worry about changing/setting it in each shell/each time you source the activate script to change Python version. Right?

i
inodb

Anaconda does not use the PYTHONPATH. One should however note that if the PYTHONPATH is set it could be used to load a library that is not in the anaconda environment. That is why before activating an environment it might be good to do a

unset PYTHONPATH

For instance this PYTHONPATH points to an incorrect pandas lib:

export PYTHONPATH=/home/john/share/usr/anaconda/lib/python
source activate anaconda-2.7
python
>>>> import pandas as pd
/home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/hashtable.so: undefined symbol: PyUnicodeUCS2_DecodeUTF8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/__init__.py", line 6, in <module>
    from . import hashtable, tslib, lib
ImportError: /home/john/share/usr/lib/python/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/hashtable.so: undefined symbol: PyUnicodeUCS2_DecodeUTF8

unsetting the PYTHONPATH prevents the wrong pandas lib from being loaded:

unset PYTHONPATH
source activate anaconda-2.7
python
>>>> import pandas as pd
>>>>

Is it safe to use PYTHONPATH to point to locally produced files and modules? Is there a better way than using PYTHONPATH?
I guess for dev that works fine. I tend to run python from the dir that has the module I'm working on, which is basically the same thing. You can also use pip install -e python_module_dir with the anaconda env loaded so you don't have update the PYTHONPATH.
a
asmeurer

No, the only thing that needs to be modified for an Anaconda environment is the PATH (so that it gets the right Python from the environment bin/ directory, or Scripts\ on Windows).

The way Anaconda environments work is that they hard link everything that is installed into the environment. For all intents and purposes, this means that each environment is a completely separate installation of Python and all the packages. By using hard links, this is done efficiently. Thus, there's no need to mess with PYTHONPATH because the Python binary in the environment already searches the site-packages in the environment, and the lib of the environment, and so on.


Using it on windows machine and the PYTHONPATH from the two Installs definitely differ. The anaconda one does not have my modules directory in it. For reference I added it from the Advanced system Settings in Windows 7. Looks fine from python it is in ipython notebook that it is broken