Environment setup

 

Environment setup 

Setting up virtual python environment 

I am using mainly Python for this project on my Ubuntu. Here is the way how I set up virtual environments for the different aspects of the project. Each virtual environment can have specific set of libraries, even specific versions of those libraries.
Checking the version of Python on my system with:
python3 -V
returns
Python 3.10.6
.
This will be the base Python used when creating the environement
For installing venv via pip use the following comand:
python3 -m pip install --user virtualenv
To create a virtual environment with the name venvdemo use the following command:
python3 -m venv venvdemo
A folder in the current directory will be created with the same name venvdemo If you want to place the directory for the venv not in the current directory, just provide the path. Inside the venvdemo you can find the config file pyvenv.cfg to see which Python was used for this environment. You will also find the bin directory where the activate script is located.
To activate the virtual environment you have to provide the path to activate:
source thepathto/venvdemo/bin/activate
Once the environment is active it will look something similar in the terminal :
(venvdemo) 5q@5q-PC:~/somePath/venvDemoFolder$ ls
venvdemo
Trying to remember where you placed it every time is not necessary, you can define an alias for easier activation. This is very easy. If you are in the venvdemo directory, use this command to send this line at the end of yourline that will add the alias to the end of your bashrc.
echo "alias venvdemo=\"source $(pwd)/bin/activate\"" >> ~/.bashrc
Activate the environment and install some useful libraries, as you would usually do. Here is an example:
python3 -m pip install numpy
python3 -m pip install pandas
Use the pip freeze command to make a snapshot of the libraries with their current versions. The list will be formatted in a way that later can be used for reference fo installation:
python3 -m pip freeze > requirements.txt
Open the file to see the list with libraries:
open requirements.txt
This file now can be used for installing all the same versions into another virtual environment. From inside an activated virtual environment run the next command to do that:
python -m pip install -r ../pathToTheFile/requirements.txt
To deactivate the active environment use
deactivate
and to delete a virtual environment you just have to remove its folder recursively
rm -r venvdemo
If you want to move your environment in different directory, just get the requirements and recreate it in the new place.