Python coding

Using virtual environments

last updated: 2025-04-04

Quick links

Intro

My software to create static homepages from markdown files called PYSHPCREATOR (PYthon Simple HomePage CREATOR) did not run with newer Python versions because of some older libraries.

A virtual environment helps with such problems, because it isolates the project from other environments, including the global interpreter environment. So no more complications from conflicting package versions, and the possibility to use an older python version. More in the python docs: https://docs.python.org/3/library/venv.html.

How to create a virtual environment?

Let's create a virtual environment in our software folder (e.g. pyshpcreator/Python) It is good to install the virtual environment in a separate folder (e.g. .venv)), so it can be excluded if versioning with e.g. git.

    cd ~/pyshpcreator/Python
    python -m venv .venv

In the bin directory in the .venv folder I see that python 3.12 was used and pip is there to install further packages.

Activate the virtual environment

To work in our virtual environment, we have to activate it:

    cd ~/pyshpcreator/Python
    source .venv/bin/activate

Now we remark the following prefix before our promt: (.venv).

Installing modules in the virtual environment

Now we can install our missing modules (e.g. markdown2, pygments) with pip:

    pip install markdown2 pygments

After installing all the modules we can check the requirement (all the modules) with the following command:

    python3 -m pip freeze

Here we see the output for my PYSHPCREATOR software:

    bcrypt==4.2.1
    beautifulsoup4==4.12.3
    bs4==0.0.2
    certifi==2025.1.31
    cffi==1.17.1
    charset-normalizer==3.4.1
    cryptography==44.0.0
    idna==3.10
    markdown2==2.5.2
    paramiko==3.5.0
    pycparser==2.22
    Pygments==2.19.1
    PyNaCl==1.5.0
    requests==2.32.3
    soupsieve==2.6
    urllib3==2.3.0

Or better we can create a file called requirements.txt containing all the requirements:

    pip3 freeze > requirements.txt

Using a downloaded program that contains a requirements.txt file

If a requirements.txt file is given by the maintainer of the software, it is easy to recreate the modules in a new virtual environment. When you create the virtual environment, you need to use the same python version than the maintainer, otherwise it is possible that module versions will not fit!

First download the repo from github, create and activate the virtual environment:

    cd ~
    git clone https://github.com/weigu1/pyshpcreator.git
    cd pyshpcreator/Python
    python -m venv .venv
    source .venv/bin/activate

Now we can install all the dependencies with:

    python3 -m pip install -r requirements.txt

Finally we open the program withNow we can open the program with:

    python pyshpcreator.py

Leave the virtual environment

We leave with:

    deactivate

Using older Python versions in a virtual environment

If you want to or need to use a different python version you have to install it first on your linux system and use then the right command (e.g. python3.9):

    sudo apt install python3.9
    mkdir /savit/programming/python/test
    python3.9 -m venv /savit/programming/python/test

Interesting links