Linux中安装python3

一、Ubuntu中安装python3

https://blog.csdn.net/chenzhuzi5981/article/details/100848412

 

二、centos中安装python3

(一)Linux中安装python3(含pip3自动安装)

1、首先安装依赖包

yum -y groupinstall "Development tools"
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 

  

一定要安装openssl模块,否则pip install会报错

https://cloud.tencent.com/developer/article/1626934

 

依次执行如下命令

2、下载python的安装包

wget -c http://npm.taobao.org/mirrors/python/3.8.6/Python-3.8.6.tgz

  

或者其他镜像源下载:

http://npm.taobao.org/mirrors/python  

 

3、解压安装

tar -zxvf Python-3.8.6.tgz
	
cd Python-3.8.6/
mkdir -p /usr/local/python3
./configure --prefix=/usr/local/python3 && make && make install

  

4、建立软链接

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

  

验证版本:

python3 -V
pip3 -V

  

5、修改python的第三方包的下载镜像

Linux下,修改 ~/.pip/pip.conf (没有就创建一个), 修改 index-url至tuna,内容如下:

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

https://www.cnblogs.com/andy9468/p/9982154.html

 

6、更新pip

pip3 install --upgrade pip

 

延伸:

Linux升级安装python2.7.5到python2.7.9https://www.cnblogs.com/andy9468/p/12574355.html

 

(二)【python3安装虚拟环境】

1、安装虚拟环境python包

安装虚拟环境的命令:

1) #安装虚拟环境

sudo pip3 install virtualenv

  

2)#安装虚拟环境命令封装包(virtualenvwrapper是对virtualenv繁琐命令的封装)

sudo pip3 install virtualenvwrapper 

  

3)编辑家目录下面的.bashrc文件,添加下面几行。(设置python的新环境变量)

vim  ~/.bashrc

 

# python3
WORKON_HOME=$HOME/.virtualenvs
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3/bin/virtualenv
source /usr/local/python3/bin/virtualenvwrapper.sh

 

4)使用source .bashrc使其生效一下。

source ~/.bashrc

  

5)如果报错:

virtualenvwrapper.sh: There was a problem running the initialization hooks.

就修改virtualenvwrapper.sh的which python 为which python3

Error while finding module specification for 'virtualenvwrapper.hook_loader'

python3 -m pip install --user virtualenvwrapper --upgrade

  

6)测试虚拟环境

mkvirtualenv  test01

  

虚拟环境推荐方法:

(1)创建python3虚拟环境命令:

mkvirtualenv test01_py3
# 或者
mkvirtualenv -p /usr/bin/python3 test01_py3

  

(2)创建python2虚拟环境:

mkvirtualenv -p python2 bj11_py3

  

(3)进入虚拟环境工作:

workon 虚拟环境名

  

(4)查看机器上有多少个虚拟环境:

workon 空格 + 两个tab键
lsvirtualenv

  

(5)退出虚拟环境:

deactivate

  

(6)删除虚拟环境:

rmvirtualenv 虚拟环境名

  

(7)虚拟环境下安装包的命令:

pip install 包名

 注意:不能使用sudo pip install 包名,这个命令会把包安装到真实的主机环境上而不是安装到虚拟环境中。

 

(8)批量安装包:

pip install -r requirements.txt

  

(9)查看虚拟环境中安装了哪些python包:

pip list
pip freeze

  

(10)导出pip清单到requirements.txt

pip freeze > requirements.txt

  

关联:https://www.cnblogs.com/andy9468/p/10355689.html

常见报错处理:

根据文档介绍执行第一步./configure –prefix=/usr/local/python3,后面添加参数为Python安装目录,默认安装目录为/usr/local/,如果出现以下错误提示则表示系统没有安装gcc
configure: error: no acceptable C compiler found in $PATH
安装gcc重新执行./configure –prefix=/usr/local/python3即可

其他文章:centos7中安装python3
http://blog.csdn.net/wjqwinn/article/details/75633714

1、安装gcc前的准备
yum provides '*/applydeltarpm'
yum install deltarpm -y

2、安装升级相应的依赖包(含gcc)
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

 
posted @ 2019-02-26 14:53  安迪9468  阅读(265)  评论(0编辑  收藏  举报