程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Linux installs Python versions. This is enough

編輯:Python

List of articles

  • Preface
  • One 、Linux install Python
    • 1、 Install dependency packages
    • 2、 download Python Installation package
      • 1、 Select the correct installation package
      • 2、 Download the installation package you need
    • 3、 decompression
    • 4、 install
    • 5、 Establish a soft connection
      • 1) Soft connection command
      • 2) Soft command description
        • ① determine python and pip The operating position of
        • ② Set up soft connections
    • 6、 function python, See if it's available
  • Two 、Python Other problems in operation
    • 1、“ModuleNotFoundError: No module named '_bz2'”
  • 3、 ... and 、 Reference article

Preface

Online Linux Installation on Python There are many ways , But most of them are troublesome , It takes a lot of steps , My scalp feels numb . And many articles say how to install step by step , But the principle or something , Not much , For beginners , When something goes wrong , Unable to resolve .
The following method , I think it is relatively simple , And better understand Python principle , Better operation , Error free methods .

One 、Linux install Python

1、 Install dependency packages

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

The above command directly copies , Just run

2、 download Python Installation package

1、 Select the correct installation package

1)Python The web address of the installation package is :https://www.python.org/ftp/python/3.8.0/
For this URL , Let's take it apart :

  • https://www.python.org/ftp/python/ Is the path of each version , The screenshot after the visit is :

    You can see this is Python A collection of installation packages for each version
  • 3.8.0/:Python Version of

So when we need to install what version of the installation package , Go directly to the path , Download it
like , download 3.9.7 Version of : The path is :https://www.python.org/ftp/python/3.9.7/

2、 Download the installation package you need

for example :https://www.python.org/ftp/python/3.9.7/ The web page of is :

We download .tgz File can 「Windows Install exe Program ,mac install pkg」, The download command is :

wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz

3、 decompression

tar -zxvf Python-3.9.7.tgz

After decompressing , Here's the picture :

4、 install

cd Python-3.9.7
./configure --prefix=/usr/local/python3
make && make install

The above commands come line by line , Go to the installation package directory first , Then install , Wait for the installation run to end

5、 Establish a soft connection

1) Soft connection command

ln -s /usr/local/python3/bin/python3.9 /usr/bin/python
ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip

2) Soft command description

Be careful : This place is the point , One is the connection python Operating position , The other is connected to pip The operating position of
Popular said , The function of this soft connection , It's telling the system ,python and pip Where to? !

Personally, I'm used to using python、pip, Not used to using python3、pip3, So my soft connection settings are /usr/bin/python and /usr/bin/pip

In the command /usr/bin/python and /usr/bin/pip Is the location of most machines , How do you determine the exact location of your machine ?「 determine python and pip The same way , With python For example 」

① determine python and pip The operating position of

where python
where pip


cd Go to the queried command path , Check the corresponding python and pip Soft connection status of :

where python
cd route
# see python Specific soft connection information , Pay attention to add *
la python*

Here's the picture :

If the path your soft connection points to is not python3.9 Of , Is to ② step

② Set up soft connections

First cd To where The path found by the command , Then delete the current soft connection , To reset , The operation steps are as follows :

If where After the order is executed , Multiple paths appear , Try every path , Until you decide which path really works

The above method , Can be used to switch Python edition , We can get Linux There are many Python edition , Which version is needed , We will soft connect to which version

Native Python Versions are in /usr/local/python3/bin Under the path :

When we need a new version , repeat 1 To 4 step , That will be /usr/local/python3/bin Create a new version file in the directory , And we don't need to delete the version we don't use , Just switch the soft connection , This is it. Python The essence of version switching , And in the Windows The above is implemented by setting environment variables .

6、 function python, See if it's available


If the above surface appears , It means that you have successfully installed , congratulations .

Two 、Python Other problems in operation

1、“ModuleNotFoundError: No module named ‘_bz2’”

Traceback (most recent call last):
File "stat_model.py", line 1, in <module>
from torchstat import stat
File "/usr/local/lib/python3.7/site-packages/torchstat/__init__.py", line 11, in <module>
from torchstat.reporter import report_format
File "/usr/local/lib/python3.7/site-packages/torchstat/reporter.py", line 1, in <module>
import pandas as pd
File "/usr/local/lib/python3.7/site-packages/pandas/__init__.py", line 55, in <module>
from pandas.core.api import (
File "/usr/local/lib/python3.7/site-packages/pandas/core/api.py", line 24, in <module>
from pandas.core.groupby import Grouper, NamedAgg
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
from pandas.core.groupby.generic import ( # noqa: F401
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 44, in <module>
from pandas.core.frame import DataFrame
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 88, in <module>
from pandas.core.generic import NDFrame, _shared_docs
File "/usr/local/lib/python3.7/site-packages/pandas/core/generic.py", line 70, in <module>
from pandas.io.formats.format import DataFrameFormatter, format_percentiles
File "/usr/local/lib/python3.7/site-packages/pandas/io/formats/format.py", line 48, in <module>
from pandas.io.common import _expand_user, _stringify_path
File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 3, in <module>
import bz2
File "/usr/local/lib/python3.7/bz2.py", line 19, in <module>
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

Question why :
bz2 This library is missing , Read other blogs about installation Python when bz library , namely “_bz2.cpython-36m-x86_64-linux-gnu.so” Wrong installation position
resolvent :
1、 lookup _bz2.cpython file

sudo find / -name "_bz2.cpython-*"

2、 Switch to python The version corresponds to the path , Copy the file to the directory :

cd /usr/local/python3/lib/python3.9/lib-dynload
sudo cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so ./

3、 Modify file name , take "-36m" It is amended as follows "-39" that will do 「 Keep consistent with other file names in the same directory 」:

sudo mv ./_bz2.cpython-36m-x86_64-linux-gnu.so ./_bz2.cpython-39-x86_64-linux-gnu.so

The operation is as follows :

3、 ... and 、 Reference article

linux-python3.8 install
ModuleNotFoundError: No module named ‘_bz2‘ Problem solving

Welcome to leave a message in the comment area , To know everything , can . If it feels good , Don't forget to like the collection !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved