masdeseiscaracteres
10/10/2017 - 1:41 PM

Conda environments tutorial

Conda environments tutorial

How is sys.path populated?

What is sys.path?

It is just a list of strings that specifies the search path for modules. Refer to the official doc for more details.

It is populated in two steps:

Entries added on interpreter startup

The first item of this list, sys.path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), sys.path[0] is the empty string, which directs Python to search modules in the current directory first.

Then the entries defined in the environment variable PYTHONPATH are appended to sys.path.

Entries added by the site.py module

Afterwards, the sys.path is the populated by the module site (which is automatically imported on startup, python -S avoids it) according to the following steps:

  1. Four directories are added to the path (if they exist):
    • sys.prefix
    • sys.prefix + site_packages_tail
    • sys.exec_prefix
    • sys.exec_prefix + site_packages_tail

where site_packages_tail is lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh)

  1. Search those directories for *.pthfiles and add their contents to sys.path. Execute also any import statement.
  2. Execute a sitecustomize.py module if it is found in the path already added to sys.path
  3. Execute a usercustomize.py module if it is found in the path already added to sys.path

The final status of the sys.path variable and user-specific directories can be checked by doing:

python -m site

Conda: how to create fully isolated environments

Show conda configurations. Useful to identify which channels, packages (to pip or not to pip) it is using by default:

conda config --show

Environments created by conda are not fully independent of others. For example, if you do:

conda create -c conda-forge -n env1 python=3.5
activate env1

pip listing this new environment will show packages in the new environment but also those in the USER_SITE (https://github.com/conda/conda/issues/394). To create fully isolated environments set the environment variable

PYTHONNOUSERSITE=1

Create environment in a specific location

Create an environment called env1 in D:\Users\username\.conda\envs\:

conda create -p D:\Users\username\.conda\envs\env1

Check results by showing the list of existing environments:

conda info --envs