Bob's Blog

Web开发、测试框架、自动化平台、APP开发、机器学习等

返回上页首页

在centos上安装python3和配置虚拟环境



目前成功尝试了2个方法,推荐用方法2.

方法一:

下载python3.6.8:

cd /tmp
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz

解压python3.6.8:

xz -d Python-3.6.8.tar.xz  (if show command not found, run: yum install xz)
tar -xvf Python-3.6.8.tar

编译:

cd Python-3.6.8
./configure --prefix=/usr/local/python3.6 --enable-optimizations --with-ssl
(if show 'no acceptable C compiler found in $PATH', run 'yum install gcc')
make
make install  (if get error: 'zipimport.ZipImportError: can't decompress data; zlib not available', run: yum install zlib-devel)

创建连接: 

ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3
ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3
now you can run python3 in console. (if show error 'pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.', run: yum install openssl-devel, then recompile)

安装virtualenv并创建虚拟环境 

pip3 install virtualenv
virtualenv --no-site-packages venv  (if get error '-bash: virtualenv: command not found', run 'python3 -m virtualenv venv' instead)

 

方法二:

安装相关的包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

下载python:

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

解压和安装python3:

unxz Python-3.6.8.tar.xz
tar -xvf Python-3.6.8.tar
cd Python-3.6.8
./configure prefix=/usr/local/python3
make && make install

创建软链接:

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

此时运行python3命令则是启动python 3.6.8

安装pip3

先安装一些环境:

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

然后用setuptools安装pip并创建软链接:

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

设置软链接

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

此时可以用pip3 list命令

 

然后安装virtualenv,但这里需要多安装virtualenvwrapper以及配置.bashrc

pip3 install virtualenv 
pip3 install virtualenvwrapper
vim ~/.bashrc

在文末填入以下代码并保存

VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3 # 指定virtualenvwrapper执行的python版本 
export WORKON_HOME=$HOME/.virtualenvs # 指定虚拟环境存放目录,.virtualenvs目录名可自拟 
source /usr/local/python3/bin/virtualenvwrapper.sh # virtualenvwrapper.sh所在目录

保存文件,执行source ~/.bashrc

当用virtualenv创建虚拟环境时,如果提示virtualenvwrapper could not find virtualenv in your path,那么再添加一个软链接:

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

然后就可以创建虚拟环境了

mkvirtualenv --python=/usr/bin/python3 venv --no-site-packages

需要注意的是venv不在当前路径下了,需要source /root/.virtualenvs/venv/bin/activate。当然也可以将整个虚拟环境的目录复制到某个地址然后直接执行source venv/bin/activate。

-------------------------------

另外如果在运行python或者用django的时候遇到这个错误:"ImportError: /lib64/libz.so.1: version `ZLIB_1.2.9'",这个时候可以按如下命令操作一次:

wget http://zlib.net/zlib-1.2.13.tar.gz  # 版本可以到zlib.net上看
tar zxf zlib-1.2.13.tar.gz
cd zlib-1.2.13
./configure
make && make install
cd /lib64/
ls | grep libz
mv libz.so.1 libz.so.1-bak
ln -s /usr/local/lib/libz.so.1.2.13 /lib64/libz.so.1

如果在用torch遇到了"No module named '_lzma'"的错误,可以按如下命令操作一次:

yum install zx-devel -y
yum install python-backports-lzma -y
pip3 install backports.lzma  # 如果提示错误,再运行zx-devel这句
# 然后把/usr/local/python3.8/lib/python3.8/lzma.py的文件的import改成下面的
try:
    from _lzma import *
    from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
    from backports.lzma import *
    from backports.lzma import _encode_filter_properties, _decode_filter_properties

 

 

下一篇:  解决github下载特别慢的方法
上一篇:  python做web开发时uWSGI和nginx的作用和区别

共有0条评论

添加评论

暂无评论