Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gather all Python modules used into one folder?

Tags:

python

I don't think this has been asked before-I have a folder that has lots of different .py files. The script I've made only uses some-but some call others & I don't know all the ones being used. Is there a program that will get everything needed to make that script run into one folder?

Cheers!

like image 746
Solihull Avatar asked May 25 '09 18:05

Solihull


People also ask

What is __ all __?

Python __all__ is a variable that can be set in the __init__.py file of a package. The __all__ variable is a list of strings that defines those symbols that are imported when a program does.

How can you import all functions from Python file in a previous directory?

In order to import a module, the directory having that module must be present on PYTHONPATH. It is an environment variable that contains the list of packages that will be loaded by Python. The list of packages presents in PYTHONPATH is also present in sys. path, so will add the parent directory path to the sys.

Where are all Python modules stored?

Usually the Python library is located in the site-packages folder within the Python install directory, however, if it is not located in the site-packages folder and you are uncertain where it is installed, here is a Python sample to locate Python modules installed on your computer.

What does __ init __ py do in Python?

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.


Video Answer


2 Answers

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

like image 112
Alex Martelli Avatar answered Oct 05 '22 17:10

Alex Martelli


# 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:])
like image 29
Dave Avatar answered Oct 05 '22 16:10

Dave