Python

Python Virtual Environments with venv

2024/03/15

Overview

I’ve used Python for a long time but mostly for light Linux administration work and small image processing projects. Lately, I’ve become more interested in Python development and will be documenting some of my experiences on this blog. I haven’t really used virtual environments, so I’ve decided to try them out.

Python virtual environments let us create isolated environments where we can install packages and dependencies specific to a project, without installing them for the entire system. This lets us manage different sets of packages and Python versions for different projects, ensuring consistency and reproducibility.

There appear to be a lot of solutions for this and I don’t really know what’s best. I’ve decided to go with venv simply because it was part of OpenAI’s ChatGPT tutorials. venv is a built-in python module for creating lightweight ‘virtual environments’.

Basic Usage

I’m using Ubuntu 22.04 on my personal development machines. venv is not installed by default, so I had to install it with apt

 sudo apt install python3.10-venv

Once installed, I can create new environments with the following command:

python3 -m venv testenv

This creates a new directory called testenv containing an isolated Python environment.

To use the environment, source the activate script in the bin/ directory

. testenv/bin/activate

You should see something like this:

brl000@quantum:~$ python3 -m venv testenv
brl000@quantum:~$ . testenv/bin/activate
(testenv) brl000@quantum:~$

From here you you can work with Python as you normally would. Install packages with pip3, run scripts, etc.

pip3 install --upgrade pip setuptools

pip install opencv-python
pip install opencv-contrib-python
pip install Pillow

When you’re done with the virtual environment, deactivate it by typing:

deactivate

You can reactivate it by sourcing the bin/activate script again.

Version Control and Python Projects

I plan on keeping virtual environments directories out of revision control. This can be done with git by adding the virtual environment directory to the .gitignore file in your project.

On the other hand, it’s useful to keep a record of projects dependencies in revision control. You can use pip3 freeze to save the dependencies into a file under revision control.

pip3 freeze requirements.txt

This will list the dependencies it puts into the file.

(testenv) brl000@quantum:~$ pip3 freeze requirements.txt
install==1.3.5
numpy==1.25.0
opencv-contrib-python==4.7.0.72
opencv-python==4.7.0.72
Pillow==9.5.0

You can later use requirements.txt to install dependencies on a new system

pip3 install --upgrade pip3 setuptools

About Me

Greg Gallardo

I'm a software developer and sys-admin in Iowa. I use C++, C#, Java, Swift, Python, JavaScript and TypeScript in various projects. I also maintain Windows and Linux systems on-premise and in the cloud ( Linode, AWS, and Azure )

Github

Mastodon

YouTube

About you

IP Address: 18.227.52.111

User Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Language:

Latest Posts

Iowa City Weather

Overnight

42 ˚F / 46 ˚F

Friday Night

41 ˚F / 50 ˚F

Saturday Night

35 ˚F / 48 ˚F

Sunday Night

32 ˚F / 44 ˚F

Monday Night

30 ˚F / 35 ˚F

Tuesday Night

24 ˚F / 31 ˚F

Wednesday Night

19 ˚F / 28 ˚F