问题
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.
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.
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.
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.
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
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.
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.
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
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
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.
Thanks in advance.
回答1:
I got it figured out. I have the script below in my ~/.bash_profile
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.
Then you are good to go.
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
回答2:
Well the error you are getting means that there is simply no file /usr/local/bin/python
in your file system. It has less to do with the settings you are adjusting but with the expectations of the scripts you are running.
Can you check the contents of virtualenvwrapper.sh
and see if it contains explicit references to /usr/local/bin/python
? At the first glance, your .bash_profile
has VIRTUALENVWRAPPER_PYTHON
pointing to that file, and since that file does not exist - you have an error message.
I am not sure how virtualenv wrapper is supposed to work. But to fix the error you need to understand which python instance you want your scripts to use, and either point them to that, or create a symlink /usr/local/bin/python
pointing to the right python instance (using link -s
command in terminal).
On the other hand, it seems like pyenv may have not been installed properly (or completely) - as their documentation says that it works by inserting ~/.pyenv/shims
in front of your PATH
variable which you do not have here. You could manually add that in .bash_profile
and see if that helps (reload the profile after updating source ~/.bash_profile
).
Update: I tried installing pyenv
using homebrew and in the end saw the message it printed about setting it up:
To enable shims and autocompletion add to your profile:
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
To use Homebrew's directories rather than ~/.pyenv add to your profile:
export PYENV_ROOT=/usr/local/opt/pyenv
Which you have (kind of) in your .bash_profile
. What does echo $PATH
show?
回答3:
I haven't installed virtualenvwrapper via apt or pip at all. I have installed pyenv and its virtualenvwrapper plugin:
git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper
In .bashrc I have:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
pyenv virtualenvwrapper
When I install some python and make it global:
pyenv update # to have access to latest python versions
pyenv install 3.9.1
pyenv global 3.9.1
then with modern versions of pyenv and the plugin, when I use any of commands
workon
mkvirtualenv
the missing virtualenvwrapper for the current global python version will be automatically installed and everything works well. Ie. if you change the global python version, virtualenvwrapper installs again for the new version.
来源:https://stackoverflow.com/questions/21349709/how-to-configure-virtualenvwrapper-to-work-with-pyenv