【问题标题】:Using Numpy creates a tcl folder when using py2exe使用 py2exe 时使用 Numpy 创建一个 tcl 文件夹
【发布时间】:2014-01-26 00:20:52
【问题描述】:

在我的 Python 程序上使用 py2exe 时,我得到了一个可执行文件,还有一个 tcl\ 文件夹。

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

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


test.py

import numpy

print 'hello'

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)

【问题讨论】:

    标签: python numpy py2exe


    【解决方案1】:

    用于确定依赖关系的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 
    

    【讨论】:

    • 感谢您的回答。更一般地说:即使使用软使用 Tkinter,您认为使用py2exe 时可以避免使用tcl/ 文件夹吗?
    • 我不确定我使用的是cx_freeze 而不是py2exe,它没有“捆绑到一个exe”选项。从理论上讲,您应该能够打包所有文件,甚至是 exe 中 Tkinter 所需的 /tcl 目录。
    • 应该有bundle_files 选项可用于py2exe。可能对此有所帮助。
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多