(一)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
依次执行如下命令
2、下载python的安装包
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4rc1.tgz
3、解压安装
tar -zxvf Python-3.6.4rc1.tgz cd Python-3.6.4rc1/ ./configure --prefix=/usr/local/python3 make && make install
4、建立软链接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
5、更新pip
pip3 install --upgrade pip
延伸:
Linux升级安装python2.7.5到python2.7.9:https://www.cnblogs.com/andy9468/p/12574355.html
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
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: 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