【问题标题】:Add existing virtualenv to virtualenvwrapper将现有的 virtualenv 添加到 virtualenvwrapper
【发布时间】:2017-04-05 06:00:31
【问题描述】:

在我安装 virtualenvwrapper 之前,我在不同的位置安装了一些 virtualenv。有没有办法在 virtualenvwrapper 中收集它们,以便出现在 workon 命令中?

【问题讨论】:

  • 如果一切都失败了,你总是可以使用pip freeze > requirements.txt然后创建一个新的virtualenv。

标签: python virtualenv virtualenvwrapper


【解决方案1】:

是的,这应该是可能的。 Virtualenvwrapper 允许您自定义定义您创建的环境的存储位置:

export WORKON_HOME=/path/to/your/envs

如果你从 virtualenv 指向你的虚拟环境的位置,它应该可以工作。 您应该将此行添加到您的 .bashrc 或 .zshrc 或您正在使用的任何其他 shell。 这样做的问题是您将无法激活不在该文件夹中的任何环境。

在这种情况下,只需将整个 virtualenv 复制到创建 virtualenvwrapper 环境的位置即可。

你可以找到它是这样的:

 mkvirtualenv test
 workon test
 which python
 # Will print path to virtual python interpreter:
 /path/to/virtualenvs/test/bin/python

复制所需的环境,使它们位于同一个文件夹中 作为刚刚创建的测试环境。在这里,这个文件夹将是 /path/to/virtualenvs/。从现在开始,我会称它为$VENVS。 复制后应该是/path/to/virtualenvs/my-other_env1/path/to/virtualenvs/my-other_env2。 假设您之前使用默认设置的 virtualenv 创建了 my-other_env1 和 2,复制 my-other-evn1 可以这样完成:

 cp ~/.virtualenvs/my-other-env1 $VENVS/

您可以在之后使用删除测试环境

rmvirtualenv test

(当然,如果你已经知道那个文件夹是什么,就不需要创建测试环境了。)

【讨论】:

  • 对于那些无法让which python 工作的人,使用lsvirtualenv 将列出他们在哪里以及哪些是可用的
  • 所以基本上你只需要将 复制到 $WORKON_HOME?
  • documentation 表示The variable WORKON_HOME tells virtualenvwrapper where to place your virtual environments. The default is $HOME/.virtualenvs. If the directory does not exist when virtualenvwrapper is loaded, it will be created automatically.
  • 所以 TL;DR cp existing_env_folder $WORKON_HOMEcp venvs/* $WORKON_HOME
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2018-09-03
相关资源
最近更新 更多