Thursday, July 3, 2008

Using py2exe and wxPython on Windows XP

In my opinion, wxPython is practically the most acceptable platform-crossing GUI toolkit. However neither it nor python are shipped with MS Windows. Since we can't expect our users to have python and wxPython installed, py2exe is no doubt a decent deal to make our python applications standalone. I had some trouble on the first trying it out on Windows XP, so I decided to write the solution here in case someone else encounters the same problem. Let me start at the very beginning. First of all you need to make sure everything needed is installed, for that matter, at least python, wxPython and py2exe must be there in our case. The versions of my installed packages are as following: Python: 2.5.2 py2exe: 0.6.6 for win32 python2.5 wxPython: unicode-2.8.7.1 for python 2.5 Just to be safe, you need to install python before the rest of the two packages. Now, I assume you have already tested & debugged your application into a satisfying status. All you need now is to make it standalone. Another assumption here is you know the path where python is installed.(I will use python25\ and emit the previous part in this essay) To achieve that, you need to write a setup.py script, which takes advantage of the method setup in standard module distutils.core. You can find instructions on py2exe's website for this step. Another good resource to learn it is the samples that come with py2exe, they are under the path Python25\Lib\site-packages\py2exe\samples You may want to refer to the python library reference to learn about distutils.core, but, before you get confused (which I did), I need to point out that py2exe added some additional options to the setup mothed, which will probably be all you'll be using. OK, a simplistic, but generally always working example of the setup.py would be:
from distutils.core import setup import py2exe setup(['MainScript.py']) # Replace MainScript.py with your script
Save the setup.py script in your app dictionary. Next thing is to make sure that you can run python in a command terminal. Which requires the path of python to be added in the Environment Variable PATH: 1 From the desktop, right-click My Computer and click properties. 2 In the System Properties window, click on the Advanced tab. 3 In the Advanced section, click the Environment Variables button. 4 Finally, in the Environment Variables window, highlight the path variable in the Systems Variable section and click edit. Add a semicolon to the end if there isn't one, and then your python path. Open a command prompt(by running cmd), go to your app path, run the setup.py with the argument py2exe:
python setup.py py2exe
A lot of information shows up and then you will find two new dictionary in your app path, here you just need dist. If you wonder what's in there, check out the Official FAQ. But you won't find your exe working, because msvcp71.dll is missing, and changing the name of MSVRP71.DLL won't work. I think this is probably a bug of py2exe. So you need to copy 'Python\Lib\site-packages\wx-2.8-msw-unicode\msvcp71.dll' manually here. That's how I made my wxPython application work so far. Farther information is surely welcomed :).

3 comments:

Anonymous said...

On my installation the missing "msvcp71.dll" is in Python\Lib\site-packages\wx-2.8-msw-unicode\wx\msvcp71.dll

Thanks though!

N.

Anonymous said...

The real test is to run your py2exe-generated installer on a machine that has never had python or wxpython installed. I'd be surprised if that worked with your minimalist setup file.

jimscafe said...

Excellent, thank you.
Yes my dll is also in the \wx\ directory.