Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将使用的所有Python模块收集到一个文件夹中?_Python - Fatal编程技术网

是否将使用的所有Python模块收集到一个文件夹中?

是否将使用的所有Python模块收集到一个文件夹中?,python,Python,我认为以前没有人问过这个问题——我有一个文件夹,其中有很多不同的.py文件。我制作的脚本只使用了一些,但有些调用了其他脚本&我不知道所有正在使用的脚本。是否有一个程序可以获取使脚本运行到一个文件夹中所需的所有内容 干杯 与您描述的非常接近。它会额外生成一个C文件来创建一个独立的可执行文件,但是您可以使用它生成的日志输出来获取脚本使用的模块列表。从那里,将它们全部复制到一个要压缩的目录中是一件简单的事情 (或其他任何内容)。使用标准库中的modulefinder模块,例如,请参见+1您可以将其用作

我认为以前没有人问过这个问题——我有一个文件夹,其中有很多不同的.py文件。我制作的脚本只使用了一些,但有些调用了其他脚本&我不知道所有正在使用的脚本。是否有一个程序可以获取使脚本运行到一个文件夹中所需的所有内容

干杯

与您描述的非常接近。它会额外生成一个C文件来创建一个独立的可执行文件,但是您可以使用它生成的日志输出来获取脚本使用的模块列表。从那里,将它们全部复制到一个要压缩的目录中是一件简单的事情
(或其他任何内容)。

使用标准库中的
modulefinder
模块,例如,请参见+1您可以将其用作这样的脚本:python-m modulefinder myscript.py
# 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:])