python可执行程序必须经过_为什么python 不生成可执行文件

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm

from matplotlib.ticker import LinearLocator, FormatStrFormatter

import matplotlib.pyplot as plt

import numpy as np

fig = plt.figure()

ax = fig.gca(projection='3d')

X = np.arange(-5, 5, 0.25)

Y = np.arange(-5, 5, 0.25)

X, Y = np.meshgrid(X, Y)

R = np.sqrt(X**2 + Y**2)

Z = np.sin(R)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,

linewidth=0, antialiased=False)

ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))

ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

经过漫长的(相对较长,跟C++编译一个小project的耗时有一拼)等待之后, 得到一个yourscript的文件夹,下面包含exe及其它文件.

我对一个用matplotlib绘制三维曲面的python代码作了尝试,小小的几行代码转成exe文件之后,就是直奔30MB的小胖子.有得有失.

编译成exe往往期望比脚本的效率高,但是事实可能并不如人意. pyinstaller形式的exe,似乎没有做代码优化,效率不见得比脚本好.

ython/387-create-a-microsoft-windows-7-executable-with-pyinstaller

Create a Microsoft Windows (7) executable with PyInstaller

CREATED ON 06 MARCH 2012

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!

Add comment

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值