Explore topics

Python Virtual Environment Configuration and Setup

Why using virtual environment

Virtual env provide Python with temporary env which separated from system installed Python env to run or develop Python project.

Separating Python code from system env will provide cleaner development env and better version control plus a clean installation for deployment.

Commandline runs for virtual environment

Init and activating virtual env

python -m venv .venv --prompt <projectName>

This will start a new virtual env inside of the current dir and stored everything inside of .venv hidden folder.

.venv folder will stored everything related to the virtual env including the module installed.

The --prompt option flag value will be used in the bash prompt. Example:

(projectName) fitri@host $

The prompt will provide a good hint of what Python env currently active.

Activating and deactivate the virtual env

Activating virtual env

Sourcing will replace terminal env from central installed Python to virtual.

source .venv/bin/activate.csh

Deactivate virtual env.

Going back to the central Python env setup from venv env.

deactivate

Freezing module requirement

To publish the project and freeze the requirement of all the modules used inside of the project, either use freeze or pipreqs.

Depending on situation freeze can be better option than pipreqs or vice versa.

For example a project that was using Python module as but not imported directly to any of the module inside of the project, freeze is better since it tracked installed modules inside of the site-packages dir in virtual env.

Example of this is Python formatter module black. This module was not usually import into local Python file but called directly thru the commandline.

python -m black main.py

But if the project nature of importing the module directly into the Python script/module then pipreqs works better since it pulling the modules imported list inside the project dir.

Requirement file thru freeze

This going to track all external module installed in site-package dir and their version and print out the output inside of requirements.txt file.

pip freeze | tee requirements.txt

Requirement file with pipreqs

Pipreqs is an external module so install it thru pip.

pip install pipreqs

Pipreqs does not need arg it will auto create requirements.txt file.

pipreqs

In case a new module was added and requirements.txt file need to be updated run it with --force flag to force the file update.

pipreqs --force

Make sure to install the modules using pip when activating the env!

Installing module from requirements.txt file

To install the module with the version listed from the requirement file just run the pip install and provide the requirement file.

pip install -r requirements.txt

Install single module with specific version

In case we need a single module with different version that what listed inside of requirement file, uninstall the module first then reinstall it back with the version we need.

pip install torchmetrics==0.11.4

That's it!

Published on