#Guide to Python Venvs.html
>"Virtual ENVironment"
>a venv is a self contained python installation
>they help to avoid system pollution
>guarantee a state which has no dependency conflicts
>there is a builtin "venv" module
1. Creation
>the entirety of a python venv resides in an arbitrarily named folder
$ python -m venv
2. Activation/Deactivation
>"activating" a venv means to manipulate a shell's behaviour
to operate with the specific python installation which the venv provides
>activating works by redefining ${PATH}, defining functions and so on
+----------+------------+-----------------------------------------+
| Platform | Shell | Command to activate virtual environment |
+----------+------------+-----------------------------------------+
| | bash/zsh | $ source /bin/activate |
| POSIX | fish | $ source /bin/activate.fish |
| | csh/tcsh | $ source /bin/activate.csh |
| | PowerShell | $ /bin/Activate.ps1 |
| Windows | cmd.exe | C:\> \Scripts\activate.bat |
| | PowerShell | PS C:\> \Scripts\Activate.ps1 |
+----------+------------+-----------------------------------------+
>since activation is a shell operation,
the "session"s lifetime will never exceed the lifetime of the shell,
each shell requires activation to use the venv
>activation will change the prompt, this is a semi reliable way to tell
whether you are inside a virtual environment
{
$ source myvenv/bin/activate
(venv) $
}
>explicit deactivation is possible:
$ deactivate
Venvs_in_projects:
>you should
>makes installation by others infinitely less painful
-do NOT bloody try to move/share them:
"Warning: Because scripts installed in environments should not expect\
the environment to be activated, their shebang lines contain the absolute paths\
to their environmentâs interpreters. Because of this, environments are\
inherently non-portable, in the general case."
Shipping_a_venv:
1. Set it up
$ python -m venv
$ source /bin/activate
(venv) $ python -m pip install +
2. Dump it
$ (venv) python -m pip freeze > requirements.txt
3. Ship it
>add your "requirements.txt" to your version control system
>do NOT track "", and while you're at adding things to your .gitignore,
do the same with "__pycache__", damn it
Restoring_a_venv:
1. Recreate
$ python -m venv
2. Activate
$ source /bin/activate
3. Populate
(venv) $ python -m pip install -r requirements.txt