Yo estaba el otro día tratando de hacer cosas de shell scripting con python (como parte de un setup.py monstruo) y me molestaba que en shell es muy fácil hacer esto:
cdfoo
bar-baz
cd-
O esto:
pushdfoo
bar-baz
popd
O esto:
(cdfoo&&bar-baz)
Y en python tenia que hacer esto, que es largo y feo:
I have written about this in the past, with the general conclusion being "it's a pain in the ass".
So, now, here is how it's done.
Start with a working PyQt application. In this example, I will use devicenzo.py mostly because:
It is a working PyQt application.
It uses a big chunk of PyQt
It's easy to test
Now you need a setup.py. Here's one that works, with extensive commments.
# We will be using py2exe to build the binaries.# You may use other tools, but I know this one.fromdistutils.coreimportsetupimportpy2exe# Now you need to pass arguments to setup# windows is a list of scripts that have their own UI and# thus don't need to run in a console.setup(windows=['devicenzo.py'],options={# And now, configure py2exe by passing more options;'py2exe':{# This is magic: if you don't add these, your .exe may# or may not work on older/newer versions of windows."dll_excludes":["MSVCP90.dll","MSWSOCK.dll","mswsock.dll","powrprof.dll",],# Py2exe will not figure out that you need these on its own.# You may need one, the other, or both.'includes':['sip','PyQt4.QtNetwork',],# Optional: make one big exe with everything in it, or# a folder with many things in it. Your choice# 'bundle_files': 1,}},# Qt's dynamically loaded plugins and py2exe really don't# get along.data_files=[('phonon_backend',['C:\Python27\Lib\site-packages\PyQt4\plugins\phonon_backend\phonon_ds94.dll']),('imageplugins',['c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qgif4.dll','c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qjpeg4.dll','c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qsvg4.dll',]),],# If you choose the bundle above, you may want to use this, too.# zipfile=None,)
Run python setup.py py2exe and get a dist folder full of binary goodness.
And that's it. Except of course, that's not it.
What this will do is create a binary set, either a folder full of things, or a single EXE file. And that's not enough. You have to consider at least the following:
Put everything in resource files: images, qss files, icons, etc. Every file your app needs? Put it in a resource file and load it from there. That way you don't have to care about them if you go the "one exe" road.
Compile .ui files to .py (same reason)
Figure out if you use Qt's plugins, and make them work. This includes: using Phonon, using QtSQL, and using any image formats other than PNG.
After you have that, are you done? NO!
Your windows user will want an installer. I am not going to go into details, but I had a good time using BitRock's InstallBuilder for Qt. It's a nice tool, and it works. That's a lot in this field.
But is that all? NO!
You have to take care of the Visual Studio Runtime. My suggestion? Get a copy of the 1.1MB vcredist_x86.exe (not the larger one, the 1.1MB one), and either tell people to install it manually, or add it to your
installer. You are legally allowed (AFAIK) to redistribute that thing as a whole. But not what's in it (unless you have a VS license).
And we are done? NO!
Once you run your app "installed", if it ever prints anything to stderr, you will get either a dialog telling you it did, or worse (if you are in aything newer than XP), a dialog telling you it can't write to a log file, and the app will never work again.
This is because py2exe catches stderr and tries to save it on a logfile. Which it tries to create in the same folder as the binary. Which is usually not allowed because of permissions.
Solution? Your app should never write to stderr. Write an excepthook and catch that. And then remove stderr or replace it with a log file, or something. Just don't let py2exe do it, because the way py2exe does it is broken.
And is that it?
Well, basically yes. Of course you should get 4 or 5 different versions of windows to test it on, but you are pretty much free to ship your app as you wish. Oh, mind you, don't upload it to downloads.com because they will wrap your installer in a larger one that installs bloatware and crap.
importbottleimportdisqusapiasdisqusimportjsonshortname='magicmisteryforum'api=disqus.DisqusAPI(open("key").read().strip())@bottle.route('/',method='GET')defindex():msg=bottle.request.GET.get('msg','')threads=api.forums.listThreads(forum=shortname,limit=100)printthreads[0]returnbottle.template('main.tpl',threads=threads,shortname=shortname,msg=msg)@bottle.route('/new',method='POST')defnew():title=bottle.request.forms.get('title',None)ifnottitle:bottle.redirect('/?msg=Missing%20Thread%20Name')returnthread=api.threads.create(forum=shortname,title=title)thread_id=thread.__dict__['response']['id']# Redirecting to /thread/thread_id doesn't work# because threads take a few seconds to appear on the listingbottle.redirect('/')@bottle.route('/thread/:id')defthread(id):t=api.threads.details(thread=id)returnbottle.template('thread.tpl',shortname=shortname,id=id,thread=t.__dict__['response'])@bottle.route('/static/:path#.+#')defserver_static(path):returnbottle.static_file(path,root='./static')app=bottle.app()app.catchall=False#Now most exceptions are re-raised within bottle.bottle.run(host='184.82.108.14',port=80,app=app)
Por supuesto que hay un poquito de templates, acá está main.tpl y thread.tpl. Como apesto para el HTML, usa Bluetrip CSS y es sencillo de customizar.
POR SUPUESTO QUE HAGO TRAMPA!
Esta cosa es apenas una capa de pintura encima de Disqus! Más un blog sin posts pero con comentarios que un foro! Pero... qué le falta para ser un foro de verdad? Funciona, no? Hasta se podrían usar categorías de Disqus para crear subforos...
Teniendo todo en cuenta, creo que es un hack bonito.
Y si esperás unos días, esto lleva a otra cosa que es mucho más mágica...