Why do you have version management

Let’s take an example:

  1. Project 1 requires Python 2.x
  2. Project 2 will go to Python 3.x

If you want to run both projects on the server, you need to have two versions installed. If we have more projects and need more releases, we need to have a concept of version management.

Why have a virtual version

As we saw above, versioning addresses the need for python versions for different projects. If we have added a project on the server, the situation is as follows, how to solve it

  1. Project 1 requires Python 2.x
  2. Project 2 is going to Python 3.x, requiring that the requests package imported be 1.x
  3. Project 3 is going to Python 3.x, and the requests package to import is 2.x

We see that project 2 and Project 3 are going to the same version, but the dependent packages are of different versions, and we cannot install different versions of the same package on the same version. It’s easy to get confused. Hence the need for virtual environments. The virtual environment is to create a copy of the required version and manage dependencies in the copy so that different projects use their own copies without interfering with each other.

Relationship between

  1. Pyenv is used to manage versions of Python. For example, if you have a 2.x version of Python installed, you can use PyEnv to install other versions of Python, so that the system can support multiple versions at once. And does not affect the system version.
  2. Pyenv-virtualenv is used to create a virtual environment, so that different projects have their own independent running environment, avoid interference with each other.
  3. Pipenv has two functions, one is to manage dependencies (in place of the PIP management tool). Second, you can create a virtual environment (using the same method as Pyenv-Virtualenv).

In simple terms, install PyEnv and then Pipenv. You can meet your daily development needs. Install Pyenv-VirtualEnv if you need more of a virtual environment. We’ll talk more about their functions later. Make a choice based on actual requirements.

Use policies

  1. Pyenv and PipenV can be installed to meet the basic needs of daily development, you can use Pipenv to manage dependencies, and virtual environments
  2. Pyenv: pyenv-VirtualEnv: Pyenv-VirtualEnv: Pyenv-VirtualEnv: Pyenv-VirtualEnv: Pyenv-VirtualEnv It is much more convenient to use than pipenv or Pyenv-VirtualEnv alone.

Pyenv use

Pyenv installation

Pyenv does not support Windows, only MAC and Linux. A simple installation script is provided that automatically installs PyEnv and Pyenv-VirtualEnv

  1. Run the following command to start the installation
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
Copy the code
  1. After the installation is complete, configure environment variables as prompted. In general, add the following code to ~/.bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Copy the code

Pyenv uninstall

After pyEnv is installed, a.pyenv folder is created in the user’s home directory. To uninstall, simply delete the folder and remove the 3 lines of code in ~/.bashrc that were added during installation. Delete pyenv.

rm -rf $(pyenv root)
# or
# rm -rf ~/.pyenv
Copy the code

Common commands

  1. Version management
  • pyenv versionsView existing versions on the local PC
  • pyenv install -lView installable versions
  • Pyenv install 2.7.3Installs the specified version
  • Pyenv uninstall 2.7.3Uninstall the specified version
  1. Switch between three versions, sorted by priority :shell local Global
  • Pyenv shell 2.7.3Set the shell-oriented Version of Python by setting the PYENV_VERSION environment variable for the current shell. This version has a higher priority than local and Global.- the unsetArguments can be used to cancel the current version of the shellpyenv shell --unset.
  • Pyenv local 2.7.3Set the local version of Python by writing the version number to the.python-version file in the current directory. The Python version set in this way has a higher priority than global. This way, every time you go to the directory and run the script with a Python command, the set version is automatically used. And it doesn’t affect the global environment
  • Pyenv global 2.7.3Set the global Python version by writing the version number to the ~/. Pyenv /version file. This approach markets the global environment and should be used with caution
  • pyenv rehashCreate shims for all installed executables, such as ~/.pyenv/versions//bin/Therefore, you should run this command every time you add or delete a Python version or a package with an executable (such as PIP).
  1. Virtual Environment Management
  • Pyenv virtualenv 2.7.10 env - 2.7.10Create a virtual environment. If the Python version is not specified, the python version of the current environment is used by default. If you specify a Python version, it must already be installed, otherwise an error will occur. The real directory of the environment is under ~/.pyenv/versions
  • pyenv virtualenvsLists the current virtual environment
  • pyenv activate env-nameActivating a Virtual Environment
  • pyenv deactivateExit the virtual environment and return to the system environment
  • pyenv uninstall my-virtual-envDelete a virtual environment or directly delete a directoryrm -rf ~/.pyenv/versions/env-name

You can also use a virtual environment, such as pyenv local env-name, to use a virtual environment for the current directory. Pyenv activate env-name is more convenient than Pyenv activate env-name.

Pipenv use

Pipenv installation

Install using PIP

pip install pipenv
Copy the code

If you want a shell to complete automatically, wouldn’t it be better to complete automatically under bash under Linux or Mac? Please append the following statement to.bashrc or.zshrc:

eval "$(pipenv --completion)"
Copy the code

Pipenv Common command

  • Pipenv - python 3.6When creating a virtual environment, PipenV will automatically scan the system for the appropriate version. If pyEnv is installed, pyEnv will be called to download the corresponding version of Python, otherwise an error will be reported.
  • pipenv shellEnter the Virtual Environment
  • pipenv install urllib3Install a virtual environment or third-party library, or specify a versionPipenv install urllib3 = = 1.22
  • pipenv uninstall urllib3Uninstall a library and use it allpipenv uninstall --all
  • pipenv update urllib3Updates the specified package without parameterspipenv updateAll current packages are uninstalled and their latest versions installed
  • pipenv --whereView the project root directory
  • pipenv --venvView the virtual environment directory
  • pipenv runRun commands in a virtual environment
  • pipenv checkCheck for security holes
  • pipenv graphDisplays the current dependency graph information
  • pipenv lockLock and generate the pipfile.lock file
  • pipenv openView a library in the editor

The resources

github.com/pyenv/pyenv

Github.com/pyenv/pyenv…

github.com/pypa/pipenv

Segmentfault.com/a/119000001…

www.xncoding.com/2016/05/09/…

This article is from my personal blog www.xingjiehu.com