Manage Python project using PIP and virtual environments

Posted by MaX on January 21, 2021

Setup minimal Python3 Project with virtual environments

The virtual environment is a simple yet effective way to configure and maintain the set of libraries you need for your project. It is very minimal, but the file requirements.txt, it can be compared to a minimalist version of package managers configurations like pom.xml for Java or package.jason for NodeJS. In this short post I will pull together few steps to create an environment form scratch, save the libs configurion and reload them.

Create and activate the virtual env inside your project folder.

Linux:

1
2
3
sudo apt-get install python3-venv
python3 -m venv .venv
source .venv/bin/activate

Windows:

py -m venv .venv
.venv\Scripts\activate.bat

Install the libraries (for Windows use py instead of python3):

Let’s now install two widely used libraries: mathplotlib and numpy

1
2
3
apt-get install python3-tk
python3 -m pip install matplotlib
python3 -m pip install numpy

Automate the installation:

Save the libraries into requirements.txt

1
2
pip list 
pip freeze > requirements.txt

Use the requirements list when you are reinstalling the project from scratch (i.e. after the checkout from a repository).

1
2
python3 -m pip install -r requirements.txt
Jupyter Notebooks requires ipykernel

References:

If you want to learn more you can llok at this interesting articles: