Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure virtualenvwrapper to work with 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.

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.

like image 540
Carolyn Avatar asked Jan 25 '14 11:01

Carolyn


People also ask

What is Pyenv virtualenvwrapper?

pyenv-virtualenvwrapper is a pyenv plugin which provides a pyenv virtualenvwrapper command to manage your virtualenvs with virtualenvwrapper. Attention: This plugin is different from pyenv-virtualenv, which provides extended commands like pyenv virtualenv 3.4.

What is the difference between virtualenv and virtualenvwrapper?

Virtualenvwrapper is a utility on top of virtualenv that adds a bunch of utilities that allow the environment folders to be created at a single place, instead of spreading around everywhere.


2 Answers

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
like image 173
CaptRespect Avatar answered Oct 29 '22 17:10

CaptRespect


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?

like image 40
alexandroid Avatar answered Oct 29 '22 15:10

alexandroid