This part of the guide is generic and covers basic virtual environment usage. However, it is Linux specific since we are focusing on Raspberry Pi usage.
The basic steps are:
- Create the venv - this is done once (per venv)
- Activate the venv - this is done every time a venv is to be used
- Use the venv - run your Python code here
- Deactivate the venv - optional
Create the venv
To create a new virtual environment, use the venv module and give it the name for the virtual environment.
python3 -m venv foobar
The virtual environment name can be any valid name. A lot of tutorials show "venv" or ".venv" for the name. That's fine but tends to imply that name is important. It's not. So here we are intentionally picking something silly, foobar
, to be clear that the virtual environment name is just a name.
So what did that command do? It created a new folder with the virtual environment name and set up a folder structure that mimics the layout the Python interpreter expects.
pi@raspberrypi:~ $ python3 -m venv foobar pi@raspberrypi:~ $ ls foobar/ pi@raspberrypi:~ $ ls foobar/ bin/ include/ lib/ pyvenv.cfg pi@raspberrypi:~ $
Activate the venv
The main way to use the virtual environment is by "activating" it. This is done by "sourcing" the activate
script found in the virtual environments bin folder.
source foobar/bin/activate
The prompt should change to include the virtual environments name. This both helps indicate a venv is in use (active) and which one.
pi@raspberrypi:~ $ source foobar/bin/activate (foobar) pi@raspberrypi:~ $
Use the venv
Once the virtual environment has been activated, Python usage proceeds in the normal fashion. Running python
or pip
will be done in the context of the virtual environment.
Modules installed with pip will be placed in the local venv folders - sudo should not be used.
(foobar) pi@raspberrypi:~ $ pip list Package Version ---------- ------- pip 23.0.1 setuptools 66.1.1 (foobar) pi@raspberrypi:~ $ pip install click Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting click Downloading https://www.piwheels.org/simple/click/click-8.1.7-py3-none-any.whl (97 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 47.2 kB/s eta 0:00:00 Installing collected packages: click Successfully installed click-8.1.7 (foobar) pi@raspberrypi:~ $ pip list Package Version ---------- ------- click 8.1.7 pip 23.0.1 setuptools 66.1.1 (foobar) pi@raspberrypi:~ $
Running Python will have access to the pip installed modules of the venv.
(foobar) pi@raspberrypi:~ $ python Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import click >>>
Deactivate the venv
If you ever want to "turn off" the virtual environment and return to the regular state, the deactivate
command can be used.
deactivate
This is not a Linux command. This "command" is a shell function that was defined in the activate
script when it was originally sourced. It simply undoes what the activate script did.
(foobar) pi@raspberrypi:~ $ deactivate pi@raspberrypi:~ $
Text editor powered by tinymce.