python-2.7 - 如何配置 virtualenvwrapper 与 pyenv 一起使用

标签 python-2.7 virtualenvwrapper

我正在尝试设置我的 imac (mavericks),以便能够轻松切换到不同版本的 python。我使用 rbenv 成功地为 Ruby 项目完成了此操作,并发现 pyenv 正是我在这方面所寻找的。我遇到的问题是使用 pyenv 创建虚拟环境。

我尝试安装 pyenv-virtualenv,因为我认为这可以很好地与 pyenv 配合使用,但目前它似乎已损坏,我很难获得详细的回复。首先,“activate”命令不起作用(它说该命令不存在,尽管有文档),一旦我安装了 pyenv-virtualenv 插件,pyenv 就不再使用正确的 python 版本。最终我删除了插件, pyenv 再次开始工作。

现在我想将常规的 virtalenvwrapper 与 pyenv 一起使用,但不断遇到错误,我没有足够的经验来解决。

首先,我根据文档安装了 pyenv 和 virtualenv 以及 virtualenvwrapper。然后我用 pyenv 安装了 python 2.7.6。这似乎工作正常,但是当我尝试在 .bash_profile 中配置 virtualenvwrapper 设置时 - 我遇到以下错误:

-bash: /usr/local/bin/python: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks. 

If Python could not import the module virtualenvwrapper.hook_loader,  
check that virtualenv has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
set properly.

如果我检查 python 路径,我会得到:

bin [master] >> which python
/usr/bin/python
bin [master] >> pyenv which python
/Users/insomniac/.pyenv/versions/2.7.6/bin/python

我尝试将 VIRTUALENVWRAPPER_PYTHON 设置为 /usr/local/bin/python/usr/bin/python 但这些设置似乎都不起作用 - 我仍然遇到相同的错误。

我真的不想使用/Users/insomniac/.pyenv/versions/2.7.6/bin/python 因为这似乎是错误的,因为正在指定 python 版本并且不利于切换 python 版本。

这些是我的 .bash_profile 中的相关设置:

# Set  PYTHONPATH
export PYTHONPATH=/Users/insomniac/Repo/tools/python/:$PYTHONPATH

# User paths first
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:sbin:$PATH

# virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Repo
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

# Load virtualenvwrapper after paths are set
if [[ -s /usr/local/bin/virtualenvwrapper.sh ]]; then
source /usr/local/bin/virtualenvwrapper.sh
fi

# PYENV
# To use Homebrew directories rather than ~/.pyenv
#export PYENV_ROOT=/usr/local/opt/pyenv  
eval "$(pyenv init -)"

# Load .bashrc if it exits (python definitions)
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

.bashrc 文件具有以下设置:

# allow install and upgrade of global package with pip
syspip(){
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

# run this for global install or upgrade
# syspip install --upgrade pip setuptools virtualenv

# PIP
# pip should only run if there is a virtualenv currently activated
# this it to prevent accidentally installing a package globally
# use syspip() to install globally
export PIP_REQUIRE_VIRTUALENV=true

# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

我很尴尬地承认,我花了大约一周的时间试图从大量不同的帖子和教程中将其拼凑起来。我已经阅读了 pyenv、virtualenv 和 virtualenvwrapper 的文档,但我仍然不清楚如何使其与 pyenv 一起工作。

我希望我已经解释得足够好并且遵守了所有的发帖规则。如果有更好的解决方案(更容易),那么我会洗耳恭听。任何帮助将不胜感激。

提前致谢。

最佳答案

我明白了。我的 ~/.bash_profile 中有下面的脚本

唯一的问题是,使用 pyenv 安装 python 后,您需要 install
在设置新的虚拟环境之前,在新的 python 环境中(pip install virtualenvwrapper)。

那么你就可以开始了。

export PYENV_ROOT="${HOME}/.pyenv"

if [ -d "${PYENV_ROOT}" ]; then
    export PATH="${PYENV_ROOT}/bin:${PATH}"
    eval "$(pyenv init -)"
fi
export PATH

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper_lazy.sh

关于python-2.7 - 如何配置 virtualenvwrapper 与 pyenv 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21349709/

相关文章:

python-2.7 - python / Pandas : How do I convert from datetime64[ns] to datetime

java - 如何在 Spark 2.1 中使用 Java 中的 lambda flatMap()

python - 在谷歌应用引擎和 ndb 数据存储中从 python2.7 升级到 python3.7

python - 在 osx 10.11.1 上使用 pip 安装 virtualenv virtualenvwrapper

python - 在 Windows 8.1 中安装 virtualenvwrapper-powershell,导入模块失败

python - 使用 gunicorn 时未加载虚拟环境路径

python-2.7 - redis.py : How to flush all the queries in a pipeline

Python 内部结构 - 对象如何了解全局变量?

python - 在 Vagrant 配置期间更新 .bashrc 和环境变量

virtualenv - 如何在 cd 进入目录时自动激活 virtualenvs