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

yum install python3-pip

7、测试是否安装成功

   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

[root@192.168.200.143 /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导入:

[root@192.168.200.143 /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 完成 ~~

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

解决方法:pip3 install --upgrade setuptools

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值