Difference between revisions of "Set up a Python virtual environment"

From Parallel Library Services
Jump to navigation Jump to search
(Created page with "Python virtual environments run their own site directories, allowing them to be optionally separated from system directories. This can easily avoid a potential nightmare of ve...")
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Python virtual environments run their own site directories, allowing them to be optionally separated from system directories. This can easily avoid a potential nightmare of version conflicts - one piece of software on your system may require a certain version to run, while another make require a different one.
Python virtual environments run their own site directories, allowing them to be optionally isolated from system directories. This can easily avoid a potential nightmare of version conflicts - one piece of software on your system may require a certain version to run, while another make require a different one.


== Creating the virtual environment ==
== Create ==
<code>venv</code> is a tool that can be used to create an environment:
If you are using Python 3, you can use <code>venv</code>, which is part of its standard library.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 8: Line 8:
</syntaxhighlight>
</syntaxhighlight>


To activate the virtual environment, run:
When this is run, <code>venv</code> creates the target directory (in this case, named <code>venv</code>), and any parent directories that don't exist yet. It also puts a <code>pyvenv.cfg</code> file with a <code>home</code> key that points to the installation of Python from where the command was run.


== Activate ==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
source venv/bin/activate
source venv/bin/activate
</syntaxhighlight>
</syntaxhighlight>


You will see the prompt change to something like this:
You will see the prompt change to display <code>(venv)</code> at the beginning of the line:
<syntaxhighlight lang="bash" line>
<syntaxhighlight lang="bash">
(venv)Username@Computer myfolder %
(venv) Username@Computer myfolder %
</syntaxhighlight>
 
== Use ==
Install dependencies within the new Python virtual environment, using <code>pip</code>, the default package manager for Unix-like systems such as Mac OS and Linux. Swap out the {packagename} for the package you want to install.
 
<syntaxhighlight lang="bash">
pip install {packagename}
</syntaxhighlight>
 
== Deactivate ==
To stop it, type:
 
<syntaxhighlight lang="bash">
deactivate
</syntaxhighlight>
</syntaxhighlight>


To close it, simply type
== Close ==
Simply type


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
exit
exit
</syntaxhighlight>
</syntaxhighlight>
== Remove ==
To remove a Python virtual environment, first deactivate it. Then you can use <code>rm</code> to remove it.
<syntaxhighlight lang="bash">
rm -r venv
</syntaxhighlight>
A more thorough method is to specify removal of the files and folders made when the environment was created. In the folder where the venv is, run
<syntaxhighlight lang="bash">
rm -r bin include lib lib64 pyvenv.cfg share
</syntaxhighlight>
[[Category:Cookbook]]
[[Category:Python]]

Latest revision as of 20:56, 4 November 2021

Python virtual environments run their own site directories, allowing them to be optionally isolated from system directories. This can easily avoid a potential nightmare of version conflicts - one piece of software on your system may require a certain version to run, while another make require a different one.

Create

If you are using Python 3, you can use venv, which is part of its standard library.

python3 -m venv venv

When this is run, venv creates the target directory (in this case, named venv), and any parent directories that don't exist yet. It also puts a pyvenv.cfg file with a home key that points to the installation of Python from where the command was run.

Activate

source venv/bin/activate

You will see the prompt change to display (venv) at the beginning of the line:

(venv) Username@Computer myfolder %

Use

Install dependencies within the new Python virtual environment, using pip, the default package manager for Unix-like systems such as Mac OS and Linux. Swap out the {packagename} for the package you want to install.

pip install {packagename}

Deactivate

To stop it, type:

deactivate

Close

Simply type

exit

Remove

To remove a Python virtual environment, first deactivate it. Then you can use rm to remove it.

rm -r venv

A more thorough method is to specify removal of the files and folders made when the environment was created. In the folder where the venv is, run

rm -r bin include lib lib64 pyvenv.cfg share