In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to install and configure Python 3.6environment on CentOS 7. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Currently, Python 2 is installed by default in CentOS 7.3 of * *, and the installation package of Python 3 is not available in the default official yum source. Some users want to upgrade to use Python3 but may actually have a variety of problems, leading to errors. Take a look at the radical Fedora community, which changed the default version to Python3 at 23 (if I remember correctly).
Let's start with the system environment I'm using, a newly created Docker container. Using cat / etc/redhat-release, you can see that you are running CentOS version 7.3.
There are two main ways to install a Python environment on a pure CentOS system. One is to compile and install through the source code, and the other is to install the RPM package that has been packed. According to personal custom, let's first take a look at how to install Python 3.6and configure the virtual environment through source code compilation.
Compile and install with source code
Basic environment
Install several necessary packages first to facilitate subsequent operations
➜yum install wget gcc make➜ # wget for downloading source packages ➜# gcc and make for compilation
Download the source package on the official website of Python
➜wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
Unpack, decompress
➜xz-d Python-3.6.1.tar.xz➜ tar-xvf Python-3.6.1.tar
Compile
➜cd Python-3.6.1➜. / configure-- prefix=/usr/local/python3.6-- enable-optimizations ➜
First explain the above parameters,-- prefix is the expected installation directory,-- enable-optimizations is the optimization option (LTO,PGO, etc.) plus the flag compilation, the performance is optimized by about 10% (if you remember correctly), but this will significantly increase the compilation time. However, LTO and PGO are not discussed in today's article. It is suggested that you can take a look at the specific implementation of LTO in GCC. It should be impossible for me to write relevant articles. Ha. After all, I can't stop writing.
Next
➜make➜ make install
As shown in the picture, you will see that there is a mistake, prompting zlib not available, so we need to install the dependency.
➜yum install zlib-devel
So why do you need this dependency package? in fact, Python has a very important built-in module zipimport for importing modules from the Zip package. If there is no zlib package, then it cannot be unzipped, and naturally the module cannot be used.
When our dependencies are installed, we re-perform the above steps of compilation and installation, and find that the execution is successful.
➜/ usr/local/python3.6/bin/python3-- version Python 3.6.1
At this point, our Python 3 is installed successfully.
Check
Compiling and installing is far from enough, let's do some checks.
When we run the Python terminal and type import bz2, we will find that there is no such module. If you are not familiar with this module, it doesn't matter. We enter import sqlite3 students who are familiar with Python should be familiar with sqlite3, but if you are still not familiar with it, then press the arrow key to try. Have you noticed that the output becomes ^ [D^ [A] or something like this?
Students who are familiar with the Python source code must know what I am going to say. But if you're not familiar with it, it doesn't matter, I'll tell you. Normally, under the terminal, we expect that when the up arrow is entered, the previous command will be displayed, and the left arrow will be entered to move the cursor to the left. But the reality is different from our expectations, what is the reason?
Here is a little bit about the history of the input device, simply because a module is needed to escape the user's input. Going back to the focus of this article, we are compiling and installing without the readline module. Now that the problem has been found, the solution is actually very simple.
➜yum install readline-devel
After the installation is complete, repeat the above steps to compile & & install.
Re-check
The above just solved the problem of key input, but the two modules I mentioned still don't come in import, so let's take a closer look at the output of our compilation process. There is such a paragraph (depending on the system environment, the output may also be inconsistent)
Python build finished successfully! The necessary bits to build these optional modules were not found: _ curses bz2 _ dbm _ gdbm _ lzma _ sqlite3 _ tkinter readline To find the necessary bits, look in setup.py in detect_modules () for the module's name.
According to the above tips, it is clear that we are missing some (optional) modules that you can ignore if you don't think you can use them. I will use more of these modules, even the TK module that ordinary people will not touch. The solution is to install the corresponding module.
➜# solve import bz2 error➜ yum install bzip2-devel ➜# solve import curses error report➜ yum install ncurses-devel ➜# solve import sqlite3 error report➜ yum install sqlite-devel ➜# resolve _ dbm _ gdbm missing remind➜ yum install gdbm-devel ➜# resolve _ lzma missing remind➜ yum install xz-devel ➜# resolve _ tkinter missing remind➜ yum Install tk-devel ➜# solves the problem of missing reminders in readline and unexpected behavior of arrow keys➜ yum install readline-devel
When these modules are installed, the recompilation will find that the reminder has disappeared and can be installed.
Install using the rpm package
Let's start by introducing the IUS community, whose name is fully written with the initials [Inline with Upstream Stable]. It is mainly a community that provides a new version of the RPM package. For specific use, you can check the official documentation. To put it simply, just follow the command below.
➜yum-y install https://centos7.iuscommunity.org/ius-release.rpm
After adding IUS, create the cache metadata before installing it
➜yum makecache➜ yum install python36u ➜yum-y install python36u-pip➜ yum-y install python36u-devel
After completion, you can directly input python3 at the terminal.
Environment configuration
It just says the installation of Python, which is available directly if it is installed using RPM. But if it is compiled by source code, it is not necessary to enter a long list of paths every time. So the solution is simple, just add a link.
➜ln-s / usr/local/python3.6/bin/python3 / usr/bin/python3
In addition, when there are multiple Python versions in the system, try to avoid environmental pollution. I personally recommend using virtualenv to create a stand-alone virtual environment, which I also use on a daily basis. However, after we have installed Python 3.6, we can directly execute the following command to create the virtual environment.
➜python3-m venv py3➜ source py3/bin/activate (py3) ➜python-V Python 3.6.1
There are two main ways to install Python 3.6 on CentOS 7: compiling the source code and installing the RPM package.
For readers who want to quickly use the source code to configure the environment but do not want to care about the specific reasons, you can directly use this script provided on my GitHub to install https://raw.githubusercontent.com/tao12345666333/dotfiles/master/env/install_Python36_on_CentOS7.sh
In addition, Python 3.6 can directly use python3-m venv venv_name to create a virtual environment.
This is how to install and configure the Python 3.6environment on CentOS 7. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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: 206
*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
© 2024 shulou.com SLNews company. All rights reserved.