Python3安装问题汇总

笔者用全新的一台虚拟机安装Python3.x版本遇到的一些问题,记录一下方便以后大家遇到同样问题可参考解决。

一、系统信息

1、以下两条命令可以查看服务器系统版本和内核版本

[root@dawn ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@dawn ~]# uname -r
6.3.2-1.el7.elrepo.x86_64

2、python3下载安装:

1). wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

创建一个安装文件位置

2). mkdir -p /usr/local/python3

3、解压Python安装包

进入解压后的目录,编译安装

3). cd Python-3.6.1
  ./configure --prefix=/usr/local/python3
  make
  make install    
  或者 make && make install

4、建立python3的软链

   ln -s /usr/local/python3/bin/python3 /usr/bin/python3

5、建立pip3的软链接

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

6、并将/usr/local/python3/bin加入PATH

#  vim ~/.bash_profile
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Get the aliases and functions
  if [ -f ~/.bashrc ]; then
  . ~/.bashrc
  fi
  # User specific environment and startup programs
  PATH=$PATH:$HOME/bin:/usr/local/python3/bin
  export PATH
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #  source ~/.bash_profile

7、检查Python3及pip3是否正常可用

python3 -V
   pip3 -V

报错:

zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] 错误 1

解决方法:

在重新编译之前还需要在解压源文件中修改Modules/Setup.dist文件,将

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

这行的注释去掉

zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

安装 zlib-devel

yum -y install zlib-devel

笔者用pip3安装psutil模块的时候出现了下面的报错:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting psutil
Could not fetch URL https://pypi.python.org/simple/psutil/: There was a problem confirming the ssl certificate: Can’t connect to HTTPS URL because the SSL module is not available. - skipping
Could not find a version that satisfies the requirement psutil (from versions: )
No matching distribution found for psutil

解决方法:

1、使用rpm -aq|grep openssl命令查询openssl是否正确安装,发现缺少openssl-devel包
Python3安装问题汇总_第1张图片
2、安装openssl-devel包

  yum install -y  openssl-devel

3、重新编译安装python

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

4、测试

[root@dawn Python-3.6.1]# pip3 install psutil
Collecting psutil
  Downloading https://files.pythonhosted.org/packages/d6/0f/96b7309212a926c1448366e9ce69b081ea79d63265bde33f11cc9cfc2c07/psutil-5.9.5.tar.gz (493kB)
    100% |████████████████████████████████| 501kB 2.6MB/s 
Installing collected packages: psutil
  Running setup.py install for psutil ... done
Successfully installed psutil-5.9.5

5、升级pip

[root@dawn Python-3.6.1]# pip3 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 159kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-21.3.1

以上安装Python3.x方法,笔者使用过好多次供大家参考;

你可能感兴趣的:(Python,python,linux,centos)