Virtual enviroments
Main packages to handle virtual environments
venv (Standard Library)
venv
already comes with the Python Standard Library so no installation is required.
Create a new virtual enviroment:
To create a new virtual environment:
A new folder appears in your current location containinng the new Python installation. The environemnt uses the same Python version as the one used to create it.To activate the virtual environment:
$ env_name/Scripts/activate.bat # Windows command line
$ env_name/Scripts/activate # Windows command line or Powershell
$ env_name/Scripts/Activate.ps1 # Windows Powershell
$ source env_name/bin/activate # Unix system
pip install package
, it will only be installed in the environment.
To deactivate the virtual environment:
You can check installed packages in the new environment by running the command below. For a new/clean environment you should get the following output.
To create a requirements file for your project:
To delete the virtual environment completly just delete the enviroment folder (env_name
) or run:
To install packages from an existing requirements.txt
file:
uv
To create a virtual environment in a given directory:
If no directory name is provided, the venv will be created in the directory.venv
.
To activate the virtual environment run:
Installing packages from PyPI:
Installing from a git repository:
To list what’s installed in a given venv
You can direct the results to a file like: The list will have explicit version requirements for each package, meaning it will be “locked” to the specific versions.If you want to take an existing pyproject.toml
or requirements.in
file and generate a locked dependency set as requirement.txt
, use:
$ uv pip compile pyproject.toml -o requirements.txt
# or
$ uv pip compile requirements.in -o requirements.txt
To bring a project’s installed dependencies in sync with a list of locked dependencies use:
Pipenv
Installing Pipenv: $ pip install pipenv
To upgrade Pipenv at any time run: $ pip install --upgrade pipenv
Installing packages for your project
Pipenv will install the excellent Requests library and create a Pipfile for you in your project’s directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them, such as when you share your project with others.To install as a dev dependency run:
To remove an installed package run:
Using installed packages
Import your packages normally in your scripts. Then you can run the script using pipenv run
:
Using $ pipenv run
ensures that your installed packages are available to your script.
It's also possible to spawn a new shell that ensures all commands have access to your installed packages with $ pipenv shell
.
Other commands:
pipenv --rm
: Removes the virtual environment completlypipenv --python 3.6.5
: Specify Python versionpipenv --venv
: Shows virtual environment pathpipenv graph
: Dependencies treepipenv install --dev -e .
: Install as editable
Set enviromental variables
Create a .env
file at the root folder of the project, next to the Pipfile
. Add enviromental variables like so:
You can add comments in this file with a leading #
. This file will be loaded automatically with pipenv shell
or pipenv run your_command
and the environment variables will be available.
You can access/check them in your code with:
Note: Remember to exclude this file from your version control.
References
virtualenv package
Install: $ pip install virtualenv
Create a new virtual enviroment:
My prefered method is to create the environment in the respective project folder and naming it venv. Another alternative is to have a folder where you place all your virtualenvironments and name them according to their respective project.
To activate the environment:
(Windows) $ venv/Scripts/activate.bat
(Linux) $ source venv/bin/activate
Once activated you can normaly use pip to install packages in this environment: $ pip install blabla
You can see the list of installed packages using: $ pip list
To transfer the environment to another machine you can use: $ pip freeze > requirements.txt
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. Later, if you need to re-create the environment, install the same packages using the same versions: $ pip install -r requirements.txt
To deactivate the environment:
(Windows) $ venv/Scripts/deactivate.bat
(Linux) $ deactivate
Poetry
Poetry not only handles dependenct management through virtual environments but also has several other features like pachaging.
Install
The recommended way to install Poetry on Windows is via Powershell:
You only need to install Poetry once. It will automatically pick up the current Python version and use it to create virtualenvs accordingly.To compleatly uninstall Poetry run:
or
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py - --uninstall
Usage
To create a new project run named poetry-demo
:
poetry-demo
directory with the following content:
poetry-demo
├── pyproject.toml
├── README.rst
├── poetry_demo
│ └── __init__.py
└── tests
├── __init__.py
└── test_poetry_demo.py
To add or modify dependencies you can edit the pyproject.toml
file. Alternatively you can add a dependency by running:
{cache-dir}\virtualenvs
on Windows.
To run your script user:
To activate the virtual environment create a poetry shell with:
To deactivate the virtual environment and exit this new shell type exit
To install the defined dependencies for your project:
To list all of the available packages:
To create a pip
style requirements.txt
file (including dev dependencies)