... | @@ -16,7 +16,12 @@ A python virtual environment is a small, easily reproducible and erasable space |
... | @@ -16,7 +16,12 @@ A python virtual environment is a small, easily reproducible and erasable space |
|
|
|
|
|
## Tools
|
|
## Tools
|
|
|
|
|
|
There are a number of tools available, and it's by using them that you can make up your own mind. I use `venv` because it's simple, lightweight and easy to use.
|
|
There are a number of tools available, and it's by using them that you can make up your own mind. In this tutorial we will use `pipenv`, based on `venv`, because it's simple, lightweight and easy to use.
|
|
|
|
|
|
|
|
### Install Pipenv today!
|
|
|
|
```bash
|
|
|
|
pip install --user pipenv
|
|
|
|
```
|
|
|
|
|
|
Here's a list of tools you can use to create your own environments:
|
|
Here's a list of tools you can use to create your own environments:
|
|
|
|
|
... | @@ -34,15 +39,15 @@ Here's a list of tools you can use to create your own environments: |
... | @@ -34,15 +39,15 @@ Here's a list of tools you can use to create your own environments: |
|
|
|
|
|
- [pipenv](https://pipenv.pypa.io/en/latest/)
|
|
- [pipenv](https://pipenv.pypa.io/en/latest/)
|
|
|
|
|
|
This tool combines `pip` and `virtualenv` into one. You no longer need to use them separately. It manages environment per project, it creates a Pipfile(.lock) inside your project.
|
|
This tool combines `pip` and `virtualenv` into one. You no longer need to use them separately. It manages environment per project, it creates a `Pipfile(.lock)` inside your project, maintaining your dependances.
|
|
|
|
|
|
- [mamba](https://mamba.readthedocs.io/en/latest/index.html)
|
|
- [mamba](https://mamba.readthedocs.io/en/latest/index.html)
|
|
|
|
|
|
This tool allows you to manage multiple environments using the packages on PyPI and the various `conda` ***channels***.
|
|
This tool allows you to manage multiple environments using the packages on `PyPI` and the various `conda` ***channels***.
|
|
|
|
|
|
- [poetry](https://python-poetry.org/)
|
|
- [poetry](https://python-poetry.org/)
|
|
|
|
|
|
This tool manages environments and dependencies in a similar way to `pipenv`, but it can also build distributions, and it can upload it to PyPI. It uses the `pyproject.toml` standard, but it does not follow the standard specifying how metadata should be represented in a `pyproject.toml` file.
|
|
This tool manages environments and dependencies in a similar way to `pipenv`, but it can also build distributions, and it can upload it to `PyPI`. It uses the `pyproject.toml` standard, but it does not follow the standard specifying how metadata should be represented in a `pyproject.toml` file.
|
|
|
|
|
|
- [hatch](https://hatch.pypa.io/latest/)
|
|
- [hatch](https://hatch.pypa.io/latest/)
|
|
|
|
|
... | @@ -81,6 +86,20 @@ jupyterlab>4.0,<4.1; platform_system == "Linux" |
... | @@ -81,6 +86,20 @@ jupyterlab>4.0,<4.1; platform_system == "Linux" |
|
requests [security] >= 2.8.1, == 2.8.* ; python_version < "2.7"
|
|
requests [security] >= 2.8.1, == 2.8.* ; python_version < "2.7"
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### :tools: Exercise
|
|
|
|
|
|
|
|
Create your `requirements.txt`and start your `pipenv`environment:
|
|
|
|
```bash
|
|
|
|
nano requirements.txt
|
|
|
|
pipenv install -r requirements.txt
|
|
|
|
```
|
|
|
|
|
|
|
|
Look at `Pipfile`and `Pifile.lock`.
|
|
|
|
|
|
|
|
To reset your environment: `pipenv --rm`, it doesn't detsroy the `Pipfile(.lock)`, so you can recreate the environment with: `pip install`.
|
|
|
|
|
|
|
|
To check in which folder are the dependances placed: `pipenv --venv`.
|
|
|
|
|
|
### `environment.yml` example
|
|
### `environment.yml` example
|
|
|
|
|
|
See more information on requirements specifiers on https://pip.pypa.io/en/stable/reference/requirement-specifiers/
|
|
See more information on requirements specifiers on https://pip.pypa.io/en/stable/reference/requirement-specifiers/
|
... | @@ -113,6 +132,15 @@ dependencies: |
... | @@ -113,6 +132,15 @@ dependencies: |
|
|
|
|
|
Before installing your dependencies, you need to create and activate your environment.
|
|
Before installing your dependencies, you need to create and activate your environment.
|
|
|
|
|
|
|
|
- With `pipenv`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
pipenv install
|
|
|
|
pipenv shell
|
|
|
|
```
|
|
|
|
|
|
|
|
And with some other environment management tools:
|
|
|
|
|
|
- With `venv` (Linux ou Mac)
|
|
- With `venv` (Linux ou Mac)
|
|
|
|
|
|
```bash
|
|
```bash
|
... | @@ -127,19 +155,13 @@ Before installing your dependencies, you need to create and activate your enviro |
... | @@ -127,19 +155,13 @@ Before installing your dependencies, you need to create and activate your enviro |
|
my_env\Scripts\activate
|
|
my_env\Scripts\activate
|
|
```
|
|
```
|
|
|
|
|
|
- With `virtualenv` (Linux ou Mac)
|
|
- With `virtualenv`
|
|
|
|
|
|
```bash
|
|
```bash
|
|
virtualenv my_env
|
|
virtualenv my_env
|
|
source my_env/bin/activate
|
|
source my_env/bin/activate
|
|
```
|
|
```
|
|
|
|
|
|
- With `virtualenv` (Windows)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
virtualenv my_env
|
|
|
|
my_env\Scripts\activate
|
|
|
|
```
|
|
|
|
|
|
|
|
- With `mamba`
|
|
- With `mamba`
|
|
|
|
|
... | @@ -187,16 +209,24 @@ Before installing your dependencies, you need to create and activate your enviro |
... | @@ -187,16 +209,24 @@ Before installing your dependencies, you need to create and activate your enviro |
|
```
|
|
```
|
|
|
|
|
|
>>>
|
|
>>>
|
|
Exercise:
|
|
**:tools: Exercise**
|
|
|
|
|
|
Install the dependencies for this workshop using either `venv` or `virtualenv`.
|
|
Install the dependencies for this workshop using `pipenv`.
|
|
|
|
|
|
The `requirements.txt` file is located in the `practical_session/configuration` directory.
|
|
The `requirements.txt` file is located in the `practical_session/configuration` directory.
|
|
|
|
|
|
|
|
If you did the previous exercices with `pipenv`, you need to clean up your environment:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
pipenv --rm
|
|
|
|
rm Pipfile*
|
|
|
|
cp practical_session/configuration/requirements.txt requirements.txt
|
|
|
|
pipenv install -r requirements.txt
|
|
>>>
|
|
>>>
|
|
|
|
|
|
## Deactivate the active environment
|
|
## Deactivate the active environment
|
|
|
|
|
|
To deactivate it you need to run `deactivate` in your terminal.
|
|
To deactivate it you need to run `deactivate` in your terminal:
|
|
|
|
|
|
```bash
|
|
```bash
|
|
deactivate
|
|
deactivate
|
... | | ... | |