问题:重命名virtualenv文件夹而不破坏它
我已经创建了文件夹并在其中初始化了virtualenv实例。
$ mkdir myproject
$ cd myproject
$ virtualenv env
当我运行时(env)$ pip freeze
,它将按原样显示已安装的软件包。
现在我想重命名myproject/
为project/
。
$ mv myproject/ project/
但是,当我跑步时
$ . env/bin/activate
(env)$ pip freeze
提示未安装pip。如何在不破坏环境的情况下重命名项目文件夹?
I’ve created folder and initialized a virtualenv instance in it.
$ mkdir myproject
$ cd myproject
$ virtualenv env
When I run (env)$ pip freeze
, it shows the installed packages as it should.
Now I want to rename myproject/
to project/
.
$ mv myproject/ project/
However, now when I run
$ . env/bin/activate
(env)$ pip freeze
it says pip is not installed. How do I rename the project folder without breaking the environment?
回答 0
您需要调整安装以使用相对路径。virtualenv
为此提供了--relocatable
选项。从文档:
通常,环境绑定到特定路径。这意味着您无法移动环境或将其复制到另一台计算机。您可以使用以下命令修复环境以使其可重定位:
$ virtualenv-可重定位的ENV
注意: ENV是虚拟环境的名称,您必须从ENV目录外部运行它。
这将使setuptools创建的某些文件或分发文件的文件使用相对路径,并将所有脚本更改为使用activate_this.py而不是使用Python解释器的位置来选择环境。
注意:在将任何软件包安装到环境中之后,必须运行此命令。如果使环境可重定位,然后安装新软件包,则必须再次运行virtualenv –relocatable。
You need to adjust your install to use relative paths. virtualenv
provides for this with the --relocatable
option. From the docs:
Normally environments are tied to a
specific path. That means that you
cannot move an environment around or
copy it to another computer. You can
fix up an environment to make it
relocatable with the command:
$ virtualenv –relocatable ENV
NOTE: ENV is the name of the virtual environment and you must run this from outside the ENV directory.
This will make some of the files
created by setuptools or distribute
use relative paths, and will change
all the scripts to use
activate_this.py instead of using the
location of the Python interpreter to
select the environment.
Note: you must run this after you’ve
installed any packages into the
environment. If you make an
environment relocatable, then install
a new package, you must run virtualenv
–relocatable again.
回答 1
我相信“知道为什么”比“知道如何”更重要。因此,这是解决此问题的另一种方法。
运行时. env/bin/activate
,它实际上执行以下命令(/tmp
例如,使用):
VIRTUAL_ENV="/tmp/myproject/env"
export VIRTUAL_ENV
但是,您刚刚将重命名myproject
为project
,因此该命令无法执行。这就是为什么这样说的原因pip is not installed
,因为您尚未安装pip
在系统全局环境中,并且您的virtualenv pip
来源不正确。
如果要手动修复此问题,请采用以下方法:
使用您喜欢的Vim编辑器,/tmp/project/env/bin/activate
通常在第42行进行修改:
VIRTUAL_ENV='/tmp/myproject/env'
=> VIRTUAL_ENV='/tmp/project/env'
/tmp/project/env/bin/pip
在第1行中进行修改:
#!/tmp/myproject/env/bin/python
=> #!/tmp/project/env/bin/python
之后,env
再次激活您的虚拟环境,您将看到自己pip
又回来了。
I believe “knowing why” matters more than “knowing how”. So, here is another approach to fix this.
When you run . env/bin/activate
, it actually executes the following commands (using /tmp
for example):
VIRTUAL_ENV="/tmp/myproject/env"
export VIRTUAL_ENV
However, you have just renamed myproject
to project
, so that command failed to execute.
That is why it says pip is not installed
, because you haven’t installed pip
in the system global environment and your virtualenv pip
is not sourced correctly.
If you want to fix this manually, this is the way:
With your favorite editor like Vim, modify /tmp/project/env/bin/activate
usually in line 42:
VIRTUAL_ENV='/tmp/myproject/env'
=> VIRTUAL_ENV='/tmp/project/env'
Modify /tmp/project/env/bin/pip
in line 1:
#!/tmp/myproject/env/bin/python
=> #!/tmp/project/env/bin/python
After that, activate your virtual environment env
again, and you will see your pip
has come back again.
回答 2
注意:作为@jb。指出,此解决方案仅适用于容易(重新)创建virtualenv
的。如果环境需要花费几个小时来安装此解决方案,则不建议
Virtualenvs很棒,因为它们易于制作和切换。它们可以防止您陷入单一配置中。如果您知道项目要求或可以得到它们,请新建一个virtualenv
:
建立requirements.txt
档案
(env)$ pip freeze > requirements.txt
- 如果您无法创建
requirements.txt
文件,请env/lib/pythonX.X/site-packages
在删除原始文件之前进行检查env
。
删除现有的 (env)
deactivate && rm -rf env
创建一个新的virtualenv
,激活它并安装需求
virtualenv env && . env/bin/activate && pip install -r requirements.txt
或者,使用virtualenvwrapper使事情变得简单一些,因为所有virtualenv都保存在集中位置
$(old-venv) pip freeze > temp-reqs.txt
$(old-venv) deactivate
$ mkvirtualenv new-venv
$(new-venv) pip install -r temp-reqs.txt
$(new-venv) rmvirtualenv old-venv
NOTE: As @jb. points out, this solution only applies to easily (re)created virtualenv
s. If an environment takes several hours to install this solution is not recommended
Virtualenvs are great because they are easy to make and switch around; they keep you from getting locked into a single configuration. If you know the project requirements, or can get them, Make a new virtualenv
:
Create a requirements.txt
file
(env)$ pip freeze > requirements.txt
- If you can’t create the
requirements.txt
file, check env/lib/pythonX.X/site-packages
before removing the original env
.
Delete the existing (env)
deactivate && rm -rf env
Create a new virtualenv
, activate it, and install requirements
virtualenv env && . env/bin/activate && pip install -r requirements.txt
Alternatively, use virtualenvwrapper to make things a little easier as all virtualenvs are kept in a centralized location
$(old-venv) pip freeze > temp-reqs.txt
$(old-venv) deactivate
$ mkvirtualenv new-venv
$(new-venv) pip install -r temp-reqs.txt
$(new-venv) rmvirtualenv old-venv
回答 3
我总是安装virtualenvwrapper来提供帮助。在shell提示下:
pip install virtualenvwrapper
在virtualenvwrapper文档中有记录的方法-cpvirtualenv,
这就是您要做的。确保您不在环境中,然后返回到Shell提示符。输入所需名称:
cpvirtualenv oldenv newenv
然后,如有必要:
rmvirtualenv oldenv
要转到您的newenv,请执行以下操作:
workon newenv
I always install virtualenvwrapper to help out. From the shell prompt:
pip install virtualenvwrapper
There is a way documented in the virtualenvwrapper documents – cpvirtualenv
This is what you do. Make sure you are out of your environment and back to the shell prompt. Type in this with the names required:
cpvirtualenv oldenv newenv
And then, if necessary:
rmvirtualenv oldenv
To go to your newenv:
workon newenv
回答 4
您可以按照以下步骤解决问题:
- 重命名目录
- 重新运行这个:
$ virtualenv ..\path\renamed_directory
- virtualenv将纠正目录关联,同时将软件包保留在原位
$ scripts/activate
$ pip freeze
确认您的包裹到位
- 一个重要的警告,如果您在virtualenv目录中的脚本文件中有任何静态路径依赖项,则必须手动更改这些依赖项。
You can fix your issue by following these steps:
- rename your directory
- rerun this:
$ virtualenv ..\path\renamed_directory
- virtualenv will correct the directory associations while leaving your packages in place
$ scripts/activate
$ pip freeze
to verify your packages are in place
- An important caveat, if you have any static path dependencies in script files in your virtualenv directory, you will have to manually change those.
回答 5
对我来说很多次都没有问题的实现方法是virtualenv-clone:
pip install virtualenv-clone
virtualenv-clone old-dir/env new-dir/env
Yet another way to do it that worked for me many times without problems is virtualenv-clone:
pip install virtualenv-clone
virtualenv-clone old-dir/env new-dir/env
回答 6
(在项目文件夹中)
cd bin
sed -i 's/old_dir_name/new_dir_name/g' *
不要忘记停用和激活
Run this inside your project folder:
cd bin
sed -i 's/old_dir_name/new_dir_name/g' *
Don’t forget to deactivate and activate.
回答 7
virtualenv --relocatable ENV
这不是理想的解决方案。我认为大多数人都希望能够重命名virtualenv 而不会产生任何长期的副作用。
因此,我创建了一个简单的工具来执行此操作。virtualenv-mv的项目页面对它进行了更详细的概述,但是从本质上讲,您可以virtualenv-mv
像使用简单的实现mv
(没有任何选项)那样使用。
例如:
virtualenv-mv myproject project
但是请注意,我只是对此进行了修改。在异常情况下(例如,符号链接的virtualenvs)它可能会损坏,因此请小心(备份无法承受的损失),如果遇到任何问题,请告诉我。
virtualenv --relocatable ENV
is not a desirable solution. I assume most people want the ability to rename a virtualenv without any long-term side effects.
So I’ve created a simple tool to do just that. The project page for virtualenv-mv outlines it in a bit more detail, but essentially you can use virtualenv-mv
just like you’d use a simple implementation of mv
(without any options).
For example:
virtualenv-mv myproject project
Please note however that I just hacked this up. It could break under unusual circumstances (e.g. symlinked virtualenvs) so please be careful (back up what you can’t afford to lose) and let me know if you encounter any problems.