In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of Python multi-version development environment management tools. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Preface
In Python development, in some cases, we may be faced with the need to install multiple versions of Python on one machine at the same time. For example:
There are multiple Python projects, each relying on a different version of Python.
There is a Python project that needs to support multiple Python versions at the same time.
So, how can you efficiently install and maintain multiple versions of Python (specifically, Python refers to the Python interpreter) on a single machine?
In addition, we may be faced with the need to install multiple versions of a Python third-party library on a single machine. For example, there are multiple Python projects, each relying on a different version of the Python third-party library requests. At this time, how to implement the installation and maintenance of multiple versions of Python requests libraries on a single machine?
This article introduces an artifact. It provides the simplest way to meet the above two needs at the same time.
Multi-version Python management
The tool that implements multiple versions of Python management is called pyenv. Its installation command is:
Curl https://pyenv.run | bash
After the installation is complete, simple configuration is required. Add the following lines of configuration information to the file ~ / .bashrc, and then execute the command exec "$SHELL" to make the configuration take effect.
Export PATH= "$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
Next, you can check whether the installation and configuration is successful by looking at the pyenv version:
Root@hzettv53:~# pyenv-vpyenv 1.2.12
Because pyenv compiles and installs Python based on source code. Therefore, we need to install some compilation-related dependency packages first. Because these dependencies are operating system related. Therefore, different operating systems have different installation commands.
Take a common Ubuntu/Debian system as an example, the installation command is:
Sudo apt-get install-y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
You can now install Python using pyenv. Before installing the new version, let's check the currently installed version of Python on the system:
Root@hzettv53:~# pyenv versions* system (set by / root/.pyenv/version) root@hzettv53:~# python-VPython 2.7.12
As you can see, you are currently using the Python that comes with the operating system, which is version 2.7.12. At this point, if we need to install a new version, such as Python 3.7.2, simply execute the command:
Pyenv install-v 3.7.2
Note that because pyenv is compiled and installed based on Python source code, this step is a bit slow and requires patience. After the installation is complete, check the Python version on the system:
Root@hzettv53:~# pyenv versions* system (set by / root/.pyenv/version) 3.7.2root@hzettv53:~# python-VPython 2.7.12
As you can see, there are already two versions of Python in the system. However, the system version is still being used. If you want to use the newly installed version, just:
Root@hzettv53:~# pyenv global 3.7.2root@hzettv53:~# pyenv versionssystem* 3.7.2 (set by / root/.pyenv/version) root@hzettv53:~# python-VPython 3.7.2
The command pyenv global 3.7.2 here changes the global Python version. If you only want to use Python 3.7.2 in the current folder, you can execute: pyenv local 3.7.2; if you only want to use Python 3.7.2 in the current Shell environment, you can execute: pyenv shell 3.7.2.
If three Python versions of global, local and shell are set at the same time, which one will really work? There is a priority, that is, shell > local > global. For example, if pyenv local 3.7.2 and pyenv global 3.7.3 are executed successively, then because local takes precedence over global, the currently effective version of Python is 3.7.2.
In this way, we can easily install multiple versions of Python on a single machine, and we can flexibly switch Python versions according to actual needs.
Multi-virtual environment management
Different projects may depend not only on different versions of Python, but also on different versions of Python third-party libraries. We refer to the Python and its third-party libraries on which the project depends as virtual environments. If there are multiple Python projects that depend on different virtual environments, how to manage them effectively?
At this point, we can still use pyenv to achieve our goals. Use the following command to create a virtual environment with a specified version of Python.
Pyenv virtualenv
The name of the virtual environment is recommended to reflect the name of the Python project. For example, if we have a project called myproject that is developed based on Python 3.7.2, we can execute the command:
Pyenv virtualenv 3.7.2 myprojectenv
In this way, we created a virtual environment called myprojectenv. So how do you use it? Where we need to use this virtual environment (for example, the folder path from cd to the project myproject), execute the following command:
Pyenv local myprojectenv
At this point, we can see that both Python and pip currently in use point to the virtual environment myprojectenv:
Root@hzettv53:~/workspace/test# pyenv which python/root/.pyenv/versions/myprojectenv/bin/pythonroot@hzettv53:~/workspace/test# pyenv which pip/root/.pyenv/versions/myprojectenv/bin/pip
This means that all Python third-party packages installed with the pip command at this time are installed in the path of the virtual environment myprojectenv, not the system path. In this way, we have implemented the binding between the Python project and the Python development environment (that is, the virtual environment) on which it depends. Different Python projects can use different Python virtual environments without affecting each other.
So, what if an Python project needs to use two Python virtual environments? We only need to create two virtual environments (such as myprojectenv and myprojectenv2) and switch them when we use them:
Root@hzettv53:~/workspace/myproject# pyenv local myprojectenvroot@hzettv53:~/workspace/myproject:~# python-VPython 3.7.2root@hzettv53:~/workspace/myproject# pyenv local myprojectenv2root@hzettv53:~/workspace/myproject:~# python-VPython 2.7.12 this is the end of the article on "sample Analysis of Python Multi-version Development Environment Management tools". I hope the above content can be helpful to you so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
The method introduced in this paper is simple, fast and practical.
© 2024 shulou.com SLNews company. All rights reserved.