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:
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.
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.
If 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