Linux 环境搭建Python3环境

前言:

  目前服务器为centos6.9, 系统自带的python的版本为2.6.x,这时我们需要用到Python3 和 pip3,但是原有的Python2 和pip2也得用,也就是说python3 and python2 共存,pip2 and pip3共存,下面文章将会介绍:

1、先到官方网站下载python3的安装包      

    wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz

    https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz 

2、解压包

     tar -xf Python-3.6.6.tar.xz

3、安装依赖

  yum install openssl-devel -y 
  yum install zlib-devel -y
  yum install libffi-devel -y

4、编译安装

  cd  Python-3.9.10
  ./configure --prefix=/usr/local/Python-3.9.10     #安装目录可以自己定义无所谓。
  make && make install

5、编译完成后会在如 /opt/下生成Python的文件夹 ,自定义软连接:
     ln -s /usr/local/Python-3.9.10/bin/python3 /usr/bin/python3

测试python3安装是否正常:

6、python3的安装已经完成,接下来给python3安装pip3

    1.首先安装setuptools

wget --no-check-certificate  https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26

tar -zxvf setuptools-19.6.tar.gz

cd setuptools-19.6

python3 setup.py build

python3 setup.py install

    2.然后安装pip

wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb

tar -zxvf pip-8.0.2.tar.gz

cd pip-8.0.2

python3 setup.py build

python3 setup.py install

3、安装完毕后,进入到Python目录下,查看生产的bin文件

      cd /opt/Python/bin

   ln -s /usr/local/Python-3.9.10/bin/pip3.6 /usr/bin/pip3            #创建软连接

4、测试是否安装成功

   pip3 -V         #检查版本
   pip 8.0.2 from /opt/Python/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg (python 3.6)

   使用pip3安装pymysql模块

   pip3 install pymysql

[[email protected] /opt/Python/bin]$ pip3 install pymysql
Collecting pymysql
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/a7/7d/682c4a7da195a678047c8f1c51bb7682aaedee1dca7547883c3993ca9282/PyMySQL-0.9.2-py2.py3-none-any.whl (47kB)
    100% |████████████████████████████████| 49kB 349kB/s 
Requirement already satisfied (use --upgrade to upgrade): cryptography in /opt/Python/lib/python3.6/site-packages (from pymysql)
Requirement already satisfied (use --upgrade to upgrade): six>=1.4.1 in /opt/Python/lib/python3.6/site-packages (from cryptography->pymysql)
Requirement already satisfied (use --upgrade to upgrade): cffi!=1.11.3,>=1.7 in /opt/Python/lib/python3.6/site-packages (from cryptography->pymysql)
Requirement already satisfied (use --upgrade to upgrade): idna>=2.1 in /opt/Python/lib/python3.6/site-packages (from cryptography->pymysql)
Requirement already satisfied (use --upgrade to upgrade): asn1crypto>=0.21.0 in /opt/Python/lib/python3.6/site-packages (from cryptography->pymysql)
Requirement already satisfied (use --upgrade to upgrade): pycparser in /opt/Python/lib/python3.6/site-packages (from cffi!=1.11.3,>=1.7->cryptography->pymysql)
Installing collected packages: pymysql
Successfully installed pymysql-0.9.2
You are using pip version 8.0.2, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

  安装完毕后,进入到python3查看使用pip3安装的pymysql模块是否可以被python3导入:

[[email protected] /opt/Python/bin]$ python3 
Python 3.6.6 (default, Jul 27 2018, 02:57:12) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>> exit(

  OK 完成 ~~

5、报错 "error: metadata-generation-failed"

Linux 环境搭建Python3环境_第1张图片

解决方法:pip3 install --upgrade setuptools

你可能感兴趣的:(python,linux,python,服务器)