Python 编码折磨是一个必经过程吗?

2014-09-13 09:02:33 +08:00
 4gShell
跟django文档里的第一个学习项目在创建完初始的目录结构修改settings.py连接数据库的参数后,运行
python manage.py syncdb出现下边的错误提示,下去买包烟希望能有惊喜答复。

E:\django_Site\mysite>python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\core\managemen
t\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\core\managemen
t\__init__.py", line 354, in execute
django.setup()
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\__init__.py",
line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\apps\registry.
py", line 108, in populate
app_config.import_models(all_models)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\apps\config.py
", line 197, in import_models
self.models_module = import_module(models_module_name)
File "G:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\contrib\auth\m
odels.py", line 40, in <module>
class Permission(models.Model):
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\models\base
.py", line 125, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\models\base
.py", line 300, in add_to_class
value.contribute_to_class(cls, name)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\models\opti
ons.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length(
))
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\__init__.py
", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\utils.py",
line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\utils.py",
line 108, in load_backend
return import_module('%s.base' % backend_name)
File "G:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "G:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\db\backends\my
sql\base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb2 in position 20: invalid
start byte

E:\django_Site\mysite>
16232 次点击
所在节点    Python
54 条回复
ericls
2014-09-13 09:11:26 +08:00
换linux
4gShell
2014-09-13 09:13:16 +08:00
@ericls 有过这样的想法,但是问题不解决就很纠结...
leopardwei
2014-09-13 09:16:42 +08:00
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb2 in position 20: invalid start byte

显示 MySQL 配置报错,具体是 utf8 编码错误,贴出你的 setting。
skyline75489
2014-09-13 09:20:14 +08:00
我感觉这个更像是Windows的问题,Python的跨平台性已经很好了,但是Windows下还是偶尔会出些问题
hslx111
2014-09-13 09:22:07 +08:00
编程还是不要用windows 最好,可以避免很多环节问题。
当然python的编码确实很蛋疼
no13bus
2014-09-13 09:23:05 +08:00
mysql的数据库编码为utf8 generial
kongkongyzt
2014-09-13 09:24:15 +08:00
即使是在Linux下也被Python的utf8编码折磨过很多次的人幽幽地说,是的
ygmpkk
2014-09-13 09:26:36 +08:00
windows还有有件比较恶心的事情就,当注册表的目录键有中文的时候,python的http请求会报编码错误
4gShell
2014-09-13 09:29:42 +08:00
@leopardwei
这是MySQL status
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
这是项目下的settings.py文件里截取的,会是下面LANGUAGE_CODE的设置的错误吗?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'django_test',
'USER': 'root',
'PASSWORD': '123456'
}
}
1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Aisa/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
leopardwei
2014-09-13 09:33:27 +08:00
把所有编码统统设为 utf-8,比如操作系统、数据库、编辑器、浏览器,省心简单,:-)
leopardwei
2014-09-13 09:37:22 +08:00
LANGUAGE_CODE 在 syncdb 的时候还没用上呢,浏览器访问时,HTTP 协议头中的 Accept-Language 用。
vicalloy
2014-09-13 09:46:09 +08:00
很可能你只是忘了在文件头上设置
# -*- coding: UTF-8 -*-
onlyice
2014-09-13 09:49:49 +08:00
联系用 IDE 在抛异常处看看堆栈和产量值,应该很好定位问题
leopardwei
2014-09-13 09:53:08 +08:00
@vicalloy
有可能,windows 默认编码是 GBK。python2.x 在文件头不显示指定的话,随系统设定了。

没在 windows 下编写过代码,经验欠缺。
onlyice
2014-09-13 09:53:57 +08:00
另外,MySQLdb 库装的路径对么,是不是给你装在一个中文目录里了,然后你手动把它的路径加到 sys.path 中?
4gShell
2014-09-13 10:02:15 +08:00
插了三根烟,跪拜了,改了大家上面提到的点,但是就是not work,现在要砸电脑了
arbipher
2014-09-13 10:05:51 +08:00
http://stackoverflow.com/questions/10838016/unicodedecodeerror-utf8-codec-cant-decode-byte

"If you are using Notepad++ make sure the "encoding" (in the menu) of all your files is set to "UTF-8"."

试试?
zjgsamuel
2014-09-13 10:06:33 +08:00
这才刚开始就准备放弃的节奏啦? 好好折腾吧~~
运行平台 编码 甚至代码缩进 这些都得注意哦
kafkakevin
2014-09-13 10:13:23 +08:00
Windows 上的问题不用解决。
服务器端的程序,换 Linux。
4gShell
2014-09-13 10:15:44 +08:00
@zjgsamuel 没有哦,只是找不到问题出在哪的那种心情很...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/133203

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX