Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0

Python Virtual Environment Usage on Raspberry Pi

published October 24, 2023, last edited March 08, 2024
Save Link Note Download
43
Intermediate
Project guide

Basic Venv Usage

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 thevenv module and give it the name for the virtual environment.

python3 -m venv foobar
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 foobarpi@raspberrypi:~ $ lsfoobar/pi@raspberrypi:~ $ ls foobar/bin/  include/  lib/  pyvenv.cfgpi@raspberrypi:~ $
pi@raspberrypi:~ $ python3 -m venv foobarpi@raspberrypi:~ $ lsfoobar/pi@raspberrypi:~ $ ls foobar/bin/  include/  lib/  pyvenv.cfgpi@raspberrypi:~ $

Activate the venv

The main way to use the virtual environment is by "activating" it. This is done by "sourcing" theactivate script found in the virtual environmentsbin folder.

source foobar/bin/activate
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:~ $
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. Runningpython orpip 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 listPackage    Version---------- -------pip        23.0.1setuptools 66.1.1(foobar) pi@raspberrypi:~ $ pip install clickLooking in indexes: https://pypi.org/simple, https://www.piwheels.org/simpleCollecting 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:00Installing collected packages: clickSuccessfully installed click-8.1.7(foobar) pi@raspberrypi:~ $ pip listPackage    Version---------- -------click      8.1.7pip        23.0.1setuptools 66.1.1(foobar) pi@raspberrypi:~ $
(foobar) pi@raspberrypi:~ $ pip listPackage    Version---------- -------pip        23.0.1setuptools 66.1.1(foobar) pi@raspberrypi:~ $ pip install clickLooking in indexes: https://pypi.org/simple, https://www.piwheels.org/simpleCollecting 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:00Installing collected packages: clickSuccessfully installed click-8.1.7(foobar) pi@raspberrypi:~ $ pip listPackage    Version---------- -------click      8.1.7pip        23.0.1setuptools 66.1.1(foobar) pi@raspberrypi:~ $

Running Python will have access to the pip installed modules of the venv.

(foobar) pi@raspberrypi:~ $ pythonPython 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import click>>>
(foobar) pi@raspberrypi:~ $ pythonPython 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linuxType "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, thedeactivate command can be used.

deactivate
deactivate

This isnot a Linux command. This "command" is a shell function that was defined in theactivate script when it was originally sourced. It simply undoes what the activate script did.

(foobar) pi@raspberrypi:~ $ deactivate pi@raspberrypi:~ $
(foobar) pi@raspberrypi:~ $ deactivate pi@raspberrypi:~ $

Page last edited March 08, 2024

Text editor powered bytinymce.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp