【发布时间】:2009-05-25 18:31:23
【问题描述】:
我认为以前没有人问过这个问题——我有一个包含许多不同 .py 文件的文件夹。我制作的脚本只使用了一些——但有些调用了其他的,我不知道所有正在使用的脚本。是否有一个程序可以获取使该脚本运行到一个文件夹所需的一切?
干杯!
【问题讨论】:
标签: python
我认为以前没有人问过这个问题——我有一个包含许多不同 .py 文件的文件夹。我制作的脚本只使用了一些——但有些调用了其他的,我不知道所有正在使用的脚本。是否有一个程序可以获取使该脚本运行到一个文件夹所需的一切?
干杯!
【问题讨论】:
标签: python
使用标准库中的modulefinder 模块,参见例如http://docs.python.org/library/modulefinder.html
【讨论】:
# zipmod.py - make a zip archive consisting of Python modules and their dependencies as reported by modulefinder
# To use: cd to the directory containing your Python module tree and type
# $ python zipmod.py archive.zip mod1.py mod2.py ...
# Only modules in the current working directory and its subdirectories will be included.
# Written and tested on Mac OS X, but it should work on other platforms with minimal modifications.
import modulefinder
import os
import sys
import zipfile
def main(output, *mnames):
mf = modulefinder.ModuleFinder()
for mname in mnames:
mf.run_script(mname)
cwd = os.getcwd()
zf = zipfile.ZipFile(output, 'w')
for mod in mf.modules.itervalues():
if not mod.__file__:
continue
modfile = os.path.abspath(mod.__file__)
if os.path.commonprefix([cwd, modfile]) == cwd:
zf.write(modfile, os.path.relpath(modfile))
zf.close()
if __name__ == '__main__':
main(*sys.argv[1:])
【讨论】:
Freeze 与您描述的非常接近。它执行生成 C 文件以创建独立可执行文件的额外步骤,但您可以使用它生成的日志输出来获取脚本使用的模块列表。从那里将它们全部复制到要压缩的目录中是一件简单的事情 (或其他)。
【讨论】: