Managing Virtual Environments in Poetry
Posted on Dec 25, 2019 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
uv (implemented in Rust) is the new cool kid now and is prefer to poetry.
Where to Create Virtual Environments
By default,
Poetry create virtual environment in $HOME/.poetry
for cahcing/sharing purpose.
However,
it is inconvenient if you use Docker-based web IDE for a Python project
as you will lose the created virtual environment once the Docker container is restarted.
In that situation,
you have to create a new virtual environment,
set up the Python interpreter again,
which is tedious.
Fortunately,
Poetry allows to create virtual environment in th root directory of a Python project,
and it is recommended to do so for the situation described above.
Run the following command to configure Poetry to create virtual environment in the root directory of the current project.
poetry config --local virtualenvs.in-project true
Notice that the above configure works for the current project only. Use the following command if you want to make Poetry always create virtual environment in the root directory of a project.
poetry config virtualenvs.in-project true
Create a Virtual Environment
Poetry supports the env
subcomamnd starting from version 1.0.0.
You can use poetry env use python_version
to specify the Python version to use for the project.
poetry env use python3
Activate Vitual Environment
The virtual environment shell can be activated using the following command.
poetry shell
If you have the virtual environment created in the directory .venv
under the root directory of the project,
you can also use the following command to activate the virtual environment.
. .venv/bin/activate
Show Information of the Vitual Environment
You can list information of virtual environments using the following command.
poetry env info
References
https://python-poetry.org/docs/managing-environments/
https://github.com/python-poetry/poetry/issues/108