Setup a virtual environment for Python

This guide will tell you how to setup a virtual environment for your Python packages. Using virtual environments makes your setup more robust on the one hand (if anything goes wrong you can just setup a new virtual environment instead of trying to fix your existing installations) and more portable on the other.

Virtualenv

Python 2

Start off by logging into Holodeck and make sure you are in your personal holohome folder. To do this either log in fresh by typing
gsissh holodeck1.aei.uni-hannover.de

or when logged in
cd ~

We will create the virtual environment here as this folder is shared across all machines. Furthermore, this way you can more easily start your environment from anywhere. To setup a virtual environment choose a name (i.e. 'env') and replace <name> in the code with it.

After use the following commands.
virtualenv <name>

source <name>/bin/activate

pip install --upgrade pip

pip install --upgrade setuptools

deactivate

The commands do the following (from top to bottom):
  • Setting up the virtual environment with name <name>
  • Starting the virtual environment with name <name>
  • Updating pip to the newest version
  • Updating setuptools to the newest version
  • Exiting the virtual environment

If you want to start the virtual environment after having created one, type:
source ~/<name>/bin/activate

When inside this environment you can install packages locally such that they do not effect the entire system. Packages installed in this environment will only be accessible when the environment is active. You can check if you are in an environment by looking at your command line. If you are inside the environment you should see
(<name>)$USERNAME@holodeck:
where <name> is the name of the environment and $USERNAME is your own username.

To deactivate the environment and return to the standard system, type
deactivate

Python 3

Start off by logging into Holodeck and make sure you are in your personal holohome folder. To do this either log in fresh by typing

gsissh holodeck1.aei.uni-hannover.de

or when logged in

cd ~

We will create the virtual environment here as this folder is shared across all machines. Furthermore, this way you can more easily start your environment from anywhere.

To setup a virtual environment choose a name (i.e. 'env') and replace <name> in the code with it.

After use the following commands.

virtualenv <name> -p python3.4

source <name>/bin/activate

pip3 install --upgrade pip

pip3 install --upgrade setuptools

deactivate

The commands do the following (from top to bottom):
  • Setting up the virtual environment with name <name> for Python 3

  • Starting the virtual environment with name <name>

  • Updating pip to the newest version
  • Updating setuptools to the newest version
  • Exiting the virtual environment
In case you get a version error, please check the installed version of Python 3 by typing

python3 -V

and change the first command to the correct version number (only the second digit has to change).

Example:

Say Python 3.6.7 was installed on Holodeck. Then the version-command returns

python3 -V

>> Python 3.6.7

Therefore the first command to setup the virtual environment has to be

virtualenv <name> -p python3.6

If you want to start the virtual environment after having created one, type:

source ~/<name>/bin/activate

When inside this environment you can install packages locally such that they do not effect the entire system. Packages installed in this environment will only be accessible when the environment is active. You can check if you are in an environment by looking at your command line. If you are you should see

(<name>)$USERNAME@holodeck:

where <name> is the name of the environment and $USERNAME is your own username.

Also, inside of this environment you can run Python 3 scripts simply by typing

python <script_name>.py

as Python 3 is the standard interpreter within.

To deactivate the environment and return to the standard system, type

deactivate

Anaconda

For Anaconda there is not really a difference between Python 2 and Python 3, so this guide applies to both versions equally. Just choose the correct version to download.

As the naming schemes can be a bit confusing an example will be provided when commands are being executed on copied strings. The abstract form will be given first with the example in brackets right behind. The example is the most recent version of Anaconda for Python 2 as of the time of writing.

As a disclaimer, this page is strongly based on this guide, hence if anything is unclear please refer to this guide.

Installing Anaconda

To start of we need to go the download section of the Anaconda website and get the link to the most recent version of Anaconda. In this guide we will use Anaconda for Python 2. You should choose the version for Linux 64-Bit (x86). (See image below) Right click on the download link and choose 'Copy Link Location'. Alternatively you can download the file to your local system and upload it to Holodeck afterwards. This guide however will assume you have copied the link.

Anaconda_install_1_large.png

Now log into Holodeck and navigate to your /holohome directory. There type (you can paste the link you have copied in the step before by pressing Ctrl + Shift + V):

curl -O <download_link> (curl -O https://repo.anaconda.com/archive/Anaconda2-2019.03-Linux-x86_64.sh)

This command will download the installer to your home directory.


The following step is optional. You can now verify the downloaded file, i.e. if the file is the same as the one on the Anaconda servers. To do this type

sha256sum <file_name> (sha256sum Anaconda2-2019.03-Linux-x86_64.sh)

which will produce an output of the following form

<string> <file_name> (cedfee5b5a3f62fcdac0a1d2d12396d0f232d2213d24d6dc893df5d8e64b8773 Anaconda2-2019.03-Linux-x86_64.sh)

The string is the hash value which you can compare to the Anaconda website here. (Make sure to choose the correct version you have downloaded. To compare the hash with the one on the website simply copy the string and search for it on the corresponding version website using Ctrl + F in your local browser.)

If the strings match, your installer is valid.


The next step is to install Anaconda. For this we simply need to execute the installer:

bash <file_name> (bash Anaconda2-2019.03-Linux-x86_64.sh)

You will be prompted with the Anaconda End User License Agreement. Read it and (if you do so) accept it in the end by typing 'yes'. ('y' won't work, to scroll down press Enter)

After having accepted the License Agreement Anaconda will ask you where to install. If you are unsure simply leave it at the default location by pressing Enter without entering anything. (The default location is your /holohom/$USERNAME directory)

The final question the installer will ask you is whether or not you want to initialize Anaconda. Again type 'yes'. Now restart your console and your environment will be set up.

To finalize the installation process we want to remove the installer. For this type

rm <file_name> (rm Anaconda2-2019.03-Linux-x86_64.sh)

Activate Anaconda

To activate the environment simply type

source ~/.bashrc

Inside this environment Python will run on the Anaconda interpreter and you can install packages via

conda install <package_name>

To create a new environment called <environment_name> type

conda create --name <environment_name>

You can activate your new environment by typing

source activate <environment_name>

For more information refer to the Conda User Guide or the Conda Cheat Sheet.

Deactivate Anaconda

To deactivate type

conda deactivate

See also

Quick start guide · Useful commands · Simple code example

-- MarlinSchFer - 25 Apr 2019
Topic attachments
I Attachment Action Size Date WhoSorted ascending Comment
Anaconda_install_1_large.pngpng Anaconda_install_1_large.png manage 32 K 30 Apr 2019 - 15:20 MarlinSchFer Graphics showing the correct version to download.
Topic revision: r5 - 23 Sep 2019, FlorianWicke
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback