xorsyst xorsyst - 1 month ago 14
Python Question

Cross-platform alternative to py2exe

py2exe is great, and I use it whenever I want to package up a python program to run on a Windows system.

My question is, is there an equivalent tool that I can use to package up the program on Windows, but that I can then run on Linux?

Answer

Ok, I've done this. It's a little hacky, but it works very well for my use case.

The gist of it is to use ModuleFinder to find all imported modules, filter out any system ones, compile them and zip them up.

Unfortunately my code for this is littered with additional complications that don't have any relevance to this question, so I can't paste a working program, just some snippets:

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)