Ok, I did it. It is a bit hacky, but it works great for my use.
Its essence is to use ModuleFinder to find all imported modules, filter out any system modules, compile them, and pin them.
Unfortunately, my code for this is studded with additional complications that have nothing to do with this issue, so I canโt insert a working program, just some fragments:
zipfile = ZipFile(os.path.join(dest_dir, zip_name), 'w', ZIP_DEFLATED) sys.path.insert(0, '.') finder = ModuleFinder() finder.run_script(source_name) for name, mod in finder.modules.iteritems(): filename = mod.__file__ if filename is None: continue if "python" in filename.lower(): continue subprocess.call('"%s" -OO -m py_compile "%s"' % (python_exe, filename)) zipfile.write(filename, dest_path)
source share