python升级到2.7.10及使用virtualenv搭建独立的Python环境
http://qicheng0211.blog.51cto.com/3958621/1561685
重新整理一下(2015-12-2更新):
先下载python2.7.10进行安装:
1 2 3 |
wget wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz tar -zxvf Python-2.7.10.tgz && cd Python-2.7.10 && ./configure --prefix=/usr/local/python2.7 && sudo make && sudo make install |
系统中在/usr/local/python2.7中安装了一个新版本的python后,再在新版本的python中安装ease_install-2.7, 再用新版本的ease_install-2.7安装pip2.7:
1 2 3 4 5 |
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py sudo /usr/local/python2.7/bin/python ez_setup.py easy_install-2.7 pip |
用新安装的pip2.7安装虚拟环境:
1 |
pip2.7 install virtualenv && pip2.7 install virtualenvwrapper |
在~/.bashrc中定义环境变量:
1 2 3 4 5 |
#PYTHON_HOME=/usr/local/python2.7/bin/ VIRTUALENVWRAPPER_PYTHON=/usr/local/python2.7/bin/python WORKON_HOME=$HOME/.virtualenvs # 放所有虚拟环境的地方 PROJECT_HOME=$HOME/workspaces # 放所有项目的地方 export PATH=$PATH:$PYTHON_HOME:$WORKON_HOME:$PROJECT_HOME |
更新环境变量:
1 2 3 |
source ~/.bashrc source /usr/local/python2.7/bin/virtualenvwrapper.sh |
指定使用python2.7创建虚拟环境:
1 2 3 |
mkvirtualenv venv --python=/usr/local/python2.7/bin/python workon venv |
=========================================================
经过试验,总结教训如下:
系统自带的python,也就是/usr/bin/python,一定要使用系统自带的。
可以在/usr/local/python2.7.10目录下新安装一个python, 然后使用这个python安装virtualenv和virtualenvwrapper, 然后安装ease_install, pip
然后在~/.bashrc里加入下面内容:
1 2 3 4 5 6 7 8 9 10 |
# virtualenv #export PIP_REQUIRE_VIRTUALENV=true export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=/data/apps/opt/myprojects export VIRTUALENVWRAPPER_PYTHON=/usr/local/python2.7/bin/python export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages' export tPIP_RESPECT_VIRTUALENV=true#这两句话的意思是pip安装东西的时候不安装到本地环境 source /usr/local/python2.7/bin/virtualenvwrapper.sh |
pip安装python-rrdtool时依赖关系报错解决:
1 2 3 4 5 6 |
tar zxvf pip-1.5.6.tar.gz cd pip-1.5.6 python setup.py install yum install gcc cmake make python-devel cairo-devel libxml2 libxml2-devel pango-devel pango libpng-devel freetype freetype-devel libart_lgpl-devel -y ln -s /usr/lib64/librrd.so.4 /usr/lib64/librrd.so pip install python-rrdtool-1.4.7 |
若报错:Failed building wheel for cryptography和Failed building wheel for cffi,则安装如下包:
1 |
sudo yum install gcc libffi-devel python-devel openssl-devel |
以上为2015-10-18号备注。
=========================================================
下载python2.7.10:
1 2 3 4 5 |
wget wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz tar -zxvf Python-2.7.10.tgz cd Python-2.7.10/ ./configure --prefix=/usr/local/python2.7 sudo make && make install |
make时报错:Python build finished, but the necessary bits to build these modules were not found:…
首先安装依赖包:
1 2 |
sudo yum install -y ncurses-devel sudo yum install -y zlib-devel mysql mysql-devel zlib |
再重试make && makei install
安装成功后查看版本:
1 |
/usr/local/python2.7.10/bin/python2.7 -V |
接下来需要创建一个链接来使系统默认python变为python2.7.10。
1 |
sudo ln -fs /usr/local/python2.7/bin/python /usr/bin/python |
进行更改后,yum果然无法运行了。修改/usr/bin/yum文件
1 |
sudo vim /usr/bin/yum |
将第一行的
1 2 |
#!/usr/bin/python中的python改为安装的python版本,我的如下: #!/usr/bin/python2.7 |
下载ez_setup.py
1 |
python ez_setup.py install |
安装好后运行 easy_install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@vice tmp]# easy_install Traceback (most recent call last): File "/usr/bin/easy_install", line 5, in <module> from pkg_resources import load_entry_point File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3074, in <module> File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3060, in _call_aside File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3087, in _initialize_master_working_set File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 647, in _build_master File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 660, in _build_from_requirements File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 833, in resolve pkg_resources.DistributionNotFound: The 'setuptools==0.9.8' distribution was not found and is required by the application |
发现命令运行会报错。重新安装python的distribution-0.6.49(0.7.3的会报错)
1 2 3 4 5 |
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz tar -zxvf distribute-0.6.49.tar.gz cd distribute-0.6.49/ sudo python setup.py install sudo easy_install pip |
1 |
sudo pip install -y virtualenv virtualenvwrapper |
提示pip为未找到命令
好吧,这样来搞 (是因为需要配置环境变量,不然找不到pip)
1 2 3 |
/usr/local/python2.7/bin/pip install virtualenv /usr/local/python2.7/bin/pip install virtualenvwrapper |
配置环境变量
1 |
vim /etc/profile |
在最后加入下面内容
1 2 3 4 5 6 7 8 9 10 11 |
PYTHON_HOME=/usr/local/python2.7/bin/ WORKON_HOME=$HOME/.virtualenvs # 放所有虚拟环境的地方 PROJECT_HOME=$HOME/dev # 放所有项目的地方 export PATH=$PATH:$PYTHON_HOME:$WORKON_HOME:$PROJECT_HOME 保存退出 source /usr/local/python2.7/bin/virtualenvwrapper.sh |
创建虚拟环境
mkvirtualenv demo1
workon
切换到环境deactivate
注销当前环境lsvirtualenv
列出所有环境rmvirtualenv
删除环境cpvirtualenv
复制环境cdsitepackages
cd到当前环境的site-packages
目录lssitepackages
列出当前环境中site-packages
内容setvirtualenvproject
绑定现存的项目和环境wipeenv
清除环境内所有第三方包
=========================================================
virtualenv可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。
一、安装virtualenv
virtualenv实际上是一个python包,所以我们可以用easy_install或者pip安装。下面介绍在CentOS系统上的安装方法。
easy_install安装方式:
1 2 |
[root@localhost ~]# yum install python-setuptools python-devel [root@localhost ~]# easy_install virtualenv |
pip安装方式:
1 2 |
[root@localhost ~]# easy_install pip [root@localhost ~]# pip install virtualenv |
yum安装方式(epel源):
1 |
[root@localhost ~]# yum install python-virtualenv |
二、创建python虚拟环境
使用virtualenv命令创建python虚拟环境:virtualenv [虚拟环境名称]。
1 2 3 |
[root@localhost ~]# virtualenv env1 New python executable in env1/bin/python Installing setuptools, pip...done. |
执行后,在本地会生成一个与虚拟环境同名的文件夹。
如果你的系统里安装有不同版本的python,可以使用–python参数指定虚拟环境的python版本:
1 2 3 4 5 |
[root@localhost ~]# virtualenv --python=/usr/local/python-2.7.8/bin/python2.7 env1 Running virtualenv with interpreter /usr/local/python-2.7.8/bin/python2.7 New python executable in env1/bin/python2.7 Also creating executable in env1/bin/python Installing setuptools, pip...done. |
实测默认情况下虚拟环境不会依赖系统环境的global site-packages。比如系统环境里安装了MySQLdb模块,在虚拟环境里import MySQLdb会提示ImportError。如果想依赖系统环境的第三方软件包,可以使用参数–system-site-packages。此外,也可使用virtualenvwrapper的toggleglobalsitepackages命令控制当前环境是否使用global site-packages。
1 2 3 |
[root@localhost ~]# virtualenv --system-site-packages env1 New python executable in env1/bin/python Installing setuptools, pip...done. |
三、启动虚拟环境
进入虚拟环境目录,启动虚拟环境,如下:
1 2 3 4 |
[root@localhost ~]# cd env1/ [root@localhost env1]# source bin/activate (env1)[root@localhost env1]# python -V Python 2.7.8 |
此时命令行前面会多出一个括号,括号里为虚拟环境的名称。以后easy_install或者pip安装的所有模块都会安装到该虚拟环境目录里。
四、退出虚拟环境
退出虚拟环境:deactivate
1 2 |
(env1)[root@localhost env1]# deactivate [root@localhost env1]# |
五、使用virtualenvwrapper
virtualenvwrapper是virtualenv的扩展工具,可以方便的创建、删除、复制、切换不同的虚拟环境。
1.安装virtualenvwrapper
1 |
[root@localhost ~]# easy_install virtualenvwrapper |
或者:
1 |
[root@localhost ~]# pip install virtualenvwrapper |
创建一个文件夹,用于存放所有的虚拟环境:
1 |
[root@localhost ~]# mkdir ~/workspaces |
设置环境变量,把下面两行添加到~/.bashrc里。
1 2 |
[root@localhost ~]# export WORKON_HOME=~/workspaces [root@localhost ~]# source /usr/bin/virtualenvwrapper.sh |
然后就可以使用virtualenvwrapper了。
2.创建虚拟环境:mkvirtualenv [虚拟环境名称]
1 2 3 4 5 6 7 |
[root@localhost ~]# mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools, pip...done. (env1)[root@localhost ~]# mkvirtualenv env2 New python executable in env2/bin/python Installing setuptools, pip...done. (env2)[root@localhost ~]# |
注意:mkvirtualenv可以使用virtualenv的参数,比如–python来指定python版本。创建虚拟环境后,会自动切换到此虚拟环境里。虚拟环境目录都在WORKON_HOME里。
3.列出虚拟环境:lsvirtualenv -b
1 2 3 |
(env2)[root@localhost ~]# lsvirtualenv -b env1 env2 |
4.切换虚拟环境:workon [虚拟环境名称]
1 2 3 |
(env2)[root@localhost ~]# workon env1 (env1)[root@localhost ~]# echo $VIRTUAL_ENV /root/workspaces/env1 |
5.查看环境里安装了哪些包:lssitepackages
6.进入当前环境的目录:cdvirtualenv [子目录名]
1 2 3 4 5 6 |
(env1)[root@localhost ~]# cdvirtualenv (env1)[root@localhost env1]# pwd /root/workspaces/env1 (env1)[root@localhost env1]# cdvirtualenv bin (env1)[root@localhost bin]# pwd /root/workspaces/env1/bin |
进入当前环境的site-packages目录:cdsitepackages [子目录名]
1 2 3 4 5 6 |
(env1)[root@localhost env1]# cdsitepackages (env1)[root@localhost site-packages]# pwd /root/workspaces/env1/lib/python2.6/site-packages (env1)[root@localhost site-packages]# cdsitepackages pip (env1)[root@localhost pip]# pwd /root/workspaces/env1/lib/python2.6/site-packages/pip |
7.控制环境是否使用global site-packages:toggleglobalsitepackages
8.复制虚拟环境:cpvirtualenv [source] [dest]
1 2 3 |
[root@localhost ~]# cpvirtualenv env1 env3 Copying env1 as env3... (env3)[root@localhost ~]# |
9.退出虚拟环境:deactivate
10.删除虚拟环境:rmvirtualenv [虚拟环境名称]
1 2 |
[root@localhost ~]# rmvirtualenv env2 Removing env2... |
virtualenv环境下pip安装flask出错解决:
1 2 3 4 5 6 7 8 |
(venv)[root@bjsa flasky]# pip install flask /home/sean/workspaces/flasky/venv/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Collecting flask /home/sean/workspaces/flasky/venv/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Using cached Flask-0.10.1.tar.gz setuptools must be installed to install from a source distribution |
提示没有安装setuptools, 下载setuptools安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@bjsa sean]# wget https://bootstrap.pypa.io/ez_setup.py [root@bjsa sean]# python ez_setup.py Downloading https://pypi.python.org/packages/source/s/setuptools/setuptools-17.1.1.zip Extracting in /tmp/tmpEl2VgC Now working in /tmp/tmpEl2VgC/setuptools-17.1.1 Installing Setuptools Traceback (most recent call last): File "setup.py", line 21, in <module> exec(init_file.read(), command_ns) File "<string>", line 11, in <module> File "/tmp/tmpEl2VgC/setuptools-17.1.1/setuptools/__init__.py", line 11, in <module> from setuptools.extension import Extension File "/tmp/tmpEl2VgC/setuptools-17.1.1/setuptools/extension.py", line 8, in <module> from .dist import _get_unpatched File "/tmp/tmpEl2VgC/setuptools-17.1.1/setuptools/dist.py", line 18, in <module> from setuptools import windows_support File "/tmp/tmpEl2VgC/setuptools-17.1.1/setuptools/windows_support.py", line 2, in <module> import ctypes File "/usr/local/lib/python2.7/ctypes/__init__.py", line 10, in <module> from _ctypes import Union, Structure, Array ImportError: /usr/local/lib/python2.7/lib-dynload/_ctypes.so: undefined symbol: PyUnicodeUCS2_FromEncodedObject Something went wrong during the installation. See the error message above. |
google(http://effbot.org/pyfaq/when-importing-module-x-why-do-i-get-undefined-symbol-pyunicodeucs2.htm)后得知:
The only way to solve this problem is to use extension modules compiled with a Python binary built using the same size for Unicode characters.
于是下载源码:
1 2 3 4 |
(venv)[root@bjsa sean]# wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz (venv)[root@bjsa sean]# tar zxvf setuptools-0.6c11.tar.gz (venv)[root@bjsa setuptools-0.6c11]# python setup.py install (venv)[root@bjsa setuptools-0.6c11]# pip install flask |
安装成功
Centos升级Python 2.7.12并安装最新pip
注:最新版为2.7.14, 请把以下所有的2.7.12替换为2.7.14
Centos系统一般默认就安装有Python2.6.6版本,不少软件需要2.7以上的,通过包管理工具安装不了最新的版本,通过源码编译可以方便安装指定版本,只需要把下面版本的数字换成你想要的版本号。
1.安装步骤
下载源码
1 |
wget http://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz |
在下载目录解压源码
1 |
tar -zxvf Python-2.7.12.tgz |
进入解压后的文件夹
1 |
cd Python-2.7.12 |
在编译前先在/usr/local建一个文件夹python2.7.12
(作为python的安装路径,以免覆盖老的版本,新旧版本可以共存的)
1 |
mkdir /usr/local/python2.7.12 |
,编译前需要安装下面依赖,否则下面安装pip就会出错
1 |
yum install openssl openssl-devel zlib-devel gcc -y |
安装完依赖后执行下面命令
1 |
vim ./Modules/Setup.dist |
找到#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉注释并保存(即去掉井号)
在解压缩后的目录下编译安装
1 2 3 |
./configure --prefix=/usr/local/python2.7.12 --with-zlib make make install |
此时没有覆盖老版本,再将原来/usr/bin/python
链接改为别的名字
1 |
mv /usr/bin/python /usr/bin/python2.6.6 |
再建立新版本python的软链接
1 |
ln -s /usr/local/python2.7.12/bin/python2.7 /usr/bin/python |
这个时候输入
python
就会显示出python的新版本信息
Python 2.7.12 (default, Oct 13 2016, 03:17:14)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
2.修改yum配置文件
之所以要保留旧版本,因为yum依赖Python2.6,改下yum的配置文件,指定旧的Python版本就可以了。
vim /usr/bin/yum
,将第一行的#!/usr/bin/python
修改成#!/usr/bin/python2.6.6
3.安装最新版本的pip
1 2 |
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py |
找到pip2.7的路径
1 |
find / -name "pip*" |
上面的命令输出
/root/.cache/pip
这里省略一堆输出
/usr/local/python2.7.12/bin/pip
/usr/local/python2.7.12/bin/pip2
/usr/local/python2.7.12/bin/pip2.7 #就是这个
/usr/bin/pip
/usr/bin/pip2
/usr/bin/pip2.6
为其创建软链作为系统默认的启动版本(之前有旧版本的话就先删掉rm -rf /usr/bin/pip
)
1 |
ln -s /usr/local/python2.7.12/bin/pip2.7 /usr/bin/pip |
看下pip的版本
1 |
pip -V |
pip 8.1.2 from /usr/local/python2.7.12/lib/python2.7/site-packages (python 2.7)
pip安装完毕,现在可以用它下载安装各种包了
我把上面的所有写成下面简单的脚本,一键就可以升级好。
1 |
wget http://7xpt4s.com1.z0.glb.clouddn.com/update-python2.7.12.sh && bash update-python2.7.12.sh |
1 Comment
cbd oil for cats · 07/07/2020 at 12:02 PM
hello there and thank you for your info – I
have definitely picked up something new from right here.
I did however expertise several technical points using this web site,
since I experienced to reload the website lots of times previous to I could get it to load properly.
I had been wondering if your web host is OK? Not that I am complaining,
but slow loading instances times will sometimes affect your placement in google and can damage your high quality score if advertising and marketing with Adwords.
Well I am adding this RSS to my email and can look out for a lot
more of your respective exciting content. Make sure
you update this again very soon.