Collect all Python modules used in one folder?

I don’t think it was asked before - I have a folder with many different .py files. script I used only some, but some call others, and I do not know all the ones used. Is there a program that will get everything you need to make this script run in one folder?

Hooray!

+7
python
source share
3 answers
# 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
source share

Use the modulefinder module in the standard library, see for example http://docs.python.org/library/modulefinder.html

+6
source share

Freeze is pretty close to what you are describing. It takes an extra step to create C files to create a standalone executable, but you can use the log output it produces to get a list of modules used by your script. From there, it's a simple matter to copy them all to a directory that needs to be archived (or something else).

0
source share

All Articles