Python¶
The default Python distribution for Ubuntu is installed. You can start it with python3
. To keep the system as flexible as possible for all users, no extra packages have been installed except pip
and venv
. To use other packages, follow the instructions below:
Virtual Environment¶
With a virtual environment, you create an isolated Python environment in which you can install other packages easily. You can create as many virtual environments as needed, typically one per project. This allows you to install different versions of packages that might be specifically needed for an application or might conflict with other packages.
To create a virtual environment, you use the venv
package. It is advisable to set up a new directory per virtual environment, preferably the root directory for your project. Furthermore, it can be very helpful to create a special text file containing all packages needed for a project. It is customary to use the filename requirements.txt
, in which you list each package on a separate line.
As a bonus, there is an easy way to automatically start the virtual environment when entering the directory for your virtual environment. This can be done with the direnv
application. When set up in your .bashrc
, this application reads the contents of the file .envrc
whenever you enter the directory or its subdirectories.
Create the Virtual Environment¶
For this instruction, we want to have a project
directory that will contain our Python code. We would like to use numpy
and matplotlib
in our Python code:
mkdir project
cd project
python3 -m venv env # The directory `env` is created for the virtual environment.
source env/bin/activate # Activate the virtual environment; check your prompt!
Install Packages¶
You now have the env
directory and have activated the virtual environment. You should see this indicated in your command line prompt with the addition (env)
. Now, we will install the packages:
# The following instruction creates the file `requirements.txt` with the contents "numpy" and "matplotlib".
cat << EOF > requirements.txt
numpy
matplotlib
EOF
pip install -r requirements.txt --upgrade
The pip
command will read the requirements.txt
file and install (or upgrade) the packages in the virtual environment (in the env
directory).
Automate Activation¶
With direnv
, the virtual environment is activated once you enter the directory. For this to work, you need to add the direnv hook to your .bashrc
once:
echo 'eval "$(direnv hook bash)"' >> .bashrc
Now, every time you log in to the server, this hook will be activated. For now, activate it manually with this command:
eval "$(direnv hook bash)"
Now, set up the directory to automatically activate the virtual environment:
cd project
cat << EOF > .envrc
source env/bin/activate
unset PS1
EOF
direnv allow
Check if the (env)
prompt changes when leaving and entering the directory with the .envrc
file.
Deactivate Manually¶
If you do not use direnv
, you can always deactivate the virtual environment with:
deactivate