2014-01-26 110 views
7

在我的Python程序中使用py2exe時,我得到一個可執行文件,但也得到一個tcl\文件夾。使用Numpy在使用py2exe時創建一個tcl文件夾

這很奇怪,因爲我根本沒有使用tcl/tk,在我的代碼中沒有任何與tkinter相關的東西。

爲什麼導入numpy負責添加這個tcl\文件夾?如何防止這種情況發生?


test.py

import numpy 

print 'hello' 

PY2EXE CODE

from distutils.core import setup 
import py2exe 

setup(script_args = ['py2exe'], windows=[{'script':'test.py'}], options = {'py2exe': {'compressed':1,'bundle_files': 1}}, zipfile = None) 

回答

11

Modulefinder其用於確定依賴模塊被 「混亂」 和叔嗨你需要Tkinter

如果運行下面的腳本...

from modulefinder import ModuleFinder 

finder = ModuleFinder() 
finder.run_script('test.py') 
print finder.report() 

...你會看到發現模塊(縮短):

Name      File 
    ----      ---- 
m BaseHTTPServer   C:\Python27\lib\BaseHTTPServer.py 
m ConfigParser    C:\Python27\lib\ConfigParser.py 
m FixTk      C:\Python27\lib\lib-tk\FixTk.py 
m SocketServer    C:\Python27\lib\SocketServer.py 
m StringIO     C:\Python27\lib\StringIO.py 
m Tkconstants    C:\Python27\lib\lib-tk\Tkconstants.py 
m Tkinter     C:\Python27\lib\lib-tk\Tkinter.py 
m UserDict     C:\Python27\lib\UserDict.py 
m _LWPCookieJar    C:\Python27\lib\_LWPCookieJar.py 
... 

所以,現在我們知道,Tkinter是進口的,但它是不是很有用。該報告沒有顯示什麼是違規模塊。然而,這是足夠的信息通過修改py2exe腳本排除Tkinter

from distutils.core import setup 
import py2exe 

setup(script_args = ['py2exe'], 
     windows=[{'script':'test.py'}], 
     options = {'py2exe': {'compressed':1, 
          'bundle_files': 1, 
          'excludes': ['Tkconstants', 'Tkinter'] 
          }, 
       }, 
     zipfile = None) 

通常是不夠的。如果你仍然好奇哪些模塊是有問題的,ModuleFinder沒什麼幫助。但您可以安裝modulegraph及其依賴altgraph。然後你可以運行下面的腳本,並將輸出重定向到一個HTML文件:

import modulegraph.modulegraph 

m = modulegraph.modulegraph.ModuleGraph() 
m.run_script("test.py") 
m.create_xref() 

您將獲得依賴關係圖,在那裏你會發現:

numpy -> numpy.lib -> numpy.lib.utils -> pydoc -> Tkinter 
+0

謝謝回答。更一般的情況是:即使使用* Tkinter軟*,您是否認爲在使用'py2exe'時可以避免使用'tcl /'文件夾? – Basj

+0

我不確定,因爲我使用'cx_freeze'而不是'py2exe',它沒有「綁定到一個exe」選項。理論上你應該能夠打包所有的文件,甚至在'exe文件裏面'Tkinter'所需的'/ tcl'目錄。 – Fenikso

+1

應該有'bundle_files'選項可用於'py2exe'。可能對此有所幫助。 – Fenikso

相關問題