CentOS的Python环境安装

Python 3.5安装

1.Python官网下载地址: https://www.python.org/downloads/

2.安装过程:

#找一个源文件存放目录,这里我的存放目录为是/usr/local/src/   
cd /usr/local/src/;
#pip3需要ssl
yum install openssl openssl-devel sqlite-devel -y;
wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz;
tar -xvf Python-3.5.3.tar.xz ;

#编译安装
cd Python-3.5.3;
./configure --prefix=/usr/local/python3.5 --enable-shared;
make;
make altinstall ;
echo "/usr/local/python3.5/lib/" >/etc/ld.so.conf.d/python3.conf
ldconfig
#至此安装完成,Py目录为:/usr/local/python3.5/bin/python

#安装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
/usr/local/python3.5/bin/python3.5 setup.py install

#安装虚拟环境工具virtualenv
/usr/local/python3.5/bin/pip3.5 install virtualenv
#虚拟出mywebapp的python环境
/usr/local/python3.5/bin/virtualenv --distribute /mnt/app/mywebapp_python3.5

python 2.7.15安装

系统默认的python2.7版本为2.7.5,很多包不支持。故可以安装python2.7.15

cd /tmp && wget http://python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -zxvf Python-2.7.15.tgz
cd Python-2.7.15/
./configure --with-ssl --prefix=/usr/local/python27
make && make install
cd /tmp && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
/usr/local/python27/bin/python get-pip.py
cd /usr/local/python27/bin/
ls
./pip install virtualenv

#虚拟化环境
./virtualenv --distribute /mnt/app/mywebapp-python27
#全局加载虚拟化环境
windows下: activate.bat
linux下: source /mnt/app/mywebapp-python27/bin/activate
#退出虚拟环境
deactivate

更好的虚拟化管理工具:virtualenvwrapper

virtualenvwrapper方便以列表的方式管理各个虚拟环境,安装virtualenvwrapper:

# windows下: 
pip install virtualenvwrapper-win
# linux下:
pip install virtualenvwrapper

echo "source /usr/bin/virtualenvwrapper.sh">> ~/.bash_profile
创建基本环境:mkvirtualenv [环境名]
删除环境:rmvirtualenv [环境名]
激活环境:workon [环境名]
退出环境:deactivate

列出所有环境:workon 或者 lsvirtualenv -b
新建虚拟环境
mkvirtualenv firstpython
mkvirtualenv mywebapp -p $(which python3)
查看有哪些虚拟环境: workon
进入虚拟环境: workon mywebapp

发表评论

邮箱地址不会被公开。