在使用py2exe时,使用Numpy会创建一个tcl文件夹

Bas*_*asj 7 python numpy py2exe

py2exe在我的Python程序上使用时,我得到一个可执行文件,但也是一个tcl\文件夹.

这很奇怪,因为我根本不使用tcl/tk,而且tkinter在我的代码中没有任何相关内容.

为什么导入numpy负责添加此tcl\文件夹?如何防止这种情况发生?


test.py

import numpy

print 'hello'
Run Code Online (Sandbox Code Playgroud)

PY2EXE代码

from distutils.core import setup
import py2exe

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

Fen*_*kso 11

Modulefinder用于确定依赖关系的模块会"混淆"并认为您需要Tkinter.

如果您运行以下脚本...

from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('test.py')
print finder.report()
Run Code Online (Sandbox Code Playgroud)

...你会看到找到的模块(缩短):

  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
...
Run Code Online (Sandbox Code Playgroud)

所以现在我们知道它Tkinter是导入的,但它不是很有用.该报告未显示违规模块是什么.但是,Tkinter通过修改py2exe脚本可以排除足够的信息:

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)
Run Code Online (Sandbox Code Playgroud)

通常这就足够了.如果你仍然好奇哪些模块是有问题的,那就ModuleFinder没什么用了.但是你可以安装modulegraph它的依赖altgraph.然后,您可以运行以下脚本并将输出重定向到HTML文件:

import modulegraph.modulegraph

m = modulegraph.modulegraph.ModuleGraph()
m.run_script("test.py")
m.create_xref()
Run Code Online (Sandbox Code Playgroud)

您将获得依赖图,您将在其中找到:

numpy -> numpy.lib -> numpy.lib.utils -> pydoc -> Tkinter 
Run Code Online (Sandbox Code Playgroud)