current position:Home>[centos7] how to install and use Python under Linux

[centos7] how to install and use Python under Linux

2022-02-01 01:43:18 Vegetable cake without vegetable

  Access permissions

  • In the graphical interface (ctrl+ alt+F2 Switch ), The application in the upper left corner , Select terminal , Input :
su
 Then enter the password according to the prompt , You can enter and get root jurisdiction 
 Copy code 

Based on the environment

  • First install several necessary packages , In order to facilitate the subsequent operation
*  yum install wget gcc make
*  # wget  For downloading the source package 
*  # gcc  and  make  Used to compile 
 Copy code 

download python package

Open the web page ,www.python.org, stay download Find the version to download in the page ( Here is python3.6.5), Save to home In the folder , Then decompress .( extract )

  • compile
*  cd Python-3.6.5
*  ./configure --prefix=/usr/local/python3.6 --enable-optimizations
*  
 Copy code 

First explain the above parameters ,--prefix  Is the expected installation directory ,--enable-optimizations  It's optimization options (LTO,PGO etc. ) Add this flag After compiling , Performance has 10% Optimization of left and right ( If I remember correctly ), But this will significantly increase the compilation time . But about LTO and PGO In fact, it is not within the scope of today's article , It is suggested that those who are interested can have a look  GCC in LTO The concrete realization of . I should be less likely to write relevant articles Ha ha ha After all, you can't stop writing .

Next

*  make
*  make install
 Copy code 

You'll see an error , Tips  zlib not available  therefore , We need to pretend to rely on

* yum install zlib-devel
 Copy code 

Then why do you need this dependency package , Because Python There is a very important built-in module  zipimport  For from Zip Import module in compressed package . And if not zlib My bag , Then you can't decompress , Naturally, the module cannot be used .

When our dependencies are installed , Re execute the above steps of compiling and installing , Found execution successful .

*  /usr/local/python3.6/bin/python3 --version
Python 3.6.5
 Copy code 

  • Check

Compiling and installing is not enough , Let's do some tests .

When we run Python terminal , Input  import bz2  When , You will find that there is no such module . If you are not familiar with this module , It doesn't matter . We type in  import sqlite3 be familiar with Python My classmates should be right sqlite3 Not too strange , But if you're still unfamiliar , Then press the direction key to try . Did you find , The output becomes ^[[D^[[A  Something like this ?

be familiar with Python Source code students , You must know what I'm going to say . But if you're not familiar with , No problem , I'll tell you . Normally speaking , Under the terminal, we expect that when the up arrow key is input , The previous command will be displayed , Enter the left direction key , You can move the cursor to the left . But the reality is different from our expectations , What's the cause of the ?

Here's a little bit about the historical reasons for the input device , Simply put, a module is needed to escape the user's input . Back to the point of this article , That is, when we compile and install, we lack  readline  This module . Now that the problem has been found , The solution is actually very simple

* yum install readline-devel
 Copy code 

After installation , Repeat the above steps , compile && Can be installed .

Use python Write code

Pip

Install well python after , The next thing you have to install is pip 了 .

curl  https://bootstrap.pypa.io/get-pip.py | python
 Copy code 

  Set up pip Install source , Increase download speed

Create folder

mkdir home/pip
 Copy code 

  utilize cd Order to enter pip In the folder , establish pip.conf file

vi pip.conf
 Copy code 

Enter the following command , And then click Esc, Input :wq Save and exit .

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
 Copy code 

install setuptool

pip install ez_setup
 Copy code 

  Try installing scrapy, Report errors :error: command 'gcc' failed with exit status 1

terms of settlement :

yum install gcc libffi-devel python-devel openssl-devel
 Copy code 

To write python Code :

vi xxx.py
 Copy code 

 (Esc,:wq Save and exit )

function :

python xxx.py
 Copy code 

Reference link :segmentfault.com/a/119000000…

copyright notice
author[Vegetable cake without vegetable],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010143158362.html

Random recommended