jjei
jjei

Reputation: 1290

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

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.

Upvotes: 8

Views: 18927

Answers (5)

user19315471
user19315471

Reputation: 599

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.

Upvotes: 0

squarespiral
squarespiral

Reputation: 267

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.]

Upvotes: 3

Grischan Glaenzel
Grischan Glaenzel

Reputation: 39

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.

Upvotes: 0

Chris
Chris

Reputation: 136936

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.

Upvotes: 5

Laurence Rawlings
Laurence Rawlings

Reputation: 424

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

Upvotes: 0

Related Questions