发布网友 发布时间:2022-04-24 08:18
共1个回答
热心网友 时间:2022-04-19 03:49
有了python27/python33,有了集成开发环境eclipse/pydev, pycharm,安装了各种package (pythonxy我就不试了,太大了)
接下来就是如何生成exe文件,没有python的电脑windows下也能用. 古人云, 如果不能下跪行礼,则要此膝何用? 套用一下,如果不能编译到exe,则要此IDE何用?
支持这种py 到 exe 转换的网上看了有很多, py2exe从2008年就没有人维护了; 剩下还有维护的为数不多的几个之一pyinstaller
如何安装?
安装的时候其实是在windows CMD命令行console窗口里用类似DOS的方式完成的.不是在python的console命令行
从py到exe通过pyinstaller如何转换的英文版:
也是以cmd.exe的console窗口命令行实现.
比如, 在pyinstaller-2.1>解压缩的文件夹里, 而python27;python33安装在另外一个路径,环境变量没有设置
操作方式是这样的: cmd.exe运行起来, cd转到 比如 D:\>packages>pyinstaller-2.1>为当前路径
然后输入:
C:\python27\python.exe pyinstaller.py -w --onefile --icon="my.ico" yourscript.py
增加--icon选项需要自己提供一图标文件my.ico放在pyinstaller-2.1>当前路径下, 连同需要转exe的 yourscript.py
[python] view plain copy
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 mole, 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.pyNote: 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.pyThis 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.pywThe --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.specWhich will now proce 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