前言
近日,需要搭建一下centos的生产环境顺便学学python,于是就有了安装centos及升级python的举动
第一步
请先将urllib升级一下才安装python2.7,否则会报https没办法打开的错误,因为centos的相关模块本来就没办法打开https,编译python以后一样不可以,所以先升级urllib然后编译python2.7才是正途。
Django站点使用django_cas接入SSO(单点登录系统),配置完成后登录,抛出“urlopen error unknown url type: https”异常。寻根朔源发现是python内置的urllib模块不支持https协议。
import urllib
urllib.urlopen(‘http://www.baidu.com‘)
>
urllib.urlopen(‘https://www.baidu.com‘)
Traceback (most recent call last):
File “”,line 1,in
File “/usr/local/python27/lib/python2.7/urllib.py”,line 86,in urlopen
return opener.open(url)
File “/usr/local/python27/lib/python2.7/urllib.py”,line 204,in open
return self.open_unknown(fullurl,data)
File “/usr/local/python27/lib/python2.7/urllib.py”,line 216,in open_unknown
raise IOError,(‘url error’,‘unknown url type’,type)
IOError: [Errno url error] unknown url type: ‘https’
之所以python内置的urllib模块不支持https协议是因为编译安装python之前没有编译安装类似于openssl这样的SSL库,以至于python不支持SSL
因为我用的是Centos系统所以安装openssl-devel
sudo yum install openssl-devel
之后重新编译Python
./configure(可选,因为之前已经配置过,按之前的配置来就行了,而且最好按之前的配置配编译安装以免依赖的库需要重新编译安装。)
make
make install
import urllib
urllib.urlopen(‘https://www.baidu.com‘)
没有再报同样的错误。
在安装完openssl-devel后重新编译python前也有说需要编辑Modules文件夹内Setup.dist文件的
修改
Socket module helper for SSL support; you must comment out the other
socket line above,and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I
(SSL)/include−I
(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
为
# Socket module helper for SSL support; you must comment out the other
socket line above,and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I
-L$(SSL)/lib -lssl -lcrypto
但实际测试下来好像并不需要修改这个文件,编译的时候能自动将SSL库编译进python中。
另外需要特别注意的是,重新编译安装python后,通过可执行文件名(可能是个连接文件)运行python可能运行的还是老的python,这是因为可执行文件名没有连接到新的python可执行程序。因此要用最新的python可执行文件名或指向该名字的连接来运行python。
重新编译安装python后有可能导致需要重新编译django,MysqLdb,pycrypto,python-ldap,django-auth-ldap,django_cas,pymongo等一些列依赖python的模块。这里要特别注意
第二步
转载:
CENTOS 6.X 系列默认安装的 Python 2.6 ,目前开发中主要是使用 Python 2.7 ,这两个版本之间还是有不少差异的,程序在 Python 2.6 下经常会出问题。
比如: re.sub 函数 ,2.7 支持 flags 参数,而 2.6 却不支持。
所以,打算安装 Python 2.7 来运行 Flask 应用程序,但 2.6 不能删除,因为系统对它有依赖。
1、安装 sqlite-devel
因为 Flask 应用程序可能使用能 sqlite 数据库,所以这个得装上(之前因为没装这个,导致 Python 无法导入 sqlite3 库。
当然,也可以从源码编译安装。
yum install sqlite-devel -y
2、安装 Python 2.7
wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar xf Python-2.7.8.tgz
cd Python-2.7.8
./configure –prefix=/usr/local
make && make install
安装成功之后,你可以在 /usr/local/bin/python2.7 找到 Python 2.7。
3、安装 setuptools + pip
这里需要注意,一定要使用 python2.7 来执行相关命令。
First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
Then install it for Python 2.7 :
python2.7 ez_setup.py
Now install pip using the newly installed setuptools:
easy_install-2.7 pip
With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install –upgrade [packagename]
pip2.7 uninstall [packagename]
4、使用 virtualenv
Install virtualenv for Python 2.7 and create a sandBox called my27project:
pip2.7 install virtualenv
virtualenv-2.7 my27project
Check the system Python interpreter version:
python –version
This will show Python 2.6.6
Activate the my27project sandBox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python –version
This will show Python 2.7.X
deactivate
基本就是这些了,网上很多教程都说要做软链接,但我感觉那样做或多或少会对系统有一些未知的影响。这个方法能尽量保持系统的完整性,很多自带 Python 程序其实在头部都指定了 #!/usr/bin/python ,所以它们用的其实是 Python 2.6 ,而不是新安装的 Python 2.7 。
原文:http://digwtx.duapp.com/54.html
参考: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
使用virtualenv出现errno95
在/mnt/hgfs/shared下面,使用virtualenv命令时候出现:
errno 95的错误。
于是查到:
Using virtualenv in VMWare with shared Windows folder
up vote
3
down vote
favorite
I currently use a linux VM in VMWare running on a Windows machine. I have a shared Windows folder for my projects that I access in linux via /mnt/hgfs/
When I try to use virtualenv for my python projects I get an 'Operation not supported' error:
joe@myserver:/mnt/hgfs/winwww/envtest# virtualenv env
Traceback (most recent call last):
File "/usr/local/bin/virtualenv",line 9,in <module>
load_entry_point('virtualenv==1.7','console_scripts','virtualenv')()
File "/usr/local/lib/python2.6/dist-packages/virtualenv.py",line 928,in main
never_download=options.never_download)
File "/usr/local/lib/python2.6/dist-packages/virtualenv.py",line 1029,in create_environment
site_packages=site_packages,clear=clear))
File "/usr/local/lib/python2.6/dist-packages/virtualenv.py",line 1164,in install_python
copyfile(join(stdlib_dir,fn),join(lib_dir,fn))
File "/usr/local/lib/python2.6/dist-packages/virtualenv.py",line 430,in copyfile
copyfileordir(src,dest)
File "/usr/local/lib/python2.6/dist-packages/virtualenv.py",line 405,in copyfileordir
shutil.copytree(src,dest,True)
File "/usr/lib/python2.6/shutil.py",line 173,in copytree
raise Error,errors
shutil.Error: [('/usr/lib/python2.6/config/libpython2.6.so','env/lib/python2.6/config/libpython2.6.so','[Errno 95] Operation not supported')]
I'm assuming this is due to Windows not being able to handle symbolic links because I can create my virtualenvs in other directories.
Has anyone worked out a way to make virtualenv work in shared Windows folders? I edit my code on my Windows machine which is why I use shared folders.
Thanks.
The virtualenv does not need to be located next to your code,so even if you can't create it within your shared folder this shouldn't affect your ability to edit the code on windows and run it on your host platform. It sounds to me like a bad idea anyway to attempt to share platform-specific files with multiple platforms.
This is the usefulness of the .pth files and if you're using setuptools with a setup.py within your project you can just $VENV/python setup.py develop which will link your project into the virtualenv,wherever it may be located. You can then run $VENV/python and your package will be in the sys.path and available for import.