How to build a single python file from multiple scripts?

前端 未结 8 1762
终归单人心
终归单人心 2020-12-24 01:30

I have a simple python script, which imports various other modules I\'ve written (and so on). Due to my environment, my PYTHONPATH is quite long. I\'m also using Python 2.

8条回答
  •  眼角桃花
    2020-12-24 02:08

    I've come up with a solution involving modulefinder, the compiler, and the zip function that works well. Unfortunately I can't paste a working program here as it's intermingled with other irrelevant code, but here are 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)
    

提交回复
热议问题