您的位置:首页 > 编程语言 > Python开发

Window下配置python2.7+Django

2016-09-12 00:39 393 查看
今天因为某个项目需要在自己的电脑上配置Django服务器做测试用在安装过程中遇到了很多问题,我在这里做一个记录和分享

目标配置

安装项功能
python2.7代码运行环境
Django 1.9.5构建服务器
pip安装python包文件
setuptools安装过程中需要
MySQLdbpython连接数据库的库

过程

安装python

从python官网下载 2.7.12 (2.7中最新的子版本)

- 尽量不要使用.2.7.5等非通用的版本,实测部分版本下库文件本身有bug,无法运行django

直接运行安装程序即可

安装完毕后需要将

C:\Python27\

C:\Python27\Scripts\

这两个路径添加到环境变量的PATH变量中

安装 setuptools

在我们安装库的时候经常会使用

python setup.py install

这条命令,这条命令依赖setuptools.py

直接google/百度可以得到一个安装脚本,本地新建一个get-setuptools.py文件,使用python get-setuptools.py命令 执行脚本就可以完成晚装

安装Pip

pip是和maven wget等工具等效的工具,在云端有一个仓库,我们通过pip模块与仓库交互,实现python库文件的安装删除与版本管理。

在官网下载安装包,解压过后在解压文件的根目录下使用

python setup.py install

命令执行安装过程。

安装Django

在命令行中执行

python -m pip install django==1.9.5

(使用python 指定pip 模块 安装 版本号为1.9.5的django,这个命令很简明,可以适用其他通用库的安装)

安装MySQLdb

在官网下载msi安装包,直接安装下载即可

*注意在安装中可能遇到找不到python27根目录的情况,这可能是因为你是64位的机器缺采用了32位的安装包,https://pypi.python.org/pypi/pip#downloads 这个网址中可以下载对应安装包

其他

在使用过程中可能遇到其他问题

安装过程中出现错误,提示Pip 安装 出现UnicodeEncodeError: ‘ascii’ codec can’t encode characters

属于字符集问题,可以采用下面的方式解决

1. 找到路径\Lib\site-packages

2. 路径下新建文件 sitecustomize.py ,文件内容为:

import sys

reload(sys)

sys.setdefaultencoding(‘utf-8’)

参见http://blog.csdn.net/a542551042/article/details/48676031

运行python manage.py runserver的时候提示.Django RuntimeError: maximum recursion depth exceeded

这个问题可能是因为python版本过来,库文件中存在bug未修复,可以尝试用下述方案解决

To fix the problem replace this (about line 56 in python\Lib\fuctools.py):

convert = {

lt‘: [(‘gt‘, lambda self, other: other < self),

(‘le‘, lambda self, other: not other < self),

(‘ge‘, lambda self, other: not self < other)],

le‘: [(‘ge‘, lambda self, other: other <= self),

(‘lt‘, lambda self, other: not other <= self),

(‘gt‘, lambda self, other: not self <= other)],

gt‘: [(‘lt‘, lambda self, other: other > self),

(‘ge‘, lambda self, other: not other > self),

(‘le‘, lambda self, other: not self > other)],

ge‘: [(‘le‘, lambda self, other: other >= self),

(‘gt‘, lambda self, other: not other >= self),

(‘lt‘, lambda self, other: not self >= other)]

}

to that:

convert = {

lt‘: [(‘gt‘, lambda self, other: not (self < other or self == other)),

(‘le‘, lambda self, other: self < other or self == other),

(‘ge‘, lambda self, other: not self < other)],

le‘: [(‘ge‘, lambda self, other: not self <= other or self == other),

(‘lt‘, lambda self, other: self <= other and not self == other),

(‘gt‘, lambda self, other: not self <= other)],

gt‘: [(‘lt‘, lambda self, other: not (self > other or self == other)),

(‘ge‘, lambda self, other: self > other or self == other),

(‘le‘, lambda self, other: not self > other)],

ge‘: [(‘le‘, lambda self, other: (not self >= other) or self == other),

(‘gt‘, lambda self, other: self >= other and not self == other),

(‘lt‘, lambda self, other: not self >= other)]

}

参见http://stackoverflow.com/questions/16259729/django-python-manage-py-runserver-gives-runtimeerror-maximum-recursion-depth-e

在使用django时提示cannot import name patterns

这个错误是因为django版本与代码的版本不一致,可以通过确认支持该代码的django版本或者修改代码解决。

参见http://stackoverflow.com/questions/22532743/cant-import-patterns-in-django
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django python