Virtual environments
In order to work in a comfortable way, it is at least useful to use virtual environments for Python
and R
. Why? During the academic year, you shall work on several software projects. Each project is likely to require installing packages and dependencies. Packages can be downloaded from pypi.org and installed using pip
(for example). You may try to work with a large set of installed packages that fits all your needs and requirements. But what if packages required in different projetcs have incompatible dependencies? Installing packages at system level will take you to a dead end. It is much safer and easier to hava a virtual environment for each project.
https://virtualenv.pypa.io/en/latest/
Install a python version in your working directory
$ cd /path_to_working_dir
$ mkdir ./temp
$ cd temp
$ wget https://www.python.org/ftp/python/3.12.9/Python-3.12.9.tar.xz
$ tar xJf Python-3.12.9.tar.xz
$ cd Python-3.12.9
$ ./configure --prefix=/path_to_working_dir/
$ make
$ make install
$ cd ..
$ # rm -rf Python-3.12.9.tar.xz Python-3.12.9 # if you want
You are in your working directory, pwd
should return /path_to_working_dir/
.
$ ./bin/python --version
should return Python 3.12.9
.
Creating a virtual environment
Package venv
belongs to the standard library. The next command creates a virtual environment for python3.12.9
in subdirectory .venv
$ ./bin/python3.12 -m venv .venv
The structure of subdirectory .venv
is summarized by:
$ tree -L 1 .venv/
.venv/
├── bin
├── etc
├── include
├── lib
├── lib64 -> lib
├── pyvenv.cfg
└── share
Subdirectory bin
contains links to the Python interpreter to be used when the virtual environment is activated. It also contains the locally installed packages.
Activating the virtual environment
To activate the virtual environment:
$ source .venv/bin/activate
Once the virtual environment is activated, locally installed packages are availabale, and the default Python version is the one used to create the virtual environment.
Activating the virtual environment modifies the prompt
(.venv) $ python --version
Installing packages in your environment
To install ze_pkg
in your (virtual) environment
(.venv) $ pip install ze_pkg
Requirements
Do not add/commit/push
your virtual environment to git
. Add a line to .gitignore
avoid committing anything in the virtual environment directory.
Save the list of installed packages in a text file
(.venv) $ pip freeze > requirements.txt
Add the text file to the index
$ git commit requirements.txt -m "favorite packages"
$ git push
If somebody (including you) clones/fork the repo, she shall install the same version of Python, create a virtual environment, activate the virtual environment, and install your favorite packages.
(.venv) $ pip install -r requirements.txt