My question: How does the Python tool Poetry know the path of the virtual environment of a project?
Explanation: When I run poetry init
inside a directory, a new project is created. Then I run poetry install
and a new virtual environment is created. HOWEVER, neither the path nor the hash of that virtual environment are stored in pyproject.toml
or poetry.lock
as I expected. How does Poetry then know the location of the virtual environment when I run poetry env info -p
?
Besides wanting to know what is going on, I need to know this for 2 reasons:
Possible solution: Looking into the source code of Poetry, it seemed to me that a file envs.toml
may include a mapping from filesystem directories to virtual environment hashes, but on my Mac OS 11.1 I can't find such a file.
Check the $VIRTUAL_ENV environment variable. The $VIRTUAL_ENV environment variable contains the virtual environment's directory when in an active virtual environment. Once you run deactivate / leave the virtual environment, the $VIRTUAL_ENV variable will be cleared/empty.
The easiest way to activate the virtual environment is to create a new shell with poetry shell . To deactivate the virtual environment and exit this new shell type exit . To deactivate the virtual environment without leaving the shell use deactivate .
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
Poetry is a tool that facilitates creating a Python virtual environment based on the project dependencies. You can declare the libraries your project depends on, and Poetry will install and update them for you. Project dependencies are recorded in the pyproject.
I dived deeper into the source code and I may have understood it now:
poetry/utils/env.py
inside the method EnvManager.generate_env_name(...)
pyproject.toml
and adding the hash of the parent directory of pyproject.toml
As a consequence:
poetry install
again and create a new virtual environmentIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With