1. 程式人生 > >如果有python而不能生成win獨立可執行檔案,則要python有什麼用呢

如果有python而不能生成win獨立可執行檔案,則要python有什麼用呢

In this article you will see how one could create an executable of some program written in the Python language. The goal is to make some distribuable executable that will work on other Microsoft Windows systems where Python isn't installed.

The content of this article has been written on and for a Microsoft Windows operating system. It should be doable on a GNU/Linux system but i didn't tested. Anyway, creating an all in one executable binary file of Python code isn't that usual on GNU/Linux systems. GNU/Linux guru's would be able to know the equivalent commands.

There is py2exe which is able to build executable from Python code. However, for PyQT this seems to fail. On the website ofRiverbank, you will see in the 3rd Party Software menu, a link pointing to PyInstaller. Download the PyInstaller zip file, extract it somewhere and place the extracted content where you want. At the time of writting this, i got PyInstaller 1.5.1. PyInstaller isn't an Python module, so it doesn't need to go into the Python's site-packages. Store PyInstaller at a place that is easy to type on the command prompt. Mind to avoid a too deep path and eventual user rights issues. Here i have put PyInstaller on the root of my D:

Then the first time, you need to configure PyInstaller. This should happen once. At least for each Python version you may have. Or happen each time the PyInstaller config change. For example, if you have already configured OyInstaller and move OyInstaller, you need to reconfigure it. Start a command line interface (CTRL+R

 and enter cmd). cd (browse with the command cd) to the location where you stored the PyInstaller and execute (configure) like following:

python Configure.py

Note: If you use multiples versions of Python on the same machine, you should make multiples copies of PyInstaller and name it differently. That to keeps stuff consistent and avoid weird issues. You should then also always start the python executable with it's version number, for example, python2.6 pyinstaller.py or python3.2 pyinstaller.py

Now you can build your Qt application as following. The short way could be (which would be also the way to go in future versions of PyInstaller):

python pyinstaller.py C:\paht\to\qt4_tests\helloworld.py 

This will create a directory called helloworld\dist\helloworld in the directory where your pyinstaller.py is located. You can double click on your exe and it will run your program. All Python code you will build as executable will be stored in the PyIstaller directory tree. I didn't find a way to change that, but it isn't that bad either.

In the long run, it's more wize to create a build config file for you project. We will use the fancybrower QT example now, which is stored in your Python install if you have installed PyQt (../site-packages\PyQt4\examples\webkit\fancybrowser). We copy the fancybrowser data somerwhere and then run:

python Makespec.py --onefile -w d:\python_stuff\fancybrowser\fancybrowser.pyw

The --onefile, like it say, will create one exe file containing all data (dlls and python related stuff). That is probably the most desirable way for Windows users. The -w will make in sort that no black console window is show and only start the main exe. Getting two window, where one is useless is not proffesional at all.

Now we can run the build with:

Build.py fancybrowser\fancybrowser.spec

Which will now produce the fancybrowser exe in D:\pyinstaller-1.5.1\fancybrowser\dist\. Start the exe and it should work! :) BTW, the final exe is about 20,5MB instead of a bunch of files (see first method) with a total size of 54,2MB.

Ofcourse, for more information, check the website of PyInstaller!