2009-05-25 15 views
7

Non penso che sia stato chiesto prima-ho una cartella che ha un sacco di diversi file .py. La sceneggiatura che ho realizzato utilizza solo alcuni, ma alcuni chiamano altri & Non conosco tutti quelli in uso. Esiste un programma che otterrà tutto il necessario per far sì che lo script venga eseguito in una cartella?Raccogliere tutti i moduli Python utilizzati in una cartella?

Cheers!

risposta

0

Freeze fa molto vicino a quello che descrivi. Fa un ulteriore passo per generare file C per creare un eseguibile autonomo, ma puoi usare l'output del registro che produce per ottenere l'elenco dei moduli che il tuo script usa. Da lì è semplice copiarli tutti in una directory da zippare allo (o qualsiasi altra cosa).

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:]) 
+0

+1 buon lavoro, questo piccolo script può essere molto utile –

6

Utilizzare il modulo modulefinder nella libreria standard, vedere ad es. http://docs.python.org/library/modulefinder.html

+2

+1 è possibile utilizzarlo come uno script del genere: python -m modulefinder myscript.py –

+0

Ottimo punto di Nadia, l'opzione -m è spesso utile davvero. –

Problemi correlati