Virtual Python Environment

Python virtual environments are the common way to install and collect all python packages required for a specific project.

Setup

The setup process for your personal virtual environment is also described in pycharm.

The preferred way is using the virtualenvwrapper - so you can use: workon to load your virtual environment:

mkvirtualenv --python=`which python<VERSION>` <VENV_NAME>

You can also create basic virtual environments (unmanaged):

python -m venv <Path-To-VEnv>

For some cases it might be necessary to use inherited system site packages (e.g VTK). This will include all python packages installed on the system to be available in your virtual environment. It is advised only (not) to use for environments where it is really necessary.

mkvirtualenv <VENV_NAME> --system-site-packages
python -m venv <Path-To-VEnv> --system-site-packages

The virtual environments created with mkvirtualenv can also be used and managed within PyCharm.

Activation (of basic virtual environments)

You can activate the virtual environment in any terminal by sourcing the activation script:

source <Path-to-VEnv>/bin/activate

Deactivation

Deactivate the virtual environment by typing:

deactivate

Remove environment

First deactivate the virtual environment:

deactivate

When using virtualenvwrapper one can simply use the command:

rmvirtualenv <VENV_NAME>

to delete it. If the virtual environment was manually created one must delete the folder by hand:

rm -r <Path-to-VEnv>/<VENV_NAME>

PIP

PIP (documentation) is the tool to manage python modules inside your virtual environment.

Install a package

pip install <Package-Name>

Uninstall a package

pip uninstall <Package-Name>

Update a package

pip install --upgrade <Package-Name>

Install packages in Users-home

pip install --user <Package-Name>

Tips

Update all packages

Create a new alias in your

~/.bash_aliases

file by inserting this line:

pip-upd='pip freeze --local | grep -v ^[eV] | cut -d = -f 1  | xargs pip install -U'

Explanation: This command

  1. retrieves a list of install packages,
  2. Packages that start with e or V are removed from the list and therefor not updated (adjust to your personal needs)
  3. removes the package version information
  4. installs the packages.

Install packages from requirements file

pip install -r <file name> 

Build Python on Cluster

Collect your desired python version

wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz 
tar xzf Python-3.x.x.tgz
cd Python-3.x.x

and configure/compile your python:

./configure --enable-optimizations --prefix=/home/username/.local
make altinstall

Make altinstall avoids, that you try to overwrite the system python. You can call your own python now by “python3.x”

Log In