How to configure virtualenvwrapper to work with pyenv

I am trying to configure my IMAC (mavericks) to be able to easily switch to different versions of python. I successfully did this for Ruby projects with rbenv and found pyenv exactly in what I was looking for in this regard. I had a problem creating virtual environments with pyenv.

I tried installing pyenv-virtualenv since I thought this would work well with pyenv, but at the moment it looks broken and it is difficult for me to get a detailed answer. At first, the activate command does not work (it says that the command does not exist, despite the docs), and as soon as I installed the pyenv-virtualenv plugin, pyenv no longer uses the correct version of python. I eventually uninstalled the plugin and pyenv started working again.

Now I would like to use the usual virtalenvwrapper with pyenv, but I continue to encounter errors that I don’t have enough to solve.

To get started, I installed pyenv and virtualenv and virtualenvwrapper according to the documentation. Then I installed python 2.7.6 with pyenv. This works fine, but when I try to configure virtualenvwrapper settings in .bash_profile, I encounter 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 the following:

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

I tried to set VIRTUALENVWRAPPER_PYTHONboth /usr/local/bin/python, and so, /usr/bin/python but none of these settings work - I keep getting the same error.

I really don't want to use /Users/insomniac/.pyenv/versions/2.7.6/bin/python it because it seems to be wrong, because the python version is listed and goes against the ability 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

.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.

, . ( ), . .

.

+4
2

, , , , /usr/local/bin/python. , , , .

virtualenvwrapper.sh , /usr/local/bin/python? , .bash_profile VIRTUALENVWRAPPER_PYTHON, , , .

, . , python , , /usr/local/bin/python, python ( link -s ).

, , pyenv, , ( ), , ~/.pyenv/shims PATH, . .bash_profile , ( source ~/.bash_profile).

: pyenv homebrew , :

To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/opt/pyenv

() .bash_profile. echo $PATH?

+2

. script ~/.bash_profile

, python pyenv (pip install virtualenvwrapper) python, .

.

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

Source: https://habr.com/ru/post/1523682/


All Articles