| 1 |
(virtualenv-heading)= |
| 2 |
|
| 3 |
# Virtualenv |
| 4 |
|
| 5 |
## Why Use Virtualenv? |
| 6 |
|
| 7 |
You should use [Virtualenv](https://virtualenv.pypa.io/en/latest/) because: |
| 8 |
|
| 9 |
- It allows you to install multiple versions of the same dependency. |
| 10 |
- If you have an operating system version of Python, it prevents you from changing its dependencies and potentially |
| 11 |
messing up your os. |
| 12 |
|
| 13 |
## How to Use Virtualenv |
| 14 |
|
| 15 |
Create your project folder, then a virtualenv within it: |
| 16 |
|
| 17 |
```console |
| 18 |
$ mkdir myproject |
| 19 |
$ cd myproject |
| 20 |
$ python3 -m venv .venv |
| 21 |
``` |
| 22 |
|
| 23 |
Now, whenever you want to work on a project, you only have to activate the corresponding environment. |
| 24 |
|
| 25 |
|
| 26 |
```{eval-rst} |
| 27 |
.. tabs:: |
| 28 |
|
| 29 |
.. group-tab:: OSX/Linux |
| 30 |
|
| 31 |
.. code-block:: text |
| 32 |
|
| 33 |
$ . .venv/bin/activate |
| 34 |
(venv) $ |
| 35 |
|
| 36 |
.. group-tab:: Windows |
| 37 |
|
| 38 |
.. code-block:: text |
| 39 |
|
| 40 |
> .venv\scripts\activate |
| 41 |
(venv) > |
| 42 |
``` |
| 43 |
|
| 44 |
You are now using your virtualenv (notice how the prompt of your shell has changed to show the active environment). |
| 45 |
|
| 46 |
To install packages in the virtual environment: |
| 47 |
|
| 48 |
```console |
| 49 |
$ pip install click |
| 50 |
``` |
| 51 |
|
| 52 |
And if you want to stop using the virtualenv, use the following command: |
| 53 |
|
| 54 |
```console |
| 55 |
$ deactivate |
| 56 |
``` |
| 57 |
|
| 58 |
After doing this, the prompt of your shell should be as familiar as before. |