Page tree

There is no packaging and versioning system for Python.

If you need a certain package or a specific version, you have to rely on either the packaging system of the operating system or one of the distribution methods.

If the operating system doesn't have the specific package or version you need, you have to rely on other installation methods.

The two most common are pip and conda (don't confuse with the anaconda python distribution).

Using pip install

In short, don't use plain pip install or even sudo pip install.

It can possibly ruin the whole system installation.

Use virtual environments instead, which can be done as user, not administrator.

Starting from Python3.5 the module venv is built in (see https://docs.python.org/3/library/venv.html for documentation).

It might need to be installed, so ask your local administrator in that case. The workflow is as follows:

  1. (skip if already done) create virtual environment
  2. activate virtual environment
  3. work/code/test
  4. de-activate virtual environment

Virtual environment handling

Usage:

Simply invoke python3 with the venv module and pass the name of the virtual environment as argument:

python3 -m venv <venvname>

This will create the basic setup for a virtual environment.

(If an error similar to "The virtual environment was not created successfully because ... is not installed" appears, the package python3-venv needs to be installed first; ask your local admin.)

It creates a new directory of the given name in the current working directory.

Activation happens by invoking the activate script in the bin/ subdirectory of this newly created directory:

source <venvname>/bin/activate

De-Activation is done by a similar  command:

source <venvname>/bin/deactivate

Deletion of a virtual environment is straightforward, simply by removing its directory.

Advantages:

You can have as many virtual environments as you disk spaces allows.

You can have multiple versions of packages and combinations on your system at the same time.

Since everything is located in user directories, there is no breaking the underlying operating system.

Deprecated: Python2.7

Virtual environments are an external package and should  be installed as system package, please ask your administrator to do so.

Once installed, create a virtual environment by invoking the command with the name of the venv as argument:

virtualenv <venvname>

The activation/deactivation is the same as for python3.

Working within an activated environment

Once activated, the terminal prompt will show the name of the activated virtual environment.

pip install can then be directly called from this prompt and any installation will only be performed within this environment.

If a certain package is only installed within this environment, it will only be available once this is activated.

Working with IPython

Unfortunately using IPython with virtual environments possibly creates trouble.

IPython is not always aware of virtual environments, respectively not part of the venv package.

If you experience trouble, please contact fs-ec@desy.de



  • No labels