Poetry: How to keep using the old virtual environment when changing the name of the project root folder?
Asked Answered
P

5

7

I am using Poetry in some of my python projects. It's not unusual that at some stage I want to rename the root folder of my project. When I do that and run poetry shell poetry creates a new virtual environment. But I don't want a new virtual environment, I just want to keep using the existing virtual environment. I know I can manually activate the old one by running source {path to the old venv}/bin/activate but then I would have to keep track of the old environment name separately and refrain from using poetry shell.

Is there something I can do about this? It's quite time consuming to start installing the dependencies again, pointing an IDE to the new environment and deleting the old virtual env, just because you have changed the root folder name - something that can happen multiple times. This question suggests that there is no solution to the problem but would want to confirm this because to me this seems quite annoying issue.

Pocketful answered 22/1, 2022 at 21:3 Comment(0)
E
4

One option is to enable the virtualenvs.in-project option, e.g. by running

poetry config virtualenvs.in-project true

If set to true, the virtualenv wil [sic] be created and expected in a folder named .venv within the root directory of the project.

This will cause Poetry to create new environments in $project_root/.venv/. If you rename the project directory the environment should continue to work.

Emelda answered 22/1, 2022 at 21:11 Comment(2)
This should solve the problem, with the side effect that now the virtual env would have to be in the same directory. Not an optimal solution in that sense but I think I could live with that or then give a try to pdm.fming.dev. Anyway, I will wait a bit to see if there is a solution that works without forcing the venv in the same directory. If not, will mark this as a correct solution.Pocketful
This does work, but limits you to a single virtual environment per project, which is a problem if you want to make sure code works with different python versions.Convivial
L
1

Annoying, yes. There must be some kind of mapping from the project dir to the environment name. The environment name is the project directory name plus a 8-digit hash. However I could not find that hash in any project file to change it, so I figured it must be generated on the fly somehow.

And indeed, after poking around in the poetry source code a bit, I came up with the following script, that produces the exact hash that poetry uses to name the virtual environments (excuse the path format, currently on a win10 box):

import hashlib
import os
import base64

path = r'C:\Users\YourUser\YourProject\YourProjectFolderName'

normalized_cwd = os.path.normcase(os.path.realpath(path))
print(f'normalized path: { normalized_cwd }')

h_bytes = hashlib.sha256(normalized_cwd.encode('utf-8')).digest()
h_str = base64.urlsafe_b64encode(h_bytes).decode()[:8]
print(f'hash: {h_str}')

Now, as far as I can see, the hash only shows up in [installation_path]\pypoetry\Cache\virtualenvs\envs.toml. So running the script above, renaming the actual environment dir from pypoetry\Cache\virtualenvs\myproject-[oldhash]-py3.10 to pypoetry\Cache\virtualenvs\myproject-[newhash]-py3.10 and likewise updating the toml file with the new hash should do the trick. And indeed in a limited test, I was able to successfully migrate an environment.

So in summary:

  1. run the above python script with your project path
  2. change hash part of the environment directory to the new hash (in pypoetry/Cache/virtualenvs)
  3. change hash from old to new in pypoetry/Cache/virtualenvs\envs.toml

So, easy enough, if you know where to look.

[You need to adjust paths accordingly for this to work - I was testing on a win10 box with a limited setup, so your paths might differ.]

Lorin answered 16/8, 2023 at 11:26 Comment(0)
Y
0

I believe you can manually specify the virtual environment that poetry uses. Before changing the root folder name get the path of the environment:

poetry env info --path

Then update the folder name and manually set the virtual environment for the project:

poetry env use /full/path/to/python

See the docs on managing environments for more info:

https://python-poetry.org/docs/managing-environments

Ynes answered 22/1, 2022 at 21:10 Comment(1)
This doesn't seem to work reliably. I played around creating a new environment, installing a single library, changing the root folder name, a couple of times. Even when explicitly using env use poetry still creates a new virtual env. Based on docs I think the env use command is for having multiple envs with different python versions but doesn't seem to fit the use case in the question.Pocketful
S
0

As far as i understand if you want to change the path permanently you have to use:

poetry config virtualenvs.path /your/path/to/envs

make sure that the folder is at least a copy of an valid poetry virtualenvs folder.

Scherman answered 6/12, 2022 at 18:51 Comment(1)
This will change the root directory for all virtual environments installed by poetry, rather than pointing the exiting project, in the new directory, back to the pre-existing environment.Convivial
S
0

Poetry will create a venv with a new name.
Copy the new name, delete new venv, then rename old venv to the new name.
Both old and new should be on the same path.

Symbolism answered 25/6 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.