Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a brand new virtual environment or duplicate an existing one in poetry? (Multiple environment in a project)

I have a project and an existing virtual environment created with poetry (poetry install/init). So, as far as I know, the purpouse of a virtual environment is avoiding to modify the system base environment and the possibility of isolation (per project, per development, per system etc...).

How can I create another brand new environment for my project in poetry? How can I eventually duplicate and use an existing one?

I mean that the current one (activated) should be not involved in this (except for eventually copying it) because I want to test another set of dependencies and code.

I am aware of this:

  • https://github.com/python-poetry/poetry/issues/4055 (answer is not clear and ticket is not closed)
  • https://python-poetry.org/docs/managing-environments/ (use command seems not to work in the requested way)
like image 902
J_Zar Avatar asked Sep 06 '25 05:09

J_Zar


2 Answers

Poetry seems to be bound to one virtualenv per python interpreter. Poetry is also bound to the pyproject.toml file and its path to generate a new environment.

So there are 2 tricky solutions:

1 - change your deps in the pyproject.toml and use another python version (installed for example with pyenv) and then:

poetry env use X.Y

poetry will create a new virtual environment but this is not exactly the same as changing just some project deps.

2 - use another pyproject.toml from another path:

mkdir env_test
cp pyproject.toml env_test/pyproject.toml
cd env_test
nano pyproject.toml # edit your dependencies
poetry install # creates a brand new virtual environment
poetry shell
# run your script with the new environment

This will generate a new environment with just the asked dependencies changed. Both environments can be used at the same time. After the test, it is eventually possible to delete the new environment with the env command.

like image 65
J_Zar Avatar answered Sep 10 '25 13:09

J_Zar


There's another slightly hacky solution. Edit your pyproject.toml, and then run

mkdir tempenv
POETRY_VIRTUALENVS_IN_PROJECT=false POETRY_VIRTUALENVS_PATH=./tempenv poetry install

This tells Poetry to use a different virtual env path. On that path it doesn't find any existing environments so it creates a new one. To keep using the new environment you need to keep prefixing POETRY_VIRTUALENVS_IN_PROJECT=false POETRY_VIRTUALENVS_PATH=./tempenv to any poetry runs, or export them with export POETRY_VIRTUALENVS_IN_PROJECT=false POETRY_VIRTUALENVS_PATH=./tempenv. If Poetry doesn't have these variables in its shell environment it will continue using the default python environment.

NB: If you use Conda, you also need to add CONDA_PREFIX= to the shell variables, otherwise Poetry will keep using the Conda environment.

like image 29
JanKanis Avatar answered Sep 10 '25 11:09

JanKanis