显示标签为“Python”的博文。显示所有博文
显示标签为“Python”的博文。显示所有博文

Py2exe Bad file Descriptor

0 评论  

       前段时间用python做的上传工具,是用wxPython做UI的,再结合py2exe打包成直接可以运行的exe,虽然打包的时候遇到了乱码的问题,但误打乱撞的也解决了这个问题,不过经验告诉我们,用一个新的东西的时候,问题总是会有的,果然,后来一直遇到一个很奇怪的问题,代码在IDE里面运行得好好的,打包成exe后就经常会报一个 error, bad file descriptor的错误。 而且最离奇的就是这个错误好像是随机出现的,一会有,一会又没有,google一下这个错误,它本身的意思就是打开文件或者读写文件的时候出错了,类似共享冲突,锁 之类。


       这个问题困扰了我很久,我也尝试过很多种方法,后来偶然一次把日志输出的功能关掉,问题暂时得到了解决,但也只是出现的频率少了,还是会出现,真是吐血,关键是搞了这么久也不知道是啥原因啊。。。


       今天又Google了一下,不经意间看到一行文字[Errno 9] Bad file descriptor. 39, #. 40, # In other words, after printing a certain number of bytes to the。。。。 突然想起,难道是因为打包过后 print 函数输出的东西没地方去了,原本是到标准输出的,现在是GUI了,没有命令行窗口了,就会出错了? 一念及此,立马把所有的print都注释掉,再重新打包,运行,呵呵,还真是这个问题


       看来编程序不能马虎啊,当初为省力,一开始没配日志模块,全用print了,方便,也就是祸根了。 另一方面,要想很快的解决问题,多学点基础,原理才是正理啊

Python点滴

0 评论  

最近在用wxPython做一个小工具,给公司内部用,Python的确很简洁,Java Swing比wxPython可是要麻烦多了,不过还是碰到一些问题,都是一说就明白的问题,只是我写这个工具也是东拼西凑的找的别人的代码,好写代码而不求胜解,才会碰到这些问题了。如下:

  • wxPython关于RadioBox事件处理的Bug
wxPython里面的UI控件很丰富,刚开始画UI的时候我就加了一个RadioBox在wxFrame上,按照Swing里面的理解,Frame才是 正牌的窗口,我也就跟着用Frame了,wxPython里面的事件处理机制也是比较简单的,只需要定义好一个组件的事件ID,再把这个事件ID关联到一 个事件处理函数即可,但是,RadioBox却始终没有触发事件,一开始我以为是我代码的问题,就仔细和原有的例子代码做比较,没找到什么地方有问题,后 来我把页面上的其它控件全部注释掉,只剩下光杆的RadioBox,还是没有事件产生,,,只好Google一把,发现有人说wxRadioBox有 bug,在Frame里面不会产生事件的Bug,真晕啊,后来把Frame页面的控件移到一个Panel里面,事件的问题就OK了。
  • python格式化当前时间
import time print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 做参数的格式化字符串可以用Unicode,可以写中文, 像这样:u'%Y年-%m月-%d日 %H:%M:%S'
  • Example 8.12. locals 是只读的,globals 不是
DiveIntoPython里的原话: Example :. def foo(arg): x = 1 print locals() (1) locals()["x"] = 2 (2) print "x=",x (3) z = 7 print "z=",z foo(3) globals()["z"] = 8 (4) print "z=",z (5) (1) 因为使用 因为使用 因为使用 3 来调用 来调用 foo,会打印出 ,会打印出 ,会打印出 {'arg': 3, 'x': 1}。这个应该没什么奇怪的。这个应该没什么奇怪的。这个应该没什么奇怪的。这个应该没什么奇怪的。这个应该没什么奇怪的。这个应该没什么奇怪的。 这个应该没什么奇怪的(2) locals 是一个返回 是一个返回 是一个返回 是一个返回 dictionary 的函数,这里您在 的函数,这里您在 的函数,这里您在 的函数,这里您在 dictionary 中设置了一个值中设置了一个值中设置了一个值中设置了一个值。您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 您可能认为这样会改变局部量 x 的值为 的值为 的值为 2,但并不会。,但并不会。,但并不会。,但并不会。locals 实际上没有实际上没有实际上没有实际上没有返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变返回局部名字空间,它的是一个拷贝。所以对进行改变空间中的变量值并无影响。空间中的变量值并无影响。空间中的变量值并无影响。空间中的变量值并无影响。空间中的变量值并无影响。 (3) 这样会打印出 这样会打印出 这样会打印出 这样会打印出 这样会打印出 x= 1,而不是 ,而不是 ,而不是 x= 2。 (4) 在有了对 locals 的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样的经验之后,您可能认为这样不会 改变 改变 z 的值,但是可以的值,但是可以的值,但是可以的值,但是可以。由于 由于 由于 Python 在实现过程中内部有所区别 在实现过程中内部有所区别 在实现过程中内部有所区别 在实现过程中内部有所区别 在实现过程中内部有所区别 在实现过程中内部有所区别 在实现过程中内部有所区别 (关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,关于这些区别我宁可不去研究,因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解因为我自已还没有完全理解) ,globals 返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是返回实际的全局名字空间,而不是一个拷贝:与 一个拷贝:与 一个拷贝:与 locals 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 的行为完全相反。所以对 globals 所返回的 所返回的 所返回的 所返回的 dictionary - 155 - Dive Into Python http://diveintopython.org/ 的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。的任何改动都会直接影响到全局变量。 (5) 这样会打印出 这样会打印出 这样会打印出 这样会打印出 这样会打印出 z= 8,而不是 ,而不是 ,而不是 z= 7。 之所以注意到这个是因为我在python里面定义的全局变量A=7,直接在代码里改成A=8,后面用到A的时候还是7,后来换成globals()['A'] = 8才OK。
  • repr函数
repr函数用来取得对象的规范字符串表示。反引号(也称转换符)可以完成相同的功能。注意,在大多数时候有eval(repr(object)) == object。 >>> i = [] >>> i.append('item') >>> `i` "['item']" >>> repr(i) "['item']" 基本上,repr函数和反引号用来获取对象的可打印的表示形式。你可以通过定义类的__repr__方法来控制你的对象在被repr函数调用的时候返回的内容。 这个是在自定义异常里面发现的,我要抛出一个自定义的异常,按照不知道什么地方来的代码,在异常的__str__方法也就是tostring方法里面用到了这个函数,导致我抛出的异常内容本是Unicode,显示出来确是类似"\\u6578"的东西。
  • wx.Sizer怎样才有Padding效果
wxSizer还是比较好定位的,我只用了BoxSizer和FlexGridSizer,不过我发现这两个里面都没有一个明确的地方可以设置padding,FlexGridSizer倒是可以设置gab,不过这时在Sizer里面的,后来才发现sizer的Add方法里面可以指定Border的宽度,和Border的上下左右组合,这样就可以实现padding效果了

【转】Python程序打包成安装包

0 评论  

The McMillan Installer

The mcillian installer development is discontinued.

mirror: http://davidf.sjsoft.com/mirrors/mcmillan-inc/installer_dnld.html

Continued development(not tested yet): http://pyinstaller.hpcf.upr.edu/cgi-bin/trac.cgi

This works on Win32. Unzip the Installer in a directory of your choice, and cd there.

Configure the Installer by running:

python Configure.py

Python must be in your PATH for this to work, if it's not, type from the command prompt:

PATH=%PATH%;c:\python23

where c:\python23 must be replaced with the root of your python installation. Then, assuming the source code is app.py (placed in c:\source):

python Makespec.py --upx --onefile --noconsole c:\source\app.py
python Build.py  app\app.spec

Replace 'app' everywhere above with your application name.

You will end up with app\app.exe under the Installer dir.This is a one file .exe containing all the application. If you don't want a one-file build, suppress the option --onefile above. If you don't have upx installed (or don't want to use it), suppress the option --upx above. The option --noconsole is needed to produce a windows gui application, instead of a console one (so the command shell won't pop up when you run the application).

[ More details to be written... ]

py2exe

http://py2exe.sourceforge.net/

Create a setup.py script for using py2exe to generating the exe file. (The script to compile in this case is wxTail.py):

# setup.py
from distutils.core import setup
import py2exe
setup(name="wxTail",scripts=["wxTail.py"],)

Sample of the win32 command for running py2exe:

python setup.py py2exe

If you use the unicode version of wxpython you have to manually include the file unicows.dll form your python installation directory. Otherwise the application will crash at least on Windows 98.

--GeraldKlix

[ More details to be written... ]

Installer

To turn your binary and accompanying library files into a Windows installer you can use:

Issues

When using py2exe with pythoncom (or with wxPython's ActiveXWrapper which uses pythoncom) py2exe has trouble finding the code for the generated COM wrapper modules and you end up with import errors. Here is the solution, which was sent to the mail list by Clark C. Evans:

  • When pythonwin is called to create an Python binding for a COM component, it generates a new python class and puts this class in the gen_py directory (which magically appears). I believe the problem is that the py2exe program doesn't expand the Python path to include the deployment directory, otherwise this directory would be found... To work-aroud this problem, simply use the makepy program to generate a python wrapper for the activeX component you have (for example, IE Explorer). Then, rename this module file to something more palatable (like ie.py). Then, instead of using "ensure module" simply import the re-named file -- the imported module is the class module. For example, to make the IE ActiveX demo work...
    1. Generate ie.py using makepy using the "-o" option
    2. Replace the "ensure module" stuff in the demo code with...
          from wxPython.wx import *
         if wxPlatform == '__WXMSW__':
             from wxPython.lib.activexwrapper import MakeActiveXClass
             import ie5
             browserModule = ie5
    No other changes seem to be necessary.

Another work-around if you run into issues running py2exe compiled programs with the ActiveXWrapper controls (specifically the PDF Window control in wxPython 2.8) is to add a typelibs entry to the options argument in your setup.py file. See the sample setup.py below for an example.

from distutils.core import setup
import py2exe

manifest = """



myProgram

   
       
   


"""

"""
installs manifest and icon into the .exe
but icon is still needed as we open it
for the window icon (not just the .exe)
changelog and logo are included in dist
"""

setup(

   options = {'py2exe': {
                           'compressed': 1,
                           'optimize': 2,
                           'bundle_files': 3,
                           'typelibs' : [("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)],
                       }
        },


   windows = [
       {
           "script": "myprogram.py",
           "icon_resources": [(1, "program.ico")],
           "other_resources": [(24,1,manifest)],
       }
   ],
     data_files=["help.txt",
                 "program.ico",
                 "program.ini"],

)

Basically the typelibs line ensures py2exe picks up the dynamically imported module in pdfwin.py that is included with the Windows wxPython distribution.

    _browserModule = win32com.client.gencache.EnsureModule(
       "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)

On Linux

Summary

A binary distribution of a Python/wxPython program on Linux has these advantages:

  • There is no need for the user to have Python, wxPython or any other extensions you use installed
  • The versions of Python, wxPython and extensions are the ones you ship, not any they may already have installed. That means you don't need to worry about users using different versions of the extensions with your software.

Prerequisites

cx_Freeze

cx_Freeze turns a Python script into a binary file and any referenced binary modules.

Download cx_Freeze from http://starship.python.net/crew/atuining/ I recommend getting the binary distribution. If you extract it in /opt, then it will end up installed in /opt/cx_Freeze-2.1/ (this location is assumed through the rest of this document).

Also note that while cx_Freeze is free software, you do need to abide by the license agreement on the same page.

chrpath

The wxPython shared libraries have some pathnames hardcoded into them (also known as an rpath). chrpath removes this hardcoded path for where other shared libraries are searched.

Download chrpath from http://freshmeat.net/projects/chrpath/ By default it installs in /usr/local/bin which should be on your path.

Making your binary distribution

Make an output directory

Create a clean directory for the resulting files. I make one named dist.

Run cxFreeze

Assuming the main entrypoint to your program is example.py, this is what you run:

$ cxpath=/opt/cx_Freeze-2.1
$ env PATH=$cxpath:$PATH FreezePython --install-dir=dist --base-binary=$cxpath/ConsoleSetLibPathBase example.py

Everything should end up in the dist subdirectory, and the main binary will be named dist/example

Run chrpath

You now need to remove hard coded path information from the wxPython and other shared libraries. I use this shell script:

for i in *.so
do
   if chrpath $i 2>/dev/null | grep = >/dev/null
   then
       echo "Fixing $i"
       chrpath -d $i
   fi
done

Change into the dist subdirectory and run the script. It should fix at least wxPython.wxc.so, and same other wx-controls you may use (eg calendar).

Package it up

You can now just tar up the dist subdirectory. Users can place the contents anywhere they choose on the filesystem, and call the main executable for everything to work.

I normally put all the files in a subdirectory of /usr/lib (for example: /usr/lib/example-1.0 and then put a wrapper script in /usr/bin that execs the binary in /usr/lib/example-1.0.

On Mac

py2app

http://undefined.org/python/py2app.html

BundleBuilder.py

The Python included with Mac OS X contains a tool called BundleBuilder.py which lets you package Python scripts into ".app" bundles that can be distributed on computers even without Python installed. (Although OS X 10.3 contains a complete implementation of Python.) Documentation on this tool (including an example of a wxPython app building script) can be found here:

http://www.python.org/cgi-bin/moinmoin/BundleBuilder

The important part to note is that you must manually include the wxWindows dynamic libraries at this point. See the lines in the example script containing myapp.libs.append to see how to do so. Hopefully a more targeted and detailed tutorial will be forthcoming. =) In the meantime, please feel free to post your questions to either wxPython-mac@lists.sourceforge.net or pythonmac-sig@python.org .

Examples

BitPim

The bitpim project produces Windows and Linux binary installers that don't require the user to have Python or wxPython installed (or even have to know what they are!)

Code is available in http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/bitpim/bitpim/

makedist.py does the actual build an invokes the various other tools. bitpim.iss is the innosetup file. p2econfig.py is the py2exe file.

原文地址 http://wiki.wxpython.org/index.cgi/CreatingStandaloneExecutables