Why do you have version management
Let’s take an example:
- Project 1 requires Python 2.x
- 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
- Project 1 requires Python 2.x
- Project 2 is going to Python 3.x, requiring that the requests package imported be 1.x
- 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
- 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.
- Pyenv-virtualenv is used to create a virtual environment, so that different projects have their own independent running environment, avoid interference with each other.
- 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
- Pyenv and PipenV can be installed to meet the basic needs of daily development, you can use Pipenv to manage dependencies, and virtual environments
- 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
- 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
- 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
- Version management
pyenv versions
View existing versions on the local PCpyenv install -l
View installable versionsPyenv install 2.7.3
Installs the specified versionPyenv uninstall 2.7.3
Uninstall the specified version
- Switch between three versions, sorted by priority :shell local Global
Pyenv shell 2.7.3
Set 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 unset
Arguments can be used to cancel the current version of the shellpyenv shell --unset
.Pyenv local 2.7.3
Set 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 environmentPyenv global 2.7.3
Set 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 cautionpyenv rehash
Create 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).
- Virtual Environment Management
Pyenv virtualenv 2.7.10 env - 2.7.10
Create 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/versionspyenv virtualenvs
Lists the current virtual environmentpyenv activate env-name
Activating a Virtual Environmentpyenv deactivate
Exit the virtual environment and return to the system environmentpyenv uninstall my-virtual-env
Delete 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.6
When 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 shell
Enter the Virtual Environmentpipenv install urllib3
Install a virtual environment or third-party library, or specify a versionPipenv install urllib3 = = 1.22
pipenv uninstall urllib3
Uninstall a library and use it allpipenv uninstall --all
pipenv update urllib3
Updates the specified package without parameterspipenv update
All current packages are uninstalled and their latest versions installedpipenv --where
View the project root directorypipenv --venv
View the virtual environment directorypipenv run
Run commands in a virtual environmentpipenv check
Check for security holespipenv graph
Displays the current dependency graph informationpipenv lock
Lock and generate the pipfile.lock filepipenv open
View 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