2010-08-01 87 views
0

我跑幫助轉換的.py到.EXE,使用py2exe

# p2e_simple_con.py 
# very simple script to make an executable file with py2exe 
# put this script and your code script into the same folder 
# run p2e_simple_con.py 
# it will create a subfolder 'dist' where your exe file is in 
# has the same name as the script_file with extension exe 
# (the other subfolder 'build' can be deleted) 
# note: with console code put a wait line at the end 

from distutils.core import setup 
import py2exe 
import sys 

sys.argv.append("py2exe") 
sys.argv.append("-q") 

# this is your code script file, change it as needed 
# use it in the working directory or give full path 
script_file = 'DateTime_Test1.py' 

# create a single .exe file and use compression 
# (the .exe file is 30% larger with no compression) 
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1,}}, 
    zipfile = None, 
    # replace console with windows for a GUI program 
    console = [{'script': script_file}] 
) 

腳本我試過,但有以下錯誤

C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'console' 
    warnings.warn(msg) 
C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'zipfile' 
    warnings.warn(msg) 
usage: py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 
    or: py2exe.py --help [cmd1 cmd2 ...] 
    or: py2exe.py --help-commands 
    or: py2exe.py cmd --help 

error: invalid command 'py2exe' 
+0

你有沒有經歷過這個教程... http://www.py2exe.org/index.cgi/Tutorial?highlight =((Tutorial)) – 2010-08-01 03:30:39

回答

1

的線條:

sys.argv.append("py2exe") 
sys.argv.append("-q") 

似乎「不尋常」,並可能導致「無效命令」消息。我很想知道擁有它們的理由。

更新

一個通常的規則是命名腳本setup.py(雖然在這種情況下,它的命名p2e_simple_con.py似乎)。在它的setup函數解析的命令行,通常你希望用一個命令,如運行它:

python setup.py py2exe 

其中py2exe是,py2exe包能夠理解的命令,然後生成EXE文件。所以只要安裝了py2exe軟件包,就應該接受該命令。

sys.argv.append("py2exe")補充說,命令的命令行,所以你的情況,你就應該能夠鍵入:

python p2e_simple_con.py 

打造的exe。但是,如果您尚未安裝py2exe軟件包,則基址distutils將無法​​識別py2exe命令。但是在那種情況下,我希望你得到import py2exe這一行的例外。所以,我不確定發生了什麼事。

用來運行安裝程序的命令是什麼?

+0

我在另一個論壇上看到了代碼,並且不確定自己。 – rectangletangle 2010-08-01 04:23:03

0

這只是一個想法(不是真的答案,但我不能評論)。

由於從錯誤消息看來有一個給出的參數py2exe它不接受,所以我猜想它是-q參數。因此,請嘗試刪除sys.argv.append("-q")行,然後運行腳本。

我沒有使用py2exe我自己所以我不能真正測試這個。這只是一個想法。

-1

這兩個警告是由於選項字典中的錯誤逗號,在最終值之後。你想擁有

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1}}, 
    zipfile = None, 
    # replace console with windows for a GUI program 
    console = [{'script': script_file}] 
) 

試試看看能否解決你的錯誤。

0

的問題可能是py2exe是蟒蛇3,

一個爲蟒2似乎是py2exe2msi

我也越來越類似的錯誤在Python 2,試圖做的代碼,這是爲python 3版本編寫。