将所有使用的Python模块集中到一个文件夹?
我觉得这个问题之前没被问过——我有一个文件夹,里面有很多不同的.py文件。我写的脚本只用到了其中的一部分,但有些脚本会调用其他的,我不知道都用到了哪些。有没有什么程序可以把让这个脚本运行所需的所有文件都放到一个文件夹里呢?
谢谢!
3 个回答
0
Freeze 这个工具的功能跟你说的差不多。它多做了一步,就是生成C语言文件来创建一个独立的可执行文件。不过,你可以利用它生成的日志输出,查看你的脚本使用了哪些模块。接下来,只需要把这些模块都复制到一个文件夹里,然后打包(或者其他处理)就可以了。
6
# 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:])
当然可以!请把你想要翻译的内容发给我,我会帮你把它变得简单易懂。
6
使用标准库中的 modulefinder
模块,具体可以参考这个链接:http://docs.python.org/library/modulefinder.html