简体   繁体   English

更改 virtualenv 文件夹名称后无法执行 pip

[英]Cannot execute pip after changing virtualenv folder name

So previously I named my virtual environment "test".所以之前我将我的虚拟环境命名为“测试”。 After that, I changed it to "testt", and after that I can't access pip commands anymore and it gives me the following error:之后,我将其更改为“testt”,之后我无法再访问 pip 命令,它给了我以下错误:

Fatal error in launcher: Unable to create process using '"C:\coding\test\test\Scripts\python.exe"  "C:\coding\test\testt\Scripts\pip.exe" ': The system cannot find the file specified.

How can I fix this?我怎样才能解决这个问题?

You should never rename a virtual Python environment.您永远不应该重命名虚拟 Python 环境。 When creating the virtual environment, the path is hardcoded in several places (see the Scripts/activate* scripts for example).创建虚拟环境时,路径在多个位置进行了硬编码(例如,请参见Scripts/activate*脚本)。

You could try to replace the hardcoded paths in all files, but I'm not sure how good this works and if this changes between (Python/venv) versions.可以尝试替换所有文件中的硬编码路径,但我不确定这有多好用,以及这是否在(Python/venv)版本之间发生变化。

Best thing to do is just remove the old virtual environment and create a new one.最好的办法是删除旧的虚拟环境并创建一个新环境。

If you're using a requirements.txt file, this as as easy as:如果您使用requirements.txt文件,这很简单:

  1. py -3.10 -m venv new_env
  2. new_env\Scripts\python.exe -m pip install -r requirements.txt

(example commands are for Windows and Python 3.10, but are similiar for Linux and/or other Python versions) (示例命令适用于 Windows 和 Python 3.10,但适用于 Linux 和/或其他 Python 版本)

If you didn't use a requirements.txt file, you could run pip freeze before removing the old virtual environment to see which modules you had installed.如果你没有使用requirements.txt文件,你可以在删除旧的虚拟环境之前运行pip freeze来查看你安装了哪些模块。

Side note: this method has the advantage that you're actually verifying that your environment is well documented (ie, the requirements.txt is up-to-date) and that you can reproduce it.旁注:此方法的优点是您实际上是在验证您的环境是否记录良好(即, requirements.txt是最新的)并且您可以复制它。 This will make it easier to repeat this process in the future (eg on another computer) as well.这也将使将来(例如在另一台计算机上)重复此过程变得更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM