Python – How to use pyenv, virtualenv, and pipenv?

How to use pyenv, virtualenv, and pipenv?… here is a solution to the problem.

How to use pyenv, virtualenv, and pipenv?

I’ve been looking for a Python version manager similar to/equal to RVM (for Ruby), I found pyenv but it’s just a switcher so I need to combine it with virtualenv (my current understanding), a bit of a struggle, but I can get used to it. Also, I’ve read that pipenv is recommended instead of virtualenv, so can it be used with pyenv? What to do?

However, the article < a href="https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe" rel="noreferrer." noopener nofollow">What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? It is mentioned that pyenv is deprecated in Python 3.6. So I went to zero and confused, what should I use? How should I use it?

Systems: Arch Linux, Plasma.

Current (installed) versions of Python: 2.7.14 and 3.6.4

What I’m going to do: I need a framework, and if for any reason I need to use the following versions: 3.4.7, 3.2.6, 2.6.7 and 2.3.3, I can finish it effortlessly ass.

I hope for your help, thank you all.

Solution

You have two options

    Use pyenv

  1. and pyenv-virtualenv wrappers together.

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

  • pyenv installs 3.6.5
  • pyenv virtualenv 3.6.5 test
  • cd/project_path
  • pyenv tested locally

The next time you access the project directory, you will automatically switch to the test environment

  1. Use both pyenv and pipenv

First, add this script to the environment configuration (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
}

And then

  • pyenv installs 3.6.5
  • pyenv shell 3.6.5
  • pip installs pipenv
  • cd/project_path
  • pipenv –python 3.6.5

The next time you access the directory, it changes to the correct vent (note: you should use pyenv shell 3.6.5 before accessing the project directory).

Related Problems and Solutions