如何配置 virtualenvwrapper 以使用 pyenv [英] how to configure virtualenvwrapper to work with pyenv

查看:58
本文介绍了如何配置 virtualenvwrapper 以使用 pyenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to setup my imac (mavericks) to be able to easily switch to different versions of python. I successfully have done this for Ruby projects with rbenv and found pyenv to be exactly what I was looking for in that regard. The problem I'm having is creating virtual environments with pyenv.

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

I tried installing pyenv-virtualenv, since I thought that would work well with pyenv, but it appears broken at the moment and I'm having a hard time getting a detailed response. First the 'activate' command does not work (it says the command does not exist, despite the docs), and once I installed the pyenv-virtualenv plugin, pyenv no longer uses the correct python version. Ultimately I removed the plugin and pyenv started working again.

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

Now I'd like to use the regular virtalenvwrapper with pyenv, but keep running into errors which I do not have enough experience to resolve.

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

To start, I installed pyenv and virtualenv and virtualenvwrapper according to the documentation. I then installed python 2.7.6 with pyenv. That seems to work fine, but when I try to configure the virtualenvwrapper settings in .bash_profile - I run into the following error:

-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 路径,我会得到这个:

If I check the python paths I get this:

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但这些设置似乎都不起作用 - 我继续遇到同样的错误.

I tried setting VIRTUALENVWRAPPER_PYTHON to both /usr/local/bin/python and /usr/bin/python but neither of those setting seem to work - I continue to get the same error.

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

I don't really want to use /Users/insomniac/.pyenv/versions/2.7.6/bin/python since that seems wrong because the python version is being specified and goes against being able to switch python versions.

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

These are the related settings in my .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 文件具有以下设置:

The .bashrc file has these settings:

# 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 一起工作.

I'm embarrassed to admit that I've spent about a week on this trying to piece it together from a ton of different posts and tutorials. I've read the docs for pyenv, virtualenv and virtualenvwrapper and it's still not clear to me on how to make it work with pyenv.

我希望我已经解释得足够好,并遵守了所有的发帖规则.如果有更好的解决方案(更容易),那么我全神贯注.任何帮助将不胜感激.

I hope I have explained this well enough and have followed all the posting rules. If there is a better solution (that's easier) then I'm all ears. Any help would be greatly appreciated.

提前致谢.

推荐答案

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

I got it figured out. I have the script below in my ~/.bash_profile

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

The only issue is that after you install python with pyenv, you'll need install
(pip install virtualenvwrapper) in the new python environment, before setting up your new virtual environment.

那么你就可以开始了.

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

这篇关于如何配置 virtualenvwrapper 以使用 pyenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆