Skip to main content

Convenience functions related to modules and importing.

Project description

Convenience functions related to modules and importing.

Latest release 20200521:

  • New import_module_from_file function to import a Python file as a module instance.
  • New direct_imports(src_filename,module_name=None) returning the set of directly imports module names.

Function direct_imports(src_filename, module_name=None)

Crudely parse src_filename for import statements. Return the set of directly imported module names.

If module_name is not None, resolve relative imports against it. Otherwise, relative import names are returned unresolved.

This is a very simple minded source parse.

Function import_module_from_file(module_name, source_file, sys_path=None)

Import a specific file as a module instance, return the module instance.

Parameters:

  • module_name: the name to assign to the module
  • source_file: the source file to load
  • sys_path: optional list of paths to set as sys.path for the duration of this import; the default is the current value of sys.path

Note that this is a "bare" import; the module instance is not inserted into sys.modules.

Warning: sys.path is modified for the duration of this function, which may affect multithreaded applications.

Function import_module_name(module_name, name, path=None, lock=None)

Import module_name and return the value of name within it.

Parameters:

  • module_name: the module name to import.
  • name: the name within the module whose value is returned; if name is None, return the module itself.
  • path: an array of paths to use as sys.path during the import.
  • lock: a lock to hold during the import (recommended).

Function module_attributes(M)

Generator yielding the names and values of attributes from a module which were defined in the module.

Function module_files(M)

Generator yielding .py pathnames involved in a module.

Function module_names(M)

Return a list of the names of attributes from a module which were defined in the module.

Release Log

Release 20200521:

  • New import_module_from_file function to import a Python file as a module instance.
  • New direct_imports(src_filename,module_name=None) returning the set of directly imports module names.

Release 20190101: New functions: module_names, module_attributes.

Release 20160918:

  • New generator function module_files yielding pathnames.
  • import_module_name: accept name=None, just return the module.
  • Add empty "install_requires" for DISTINFO completeness.

Release 20150116: Initial PyPI release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.py.modules-20200521.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file cs.py.modules-20200521.tar.gz.

File metadata

  • Download URL: cs.py.modules-20200521.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for cs.py.modules-20200521.tar.gz
Algorithm Hash digest
SHA256 3997c3073e3b65668fc783c526215d4c71deb14a460c86fd7f035b77980c9bda
MD5 25518d50a97429f36368e166f2f999aa
BLAKE2b-256 c7cd040c69dcd660c2d964cf74bb8f70e8997f286eef632fdcbdba1bff035d6b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page