标签 ‘ python

OS X MySQL-python Reason: image not found

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

/usr/local/mysql/lib/libmysqlclient.18.dylib 你的mysql lib

python 乱码,python 编码问题

s.decode(‘gbk’).encode(‘utf-8′) , 在实际开发中,我发现,这种办法经常会出现异常:
UnicodeDecodeError: ‘gbk’ codec can’t decode bytes in position 69316-69317: illegal multibyte sequence

UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xbb in position 102: invalid start byte

这是因为遇到了非法字符,比如全角空格往往有多种不同的实现方式,\xa3\xa0,或者\xa4\x57,这些字符,看起来都是全角空格,但它们并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在转码的过程中出现了异常。
这样的问题很让人头疼,因为只要字符串中出现了一个非法字符,整个字符串——有时候,就是整篇文章——就都无法转码。

解决办法:
s.decode(‘gbk’, ‘ignore’)
因为decode的函数原型是decode([encoding], [errors=’strict’]),可以用第二个参数控制错误处理的策略,默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。

python添加自定义模块

最简单的办法是用使用python .pth文件来实现。Python 在遍历已知的库文件目录过程中,如果见到一个 .pth 文件,就会将文件中所记录的路径加入到 sys.path 设置中,于是 .pth 文件说指明的库也就可以被 Python 运行环境找到了。

在site-packages/ 目录下放一个 common.pth 文件,文件的内容写:
/Users/Demo/SomeModule

SomeModule 这个目录中的 Python 文件就可以被运行环境找到了,这比调整环境变量或者把库文件本身挪来挪去方便多了。
其实,easy_install 所依赖的 egg 包安装就是靠 site-packages 目录下的 .pth 文件添加对 egg 包的引用实现的。所以修改对应的 .pth 文件内容,就可以实现对 egg 包的卸载。

Django 反向生成 从数据库生成Model

使用Django生成Model

python manage.py inspectdb

python manage.py inspectdb > models.py

就可以生成了
自动产生Django model

class DjangoModel(models.Model):     
    id = models.IntegerField(primary_key=True)     
    action_time = models.DateTimeField()     
    user_id = models.IntegerField()     
    content_type_id = models.IntegerField(null=True, blank=True)     
    object_id = models.TextField(blank=True)     
    object_repr = models.CharField(maxlength=200)     
    action_flag = models.TextField() # This field type is a guess.     
    change_message = models.TextField()     
    class Meta:     
        db_table = 'django_admin_log'  

django syncdb create superusers error

使用django syncdb创建管理后台时出现
You just installed Django’s auth system, which means you don’t have any superusers defined.
Would you like to create one now? (yes/no): yes
line 85, in get_system_username
return getpass.getuser().decode(locale.getdefaultlocale()[1])
TypeError: decode() argument 1 must be string, not None
发现创建用户失败

发现可以手动创建用户

python manage.py shell

from django.contrib.auth.models import User 
u = User.objects.create_user('admin', 'test@test.com', 'adminpwd')
u.is_staff  =  True
u.is_superuser = True
u.save()

scrapy OpenSSL error: command ‘gcc’ failed with exit status 1

在安装scrapy出现了这个错误

OpenSSL/ssl/connection.c: In function ‘ssl_Connection_set_context’:
OpenSSL/ssl/connection.c:289: warning: implicit declaration of function ‘SSL_set_SSL_CTX’
OpenSSL/ssl/connection.c: In function ‘ssl_Connection_get_servername’:
OpenSSL/ssl/connection.c:313: error: ‘TLSEXT_NAMETYPE_host_name’ undeclared (first use in this function)
OpenSSL/ssl/connection.c:313: error: (Each undeclared identifier is reported only once
OpenSSL/ssl/connection.c:313: error: for each function it appears in.)
OpenSSL/ssl/connection.c:320: warning: implicit declaration of function ‘SSL_get_servername’
OpenSSL/ssl/connection.c:320: warning: assignment makes pointer from integer without a cast
OpenSSL/ssl/connection.c: In function ‘ssl_Connection_set_tlsext_host_name’:
OpenSSL/ssl/connection.c:346: warning: implicit declaration of function ‘SSL_set_tlsext_host_name’
error: command ‘gcc’ failed with exit status 1

原来是centos5下pyopenssl 0.13的版本和openssl不兼容
下载补丁
wget https://bugs.launchpad.net/pyopenssl/+bug/845445/+attachment/2666639/+files/pyOpenSSL-0.13.centos5.patch
移动补丁到对应的目录下
mv *.patch pyOpenSSL-0.13
进入目录
cd pyOpenSSL-0.13
patch -p1 < pyOpenSSL-0.13.centos5.patch 安装带补丁的pyOpenSSL完成后,再安装scrapy就成功了

linux 安装 python Django mysql-python

安装python

下载python

http://www.python.org/

./configure –prefix=/home/admin/python

指定安装目录

安装django

https://www.djangoproject.com/download/

 

下载后解压进入目录

python setup.py install

安装mysql-python

你需要gcc,setuptools,mysql,zlib

详情可见readme

http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/

阅读全文

return top