之前一直在Ubuntu环境下进行Django开发,最近将开发工作转移到CentOS的过程中,发现Django开发环境的搭建过程很是不顺,现将Django安装过程以及相关问题的解决方法罗列如下。
升级Python2.6.6到Python2.7.9
由于CentOS6.5自带的Python版本为2.6.6,该版本不支持Django,因此需要将Python升级到2.7以上。同时,由于yum中最新的Python版本也是2.6.6,所以只能通过源码方式安装Python2.7.9。
查看Python版本:
$ python -V
Python 2.6.6
安装编译Python需要的包
注:可以编译Python源码不通过再安装相应的包,也可先全部安装了再说,以免有后顾之忧。建议全部安装,简单粗暴。
$ yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
下载Python2.7.9源码
$ mkdir /opt/python2.7.9
$ cd /opt/python2.7.9
$ wget –no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
$ tar xf Python-2.7.9.tar.xz
编译安装
$ cd /opt/python2.7.9/Python-2.7.9
$ ./configure –prefix=/usr/local
$ make
$ make install
创建新的python软链接
$ ln -s /usr/local/bin/python2.7 /usr/bin/python
查看Python版本:
$ python -V
Python 2.7.9
python软链接指向python2.7版本后,yum不能正常工作。将yum使用的python修改为2.6。
查看yum路径:
$ which yum
/usr/bin/yum
#!/usr/bin/python2.6
安装python包管理工具pip
$ yum install python-pip
有可能遇到如下问题。
问题1,执行pip找不到pkg_resources:
$ pip
Traceback (most recent call last):
File “/usr/bin/pelican-quickstart”,line 5,in \< module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
解决方法:
$ yum install python-devel
问题2,执行pip报错:
[root@donkey site-packages]# pip
Traceback (most recent call last):
File “/usr/bin/pip”,line 7,in
from pip import main
ImportError: No module named pip
原因:
因为yum使用的是python2.6,执行yum install python-pip会将pip安装到python2.6的site-packages目录中。运行pip命令调用的是python2.7,而python2.7的默认包导入路径不会有python2.6的site-packages。
查看python包导入路径:
$ [root@donkey site-packages]# python
Python 2.7.9 (default,Mar 8 2017,08:47:48)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type “help”,“copyright”,“credits” or “license” for more information.
>>> import sys
>>> sys.path
[”,‘/usr/local/lib/python27.zip’,‘/usr/local/lib/python2.7’,‘/usr/local/lib/python2.7/plat-linux2’,‘/usr/local/lib/python2.7/lib-tk’,‘/usr/local/lib/python2.7/lib-old’,‘/usr/local/lib/python2.7/lib-dynload’,‘/usr/local/lib/python2.7/site-packages’]
>>>
解决方法:
将python2.6的site-packages目录加入到pip的包导入路径中。
文件/usr/bin/pip中,添加:
sys.path.append(‘/usr/lib/python2.6/site-packages’)
[root@donkey site-packages]# cat /usr/bin/pip
#!/usr/bin/python# -- coding: utf-8 --
import re
import sys
sys.path.append(‘/usr/lib/python2.6/site-packages’)
from pip import mainif __name__ == ‘__main__’:
sys.argv[0] = re.sub(r’(-script.pyw|.exe)?$’,”,sys.argv[0])
sys.exit(main())
[root@donkey site-packages]#
安装Django
建议安装Django 1.8版本,Django1.8之后很多命令发生了更改,而网上大多数教程均是针对1.8的版本。
$ pip install Django==1.8
验证安装结果,查看Django版本,成功显示版本则证明Django安装成功!
[root@donkey bin]# python
Python 2.7.9 (default,11:08:26)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type “help”,“credits” or “license” for more information.
>>> import django
>>> django.VERSION
(1,8,‘final’,0)
>>>
一键安装脚本
根据该博文一步步操作基本能够完成CentOS 6.5的Django安装,但是你需要按顺序敲命令,等待一个命令执行完之后再敲另外一个,费时费力,效率很低。现将以上安装、配置步骤写成一个脚本,脚本执行时,你可抽身做其他事情,一段时间后确认安装结果即可。
脚本源码
#!/bin/sh
# File name: CentOS-Django-Install.sh
# This script is used to install Django 1.8 on CentOS 6.5 automatically.
# Author: Arvin(mikepetermessidona@hotmail.com) twitter(@Messi_Arvin)
# Date: Mar.09.2017
# Usage: Save this file as CentOS-Django-Install.sh.
# $ chmod u+x CentOS-Django-Install.sh
# $ su root
# # ./CentOS-Django-Install.sh
# Check result:
# [root@donkey opt]# python
# Python 2.7.9 (default,Mar 9 2017,10:20:35)
# [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
# Type "help","copyright","credits" or "license" for more information.
# >>> import django
# >>> django.VERSION
# (1,'final',0)
# >>>
#
DIR_PYTHON="/opt/python2.7.9"
CMD_PYTHON="/usr/bin/python"
echo -e "\e[1;33m[INFO]:\e[0m DIR_PYTHON: "$DIR_PYTHON""
echo -e "\e[1;33m[INFO]:\e[0m CMD_PYTHON: "$CMD_PYTHON""
function Django_Install_Func(){
#Install python packages compiling python source code.
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
if [ ! -d $DIR_PYTHON ]; then
mkdir $DIR_PYTHON
echo -e "\e[1;33m[INFO]:\e[0m Directory "$DIR_PYTHON" is created!"
else
echo -e "\e[1;31m[ERROR]:\e[0m Directory "$DIR_PYTHON" existed! Please define a new directory to save python2.7.9 source code."
exit 1
fi
echo -e "\e[1;33m[INFO]:\e[0m Start to upgrade Python!"
cd $DIR_PYTHON
wget --no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
tar xf Python-2.7.9.tar.xz
cd $DIR_PYTHON/Python-2.7.9
./configure --prefix=/usr/local
make && make install
if [ -f $CMD_PYTHON ]; then
rm $CMD_PYTHON
fi
ln -s /usr/local/bin/python2.7 $CMD_PYTHON
echo -e "\e[1;33m[INFO]:\e[0m Start to modify yum!"
CMD_YUM=`which yum`
echo -e "\e[1;33m[INFO]:\e[0m CMD_YUM: "$CMD_YUM""
LINE_YUM=`awk '/#!\/usr\/bin\/python/{print NR}' /usr/bin/yum`
sed -i "$LINE_YUM c #!/usr/bin/python2.6" $CMD_YUM
yum install python-devel
yum install python-pip
echo -e "\e[1;33m[INFO]:\e[0m Start to modify pip!"
CMD_PIP=`which pip`
echo -e "\e[1;33m[INFO]:\e[0m CMD_PIP: "$CMD_PIP""
LINE_PIP=`awk '/python2.6\/site-packages/{print NR}' "$CMD_PIP"`
if [ ! $LINE_PIP ]; then
sed -i "/import sys/a\sys.path.append('/usr/lib/python2.6/site-packages')" $CMD_PIP
echo -e "\e[1;33m[INFO]:\e[0m Append python2.6/site-packages!"
else
echo -e "\e[1;33m[INFO]:\e[0m python2.6/site-packages existed!"
fi
echo -e "\e[1;33m[INFO]:\e[0m Start to install Django!"
pip install Django==1.8
echo -e "\e[1;33m[INFO]:\e[0m Django is installed!"
}
PYTHON_VERSION=$(python -V 2>&1 >/dev/null)
echo -e "\e[1;33m[INFO]:\e[0m PYTHON_VERSION: "$PYTHON_VERSION""
if [[ "Python 2.6.6" == $PYTHON_VERSION ]]; then
Django_Install_Func
else
echo -e "\e[1;31m[ERROR]:\e[0m Current python version is: $PYTHON_VERSION,please modify this script."
fi
github: CentOS-Django-Install.sh
使用方法
复制以上代码,保存为CentOS-Django-Install.sh
给予执行权限:
$ chmod u+x CentOS-Django-Install.sh
使用root用户运行:
$ su root
# ./CentOS-Django-Install.sh