Skip to main content
Chemistry LibreTexts

3: Virtual Environments (venv)

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Creating venv

    With the release of Debian 12 (Bookworm) third party packages can no longer be installed in the system level python installation and must be installed in a virtual environment. This tutorial will go over how to do this.  First you need to be sure the venv module is installed.  Run the following code and it will let you know if it is already installed, or give you the option to install it.

    sudo apt install python3-venv

    Now create a folder for your virtual environment, we will call it my_venv for my virtual environment (you can call it anything you want, and many programs call the folder venv, as that helps you recognize what it is).  Note, this may take a few seconds.  Be sure you are in the directory you want to be in when you do this.

    python3 -m venv my_venv

    If you want to see what is in it type

    ls my_venv


    and you will see the pyvenv.cfg file along with several folders.

    To activate use the following code

    source my_venv/bin/activate


    You will not that (my_venv) is now in front of the prompt

    Now you can install packages in your virtual environment.  for example, if you want to install gpio zero type


    pip install gpiozero


    To deactivate the virtual environment simple type deactivate.  Note, that is not a linux command, but a shell function.



    Note, if you want to reactivate the virtual environment from your home directory you need to give the source-activate command a path to the bin folder


    now you see (my_venv) in front of the prompt


    Thonny and venv

    You now need to set up Thonny to use the python interpreter in the virtual environment

    Go to Tools/Options/interpreter.  If the virtual environment does not show in the dropdown box, click the three dots to the right of the dropdown arrow

    clipboard_ee31f911ee3bdb62ba5ebfd7acf4f9746.pngFigure \(\PageIndex{1}\): If you do not see your venv click the three dots to the left of the dropdown arrow. (Belford cc0.0)

    Now  navigate to the bin directory of your venv and pick the python interpreter you want to run

    clipboard_e79d0d30d0333fb16a7b865faf240140e.pngFigure \(\PageIndex{2}\): Pick the version of python you want to run. (Copyright; author via source)




    3: Virtual Environments (venv) is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?