为什么python 不生成可执行文件

Python04

为什么python 不生成可执行文件,第1张

有了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>解压缩的文件夹里, 而python27python33安装在另外一个路径,环境变量没有设置

操作方式是这样的: 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 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

很早之前,学习Python web编程的时候,就涉及一个Python的urllib。可以用urllib.urlopen("url").read()可以轻松读取页面上面的静态信息。但是,随着时代的发展,也来越多的网页中更多的使用javascript、jQuery、PHP等语言动态生成页面信息。因此,用urllib再去抓取页面HTML就不足以达到我们想要的效果。

回到顶部

解决思路:

有一个思路最为简单的思路可以动态解析页面信息。urllib不可以解析动态信息,但是浏览器可以。在浏览器上展现处理的信息其实是处理好的HTML文档。这为我们抓取动态页面信息提供了很好的思路。在Python中有一个很有名的图形库——PyQt。PyQt虽然是图形库,但是他里面 QtWebkit。这个很实用。谷歌的Chrome和苹果的Safari都是基于WebKit内核开发的,所以我们可以通过PyQt中得QtWebKit 把页面中的信息读取加载到HTML文档中,再解析HTML文档,从HTML文档中提取我们想用得信息。

回到顶部

所需材料:

作者本人实用Mac OS X。应该在Windows和Linux平台也可以采用相同的办法。

1、Qt4 library

Library,而不是Creator。Library在Mac的默认安装路径下,应该是/home/username/Developor/,不要改变Qt4的默认安装路径。否则可能安装失败。

官方网址:http://qt-project.org/downloads

2、SIP、PyQt4

这两个软件可以在在PyQt的官网找到。下载的是它的源码。Mac和Linux需要自己编译。

下载地址是:http://www.riverbankcomputing.co.uk/software/pyqt/download

在终端切换到文件解压后的目录中。

在终端中输入

python configure.py

make

sudo make install

进行安装编译。

SIP和PyQt4两个安装方法相同。但是PyQt4依赖SIP。所以先安装SIP再安装PyQt4

1、2两步完成之后,Python的PyQt4的模块就安装好了。在Python shell中输入import PyQt4看看能不能找到PyQt4的模块。

3、Spynner

spynner是一个QtWebKit的客户端,它可以模拟浏览器,完成加载页面、引发事件、填写表单等操作。

这个模块可以在Python的官网找到。

下载地址: https://pypi.python.org/pypi/spynner/2.5

解压后,cd到安装目录,然后输入sudo python configure.py install安装该模块。

这样Spynner模块就安装完成了,在python shell中试试import spynner看看该模块有没有安装完成。

回到顶部

Spynner的简单使用

Spynner的功能十分强大,但是由于本人能力有限,就介绍一下如何显示网页的源码吧。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#! /usr/bin/python

#-*-coding: utf-8 -*-

import spynner

browser = spynner.Browser()

#创建一个浏览器对象

browser.hide()

#打开浏览器,并隐藏。

browser.load("http://www.baidu.com")

#browser 类中有一个类方法load,可以用webkit加载你想加载的页面信息。

#load(是你想要加载的网址的字符串形式)

print browser.html.encode("utf-8")

#browser 类中有一个成员是html,是页面进过处理后的源码的字符串.

#将其转码为UTF-8编码

open("Test.html", 'w+').write(browser.html.encode("utf-8"))

#你也可以将它写到文件中,用浏览器打开。

browser.close()

#关闭该浏览器

通过这个程序,就可以比较容易的显示webkit处理的页面HTML源码了。