Unexpected python paths in conda environment

This is expected behavior (see PEP 370) and partially why Anaconda recommended against user-level package installations.

The site module is responsible for setting the sys.path when Python is initializing. The code in site.py specifically appends the user site prior to appending the prefix site, which is what leads to this prioritization. The motivation according to PEP 370 is that users would have a Python installed at system-level, but want to prioritize packages they install at the user level, hence the user site should load prior to the prefix site.

Options

There are several options for avoiding the user-level site-packages from getting loaded.

1: Environment variable

The environment variable PYTHONNOUSERSITE will toggle loading of user-level site-packages. Namely,

PYTHONNOUSERSITE=1 python -c "import sys; print(sys.path)"

2: Python -s flag

Alternatively, the Python binary has an -s argument to specifically disable user-level site packages.

python -s -c "import sys; print(sys.path)"

3: Remove (and avoid future) user-level installs

The Conda recommendation is to avoid pip install --user altogether, which would be interpreted that one should remove the ~/.local/lib/python* folders from your system.

4: Automated Conda environment variable

Conda Forge package

The Conda Forge package conda-ecosystem-user-package-isolation will automatically set PYTHONNOUSERSITE=1 during environment activation.

If you would like all environments to have such isolation by default, then consider adding this to the create_default_packages configuration list:

conda config --add create_default_packages conda-ecosystem-user-package-isolation

Note that this package also sets R_LIBS_USER="-", isolating any R environments from user-level packages.

Alternative packages

If you want a more granular option, I have also created separate packages that set just the PYTHONNOUSERSITE=1 and PYTHONPATH="" environment variables, which can be installed with:

## set PYTHONNOUSERSITE=1
conda install merv::envvar-pythonnousersite-true

## clear PYTHONPATH
conda install merv::envvar-pythonpath-null

Leave a Comment