Python Virtual Environment: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:


=Overview=
=Overview=
A virtual environment is a mechanism to isolate a set of installed dependencies. Virtual environments can be managed with <code>[[virtualenv#Overview|virtualenv]]</code>, <code>[[Python Module venv|venv]]</code>, etc. A virtual environment can be created manually as follows:
<syntaxhighlight lang='bash'>
python3 -m venv <virtual-env-dir-name>
python3 -m venv venv
# recommended to upgrade pip
venv/bin/python3 -m pip install --upgrade pip
venv/bin/pip install -r requirements.txt
</syntaxhighlight>
To upgrade <code>pip</code> within an already initialized virtual environment:
<syntaxhighlight lang='bash'>
venv/bin/python3 -m pip install --upgrade pip
</syntaxhighlight>
<font color=darkkhaki>The dependencies installed in a virtual environment are used automatically if the interpreter is <code>./venv/bin/python</code>.</font>
Also see [[pip#Overview|pip]] and [[pip#requirements.txt|requirements.txt]].
It is a good practice to avoid storing the content of <code>venv</code> or equivalent in source control. The content is populated locally on the developers' machines.
=Activated Virtual Environment Shell=
An "activated" virtual environment means making the virtual environment Python interpreter the default interpreter for the '''shell session'''.
To activate the virtual environment:
<syntaxhighlight lang='bash'>
source .venv/bin/activate
</syntaxhighlight>
To "deactivate", run:
<syntaxhighlight lang='bash'>
deactivate
</syntaxhighlight>
or simply exit the shell.
=Bash Wrapper that Bootstraps a Virtual Environment=
<syntaxhighlight lang='bash'>
function init_venv() {
  [[ -d $(dirname $)/venv ]] && return 0
  info "initializing venv ..."
  python3 --version 1>/dev/null 2>&1 || fail "python3 not in PATH"
  python3 -m venv "$(dirname $)/venv"
}
</syntaxhighlight>

Revision as of 17:18, 2 May 2023

External

Internal

Overview

A virtual environment is a mechanism to isolate a set of installed dependencies. Virtual environments can be managed with virtualenv, venv, etc. A virtual environment can be created manually as follows:

python3 -m venv <virtual-env-dir-name>
python3 -m venv venv
# recommended to upgrade pip
venv/bin/python3 -m pip install --upgrade pip
venv/bin/pip install -r requirements.txt

To upgrade pip within an already initialized virtual environment:

venv/bin/python3 -m pip install --upgrade pip

The dependencies installed in a virtual environment are used automatically if the interpreter is ./venv/bin/python.

Also see pip and requirements.txt.

It is a good practice to avoid storing the content of venv or equivalent in source control. The content is populated locally on the developers' machines.

Activated Virtual Environment Shell

An "activated" virtual environment means making the virtual environment Python interpreter the default interpreter for the shell session.

To activate the virtual environment:

source .venv/bin/activate

To "deactivate", run:

deactivate

or simply exit the shell.

Bash Wrapper that Bootstraps a Virtual Environment

function init_venv() {
  [[ -d $(dirname $)/venv ]] && return 0
  info "initializing venv ..."
  python3 --version 1>/dev/null 2>&1 || fail "python3 not in PATH"
  python3 -m venv "$(dirname $)/venv"
}