3

I was looking for a version manager for Python similar/equal to RVM (for Ruby), I found pyenv but it is merely a switcher so I'd need to combine it with virtualenv (my understanding so far), a little laborious but I could get used to it. Moreover I've read pipenv is recommended to use instead of virtualenv, so could it work with pyenv? how?

However the article What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? mentionates pyenv is deprecated in Python 3.6. So I'm in zero and confused again, what should I use? and how should I use it?

System: Arch Linux, Plasma.

Current (installed) versions for Python: 2.7.14 and 3.6.4

What I plan to do: I need a framework where if for any reason I need to work with versions: 3.4.7, 3.2.6, 2.6.7 and 2.3.3, I could do it without any pain in the ass.

I hope your help, thanks people.

4 Answers4

3

you have two options

  1. use pyenv and pyenv-virtualenv wrapper together.

for example, you want to create a new project test and also want to create a virtual env for it.

  • pyenv install 3.6.5
  • pyenv virtualenv 3.6.5 test
  • cd /project_path
  • pyenv local test

next time, you access the project dir, it will change to test environment automatically

  1. use pyenv and pipenv together

firstly, add this script to the env config(bashrc or zshenv, etc.)

export PIPENV_VENV_IN_PROJECT=1
PROMPT_COMMAND='prompt'
precmd() { eval "$PROMPT_COMMAND" }
function prompt()
{
    if [ ! $PIPENV_ACTIVE ]; then
      if [ `pipenv --venv 2>/dev/null` ]; then
        export PIPENV_INITPWD="$PWD"
        pipenv shell
      fi
    elif [ $PIPENV_INITPWD ] ; then
      cd "$PIPENV_INITPWD"
      unset PIPENV_INITPWD
    fi
}

then

  • pyenv install 3.6.5
  • pyenv shell 3.6.5
  • pip install pipenv
  • cd /project_path
  • pipenv --python 3.6.5

next time you access the directory, it will change to the correct vent(note: you should use pyenv shell 3.6.5 before you access the project dir)

dameng
  • 508
  • 7
  • 12
0

If I recall correctly, virtualenv is more for making sure your application isn't interfering with others on the same running system. If you're running a UNIX based OS (Linux/FreeBSD/macOS) you could compile python (with configure --prefix=/opt/python/) and alias the binary executable in your .bashrc or bash_profile.

alias python363='/opt/python363/bin/python3'

or if you want to make it system wide and have access to root you can make a symlink ln -s /opt/python363/bin/python3 /usr/bin/python363 (for version 3.6.3 for example).

then execute the file with python363 /path/to/your/script.py

Is there a better way to do it? Possibly, but it is one alternative to your predicament albeit not the most elegant.

  • NOTE: You'll also have to alias the pip command installed with that particular version in order to install libraries to the correct verison

i.e. in .bashrc or bash_profile

alias pip363='/opt/python363/bin/pip3'

or as root with a symlink

 ln -s /opt/python363/bin/pip3 /usr/bin/pip363
Victisomega
  • 1
  • 1
  • 2
0

pyenv supports virtualenvs

Just install the python versions you want, for example:

pyenv install 3.6.4
pyenv install 3.6.0

Select which python version you want with the argument global

pyenv global 3.6.4

Then create a virtualenv (I'll name it myve)

pyenv virtualenv myve

And activate it with activate

pyenv activate myve

The only difference with your typical virtualenv created manually is the location and that you'll have a name for it, but, in the end, it's quite comfortable and similar to how virtualenvwrapper works.

Check which virtualenv or version is activated with

pyenv versions

Deactivate with

pyenv deactivate 

Remove a virtualenv or a python version with uninstall

pyenv uninstall myve
0

The recently old way

Use Pyenv and the first thing you do is install the virtualenv plugin. Makes naming and using Virtual environments a charm.

*

The new way

Still use Pyenv for maintaining python versions behind the scenes. Use pipenv as package management + virtual environment tool. The only trick is to add export PIPENV_PYTHON="$PYENV_ROOT/shims/python" to .rc file (.bashrc or .zshrc) after PYENV_ROOT gets updated using it's shim. pyenv which python could go wrong later, but pipenv wouldn't. I go into more detail in my blog post, shamelessly mentioning a plug.

monkut
  • 42,176
  • 24
  • 124
  • 155
The Nomadic Coder
  • 580
  • 1
  • 4
  • 16
  • So I just installed `pyenv` after having been using `pipenv` and having it installed for awhile... it sounds like to get my system in the right state I should *uninstall* my original pipenv and reinstall it into each of the pyenv installed python versions? Is that right? – monkut Oct 17 '18 at 06:55