﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-路的尽头-文章分类-python</title><link>http://www.cppblog.com/tangxinfa/category/7571.html</link><description>开发者之旅</description><language>zh-cn</language><lastBuildDate>Sat, 18 Feb 2012 16:21:38 GMT</lastBuildDate><pubDate>Sat, 18 Feb 2012 16:21:38 GMT</pubDate><ttl>60</ttl><item><title>python tips</title><link>http://www.cppblog.com/tangxinfa/articles/python_tips.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Mon, 09 Jan 2012 10:20:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/python_tips.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/163884.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/python_tips.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/163884.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/163884.html</trackback:ping><description><![CDATA[１，ip地址类型转换<br />    import struct<br />    import socket<br />    struct.unpack('!I', socket.inet_aton('192.168.102.0'))<br />    socket.inet_ntoa(struct.pack('!I', 3232261632))<br />２，默认版本为python3的系统上临时切换回python2<br />   ln -s /usr/bin/python2 ./python<br />   export PATH=$PWD:$PATH<br />   接下来执行的python脚本将是python2<br /><img src ="http://www.cppblog.com/tangxinfa/aggbug/163884.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2012-01-09 18:20 <a href="http://www.cppblog.com/tangxinfa/articles/python_tips.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQLdb utf-8 tips</title><link>http://www.cppblog.com/tangxinfa/articles/python_mysqldb_utf8_tips.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Thu, 28 Apr 2011 17:22:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/python_mysqldb_utf8_tips.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/145269.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/python_mysqldb_utf8_tips.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/145269.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/145269.html</trackback:ping><description><![CDATA[
		<ul>
				<li>做好四件事：</li>
		</ul>
１，python源代码保存为utf-8<br />２，数据库建成utf-8<br />３，mysql连接设置为utf-8<br />４，查询結果中的文本字段是unicode的，转回utf-8。<br /><br /><ul><li>总结性的示例代码：</li></ul><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;"> 1</span> <span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/env python</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 2</span> <span style="color: #008000; font-weight: bold; font-style: italic; text-decoration: underline;">#-*- coding: <span style="color: #ff0000;">utf-8</span> -*-</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 3</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> MySQLdb<br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"></span><span style="color: #0000ff;">if</span><span style="color: #000000;"> </span><span style="color: #800080;">__name__</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">__main__</span><span style="color: #800000;">'</span><span style="color: #000000;">:<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    mysql </span><span style="color: #000000;">=</span><span style="color: #000000;"> MySQLdb.connect(host</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">localhost</span><span style="color: #800000;">'</span><span style="color: #000000;">, user</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">root</span><span style="color: #800000;">'</span><span style="color: #000000;">, passwd</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">123456</span><span style="color: #800000;">'</span><span style="color: #000000;">, <span style="font-weight: bold; font-style: italic; text-decoration: underline; color: #ff0000;">charset</span></span><span style="color: #ff0000; font-weight: bold; font-style: italic; text-decoration: underline;">='utf8'</span><span style="color: #000000;">)<br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    cursor </span><span style="color: #000000;">=</span><span style="color: #000000;"> mysql.cursor()<br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    cursor.execute(</span><span style="color: #800000;">'</span><span style="color: #ff0000; font-weight: bold; font-style: italic; text-decoration: underline;">SET NAMES UTF8</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">10</span> <span style="color: #000000;">    sql </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">DROP DATABASE IF EXISTS mysqldb_utf8_test</span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">11</span> <span style="color: #000000;">    cursor.execute(sql)<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;">    sql </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">CREATE DATABASE mysqldb_utf8_test <span style="font-weight: bold; font-style: italic; text-decoration: underline; color: #ff0000;">DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci</span></span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">13</span> <span style="color: #000000;">    cursor.execute(sql)<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;">    mysql </span><span style="color: #000000;">=</span><span style="color: #000000;"> MySQLdb.connect(host</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">localhost</span><span style="color: #800000;">'</span><span style="color: #000000;">, user</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">root</span><span style="color: #800000;">'</span><span style="color: #000000;">, passwd</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">123456</span><span style="color: #800000;">'</span><span style="color: #000000;">, db</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">mysqldb_utf8_test</span><span style="color: #800000;">'</span><span style="color: #000000;">, <span style="color: #ff0000; text-decoration: underline; font-style: italic; font-weight: bold;">charset</span></span><span style="color: #ff0000; text-decoration: underline; font-style: italic; font-weight: bold;">='utf8'</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">15</span> <span style="color: #000000;">    cursor </span><span style="color: #000000;">=</span><span style="color: #000000;"> mysql.cursor()<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;">    cursor.execute(</span><span style="color: #800000;">'</span><span style="color: #ff0000; font-weight: bold; font-style: italic; text-decoration: underline;">SET NAMES UTF8</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;">    sql </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">CREATE TABLE utf8_table(key_field VARCHAR(32) NOT NULL, value_field VARCHAR(255) NOT NULL)</span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">18</span> <span style="color: #000000;">    cursor.execute(sql)<br /></span><span style="color: #008080;">19</span> <span style="color: #000000;">    key </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">tangxinfa</span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">20</span> <span style="color: #000000;">    value </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">好人一个</span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">21</span> <span style="color: #000000;">    sql </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">INSERT INTO utf8_table VALUES("%s", "%s")</span><span style="color: #800000;">'</span><span style="color: #000000;">%</span><span style="color: rgb(0, 0, 0);">(key, value)</span><span style="color: #000000;"></span><br /><span style="color: #008080;">22</span> <span style="color: rgb(0, 0, 0);">    cursor.execute(sql)       </span><span style="color: #000000;">#注意某些旧版本的mysql（如4.1.22以下），mysql.character_set_name()总是返回latin1，会引起乱码，需要改为</span>cursor.execute(<span style="color: #800000;">'</span><span style="color: #800000;">INSERT INTO utf8_table VALUES("%s", "%s")</span><span style="color: #800000;">', </span><span style="color: #000000;"></span><span style="color: rgb(0, 0, 0);">(key, value)</span><span style="color: #000000;"></span>)<br /><span style="color: #008080;">23</span> <span style="color: #000000;">    sql </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">select * from utf8_table</span><span style="color: #800000;">'</span><span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;">    cursor.execute(sql)<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> record </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> cursor.fetchall():<br /></span><span style="color: #008080;">26</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> item </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> record:<br /></span><span style="color: #008080;">27</span> <span style="color: #000000;">            </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> item.encode(</span><span style="color: #ff0000; font-weight: bold; font-style: italic; text-decoration: underline;">'utf8'</span><span style="color: #000000;">)</span></div><br /><ul><li>参考</li></ul><a>http://mysql-python.sourceforge.net/MySQLdb.html</a><br /><a href="http://bbs.phpchina.com/viewthread.php?tid=13861" style="color: #000000;">http://bbs.phpchina.com/viewthread.php?tid=13861</a><br />http://hi.baidu.com/ak456/blog/item/c318502394aa20569922ed7b.html<br /><br /><br /><img src ="http://www.cppblog.com/tangxinfa/aggbug/145269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2011-04-29 01:22 <a href="http://www.cppblog.com/tangxinfa/articles/python_mysqldb_utf8_tips.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>没有root权限怎么安装python的模块</title><link>http://www.cppblog.com/tangxinfa/articles/python_package_install_anywhere.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Wed, 15 Sep 2010 04:59:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/python_package_install_anywhere.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/126651.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/python_package_install_anywhere.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/126651.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/126651.html</trackback:ping><description><![CDATA[可以将包安装在任意目录，在python2.4下将MySQLdb安装在~/opt目录中．<br /><div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mkdir </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">p ${HOME}</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">opt</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">lib</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">python2.</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">site</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">packages</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);"><br />echo </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">PYTHONPATH=\$PYTHONPATH:\${HOME}/opt/lib/python2.4/site-packages/</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">~/</span><span style="color: rgb(0, 0, 0);">.bashrc<br />echo </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">export PYTHONPATH</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">~/</span><span style="color: rgb(0, 0, 0);">.bashrc<br />echo </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">export PATH=\$PATH:\${HOME}/opt/bin</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">~/</span><span style="color: rgb(0, 0, 0);">.bashrc<br />source </span><span style="color: rgb(0, 0, 0);">~/</span><span style="color: rgb(0, 0, 0);">.bashrc<br />easy_install </span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">prefix</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">${HOME}</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">opt MySQL</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">python</span></div><br /><img src ="http://www.cppblog.com/tangxinfa/aggbug/126651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2010-09-15 12:59 <a href="http://www.cppblog.com/tangxinfa/articles/python_package_install_anywhere.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>emacs的shell/eshell下执行python脚本无法实时显示输出的原因及解决办法</title><link>http://www.cppblog.com/tangxinfa/articles/57758.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Fri, 01 Aug 2008 06:44:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/57758.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/57758.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/57758.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/57758.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/57758.html</trackback:ping><description><![CDATA[问题：<br>　　在emacs(我用的是ntemacs23)的shell或eshell下，通过python test.py方式执行python脚本，<br>该脚本的标准输出（通过print语句）不会马上显示，而是在该脚本执行完毕时才显示，从而导致用户无法获<br>知脚本的运行状态．<br>原因：<br>　　参考http://coding.derkeiler.com/Archive/Python/comp.lang.python/2003-12/2978.html<br>解决方法：<br>　　加-u参数．<br>　　如：python -u test.py<br><br><br><img src ="http://www.cppblog.com/tangxinfa/aggbug/57758.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2008-08-01 14:44 <a href="http://www.cppblog.com/tangxinfa/articles/57758.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>web.py分析</title><link>http://www.cppblog.com/tangxinfa/articles/56306.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Wed, 16 Jul 2008 07:47:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/56306.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/56306.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/56306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/56306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/56306.html</trackback:ping><description><![CDATA[这里只分析web.py应用以web服务器方式运行时的主要处理流程．<br>&nbsp;&nbsp;&nbsp; 主线程创建的web服务对象（类CherryPyWSGIServer的实例）创建数个（默认为10）工作线程对象及一个连接队列．web服务对象接收来自客户端的连接，然后将该连接对象放到连接队列中，工作线程从连接队列中取走一个连接对象，然后为这个客户http连接服务（处理http）．这是典型的生产者－消费者模式．<br><br><br><br><img src ="http://www.cppblog.com/tangxinfa/aggbug/56306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2008-07-16 15:47 <a href="http://www.cppblog.com/tangxinfa/articles/56306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>不一样的python－函数默认值</title><link>http://www.cppblog.com/tangxinfa/articles/56271.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Wed, 16 Jul 2008 01:57:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/56271.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/56271.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/56271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/56271.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/56271.html</trackback:ping><description><![CDATA[首先看例子：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;modify_default_value(param</span><span style="color: #000000;">=</span><span style="color: #000000;">[]):<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;param.append(</span><span style="color: #800000;">'</span><span style="color: #800000;">value</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;param<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">modify_default_value()<br></span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">modify_default_value()</span></div>
输出：<br>['value']<br>['value', 'value']<br>为什么第二输出会与第一行输出不一样？有点违反直觉，不是吗？<br>在python中参数默认<span style="font-weight: bold;">值</span>存储在函数对象里，当这个值是个可修改对象（mutable object）时（如：list，dictionary），函数中对默认参数值的修改会彻底地改变这个函数的参数默认值，这也就是为什么第二次调用modify_default_value输出会不一样了．为了更好地理解函数参数默认值，试试从函数外部探查一下函数的参数默认值：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;modify_default_value.func_defaults</span></div>
输出：<br>['value', 'value']<br><br><br><br> <img src ="http://www.cppblog.com/tangxinfa/aggbug/56271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2008-07-16 09:57 <a href="http://www.cppblog.com/tangxinfa/articles/56271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PyInstaller打包第三方模块adodb</title><link>http://www.cppblog.com/tangxinfa/articles/56115.html</link><dc:creator>唐新发</dc:creator><author>唐新发</author><pubDate>Mon, 14 Jul 2008 08:58:00 GMT</pubDate><guid>http://www.cppblog.com/tangxinfa/articles/56115.html</guid><wfw:comment>http://www.cppblog.com/tangxinfa/comments/56115.html</wfw:comment><comments>http://www.cppblog.com/tangxinfa/articles/56115.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/tangxinfa/comments/commentRss/56115.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/tangxinfa/services/trackbacks/56115.html</trackback:ping><description><![CDATA[上次用python的adodb模块写了个访问access数据库的程序，发布时想打包成单独的一个exe文件，完成这个任务的工具是PyInstaller，我试过用它打包一些小python程序，一次就成功了，但adodb就不行了，似乎没办法将adodb模块打包进去．通过查看手册，总算明白是怎么回事了，PyInstaller无法获知通过非常手段导入的模块，<br>如<span style="font-family: monospace;">调用</span><tt class="docutils literal"><span class="pre">__import__，exec，eval函数．PyInstaller对于这种情况会在warn*.txt日志中留下一句警告：<span style="font-family: sans-serif;">W: delayed&nbsp; __import__ hack detected at line 0 - adodb (D:\Program Files\Python25\Lib\site-packages\adodb\adodb.pyc)<br>这句警告说明了adodb.py中通过非常手段导入了一个未知模块．通过查看adodb.py发现它确实是通过__import__导入了一个模块：adodb_odbc．<br>解决该问题的步骤如下，在PyInstaller根目录的hooks写一个名为hook-adodb.py的文件内容为：<br>hiddenimports = ['adodb_odbc']<br>在这个目录上可以发现PyInstaller对于诸多已知模块已经内置提供了相应的hook文件．<br>接下来删除打包所生成的一些文件，再次执行打包过程，OK．<br><br>一些感想：对于非python项目，可以尝试用Python进行快速原型</span></span></tt><tt class="docutils literal"><span class="pre"><span style="font-family: sans-serif;">开发</span></span></tt><tt class="docutils literal"><span class="pre"><span style="font-family: sans-serif;">，如验证算法等，当项目期限将至时，转化为其它语言的时间不够时，可用PyInstaller将这个Python程序转化为exe，然后将这个exe分发给最终用户，这样对已有的系统布署不会造成过大的冲击．<br><br><br></span></span></tt> <img src ="http://www.cppblog.com/tangxinfa/aggbug/56115.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/tangxinfa/" target="_blank">唐新发</a> 2008-07-14 16:58 <a href="http://www.cppblog.com/tangxinfa/articles/56115.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>