{"id":15654316,"url":"https://github.com/niklasrosenstein/python-localimport","last_synced_at":"2025-04-30T22:49:56.352Z","repository":{"id":30235144,"uuid":"33786301","full_name":"NiklasRosenstein/python-localimport","owner":"NiklasRosenstein","description":"Isolated import of Python Modules for embedded applications.","archived":false,"fork":false,"pushed_at":"2022-02-23T00:42:30.000Z","size":571,"stargazers_count":27,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-04-30T22:49:49.157Z","etag":null,"topics":["import-manager","library","python"],"latest_commit_sha":null,"homepage":"http://niklasrosenstein.github.io/python-localimport/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NiklasRosenstein.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-11T17:59:43.000Z","updated_at":"2023-04-16T21:18:57.000Z","dependencies_parsed_at":"2022-08-30T05:31:56.018Z","dependency_job_id":null,"html_url":"https://github.com/NiklasRosenstein/python-localimport","commit_stats":null,"previous_names":["niklasrosenstein/localimport"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-localimport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-localimport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-localimport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-localimport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NiklasRosenstein","download_url":"https://codeload.github.com/NiklasRosenstein/python-localimport/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251795387,"owners_count":21645019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["import-manager","library","python"],"created_at":"2024-10-03T12:50:39.698Z","updated_at":"2025-04-30T22:49:56.331Z","avatar_url":"https://github.com/NiklasRosenstein.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg\" align=\"right\"\u003e\n\u003cp align=\"center\"\u003e\n  \u003cb\u003elocalimport\u003c/b\u003e allows you to import Python modules in an\u003c/br\u003e\n  isolated environment, preserving the global importer state.\n\u003c/p\u003e\n\n### Features\n\n- Emulates an isolated environment for Python module imports\n- Evaluates `*.pth` files\n- Compatible with `pkg_resources` namespaces\n- Mocks `pkgutil.extend_path()` to support zipped Python eggs\n\nCheck out the [localimport Documentation](http://niklasrosenstein.github.io/python-localimport/).\n\n### Example\n\nGiven your Python script, application or plugin comes with a directory that\ncontains modules for import, you can use localimport to keep the global\nimporter state clean.\n\n```\napp.py\nres/modules/\n  some_package/\n    __init__.py\n```\n\n```python\n# app.py\nwith localimport('res/modules') as _importer:\n  import some_package\nassert 'some_package' not in sys.modules\n```\n\n\u003e **Important**: You must keep the reference to the `localimport` object alive,\n\u003e especially if you use `from xx import yy` imports.\n\n### Usage\n\nIn most cases it would not make sense to use `localimport` as a Python module\nwhen you actually want to import Python modules since the import of the\n`localimport` module itself would not be isolated.  \nThe solution is to use the `localimport` source code directly in your\napplication code. Usually you will use a minified version.\n\nPre-minified versions of `localimport` can be found in this [Gist][pre-minified].\nOf course you can minify the code by yourself, for example using the [nr][nr]\ncommand-line tools.\n\n    nr py.blob localimport.py -cme localimport \u003e localimport-gzb64-w80.py\n\nDepending on your application, you may want to use a bootstrapper entry point.\n\n```python\n# @@@ minified localimport here @@@\n\nwith localimport('.') as _importer:\n  from my_application_package.__main__ import main\n  main()\n```\n\n  [pyminifier]: https://pypi.python.org/pypi/pyminifier\n  [py-blobbify]: https://pypi.python.org/pypi/py-blobbify\n  [pre-minified]: http://bitly.com/localimport-min\n  [nr]: https://github.com/NiklasRosenstein/py-nr\n\n### API\n\n#### `localimport(path, parent_dir=None, do_eggs=True, do_pth=True, do_autodisable=True)`\n\n\u003e A context manager that creates an isolated environment for importing\n\u003e Python modules. Once the context manager exits, the previous global\n\u003e state is restored.\n\u003e\n\u003e Note that the context can be entered multiple times, but it is not recommended\n\u003e generally as the only case where you would want to do that is inside a piece\n\u003e of code that gets executed delayed (eg. a function) which imports a module,\n\u003e and building the isolated environment and restoring to the previous state has\n\u003e some performance impacts.\n\u003e\n\u003e Also note that the context will only remove packages on exit that have\n\u003e actually been imported from the list of paths specified in the *path*\n\u003e argument, but not modules from the standard library, for example.\n\u003e\n\u003e __Parameters__\n\u003e\n\u003e * *path* \u0026ndash; A list of paths that are added to `sys.path` inside the\n\u003e   context manager. Can also be a single string. If one or more relative\n\u003e   paths are passed, they are treated relative to the *parent_dir* argument.\n\u003e * *parent_dir* \u0026ndash; A path that is concatenated with relative paths passed\n\u003e   to the *path* argument. If this argument is omitted or `None`, it will\n\u003e   default to the parent directory of the file that called the `localimport()`\n\u003e   constructor (using `sys._getframe(1).f_globals['__file__']`).\n\u003e * *do_eggs* \u0026ndash; A boolean that indicates whether `.egg` files or\n\u003e   directories found in the additional paths are added to `sys.path`.\n\u003e * *do_pth* \u0026ndash; A boolean that indicates whether `.pth` files found\n\u003e   in the additional paths will be evaluated.\n\u003e * *do_autodisable* \u0026ndash; A boolean that indicates that `localimport.autodisable()`\n\u003e   should be called automatically be the context manager.\n\u003e\n\u003e *Changed in 1.7* Added `do_autodisable` parameter.\n\n#### `localimport.autodisable()`\n\n\u003e Uses `localimport.discover()` to automatically detect modules that could be\n\u003e imported from the paths in the importer context and calls #disable on all\n\u003e of them.\n\u003e\n\u003e *New in 1.7*\n\n#### `localimport.disable(modules)`\n\n\n\u003e Disable one or more modules by moving them from the global module cache\n\u003e (`sys.modules`) to a dictionary of temporary hidden modules in the isolated\n\u003e environment. Once the `localimport()` context manager exits, these modules\n\u003e will be restored. Does nothing when a module does not exist.\n\u003e\n\u003e __Parameters__\n\u003e\n\u003e * *modules* \u0026ndash; A list of module names or a single module name string.\n\n#### `localimport.discover()`\n\n\u003e A shorthand for `pkgutil.walk_packages(importer.path)`.\n\u003e\n\u003e *New in 1.7*\n\n---\n\n\u003cp align=\"center\"\u003eCopyright \u0026copy; 2018 Niklas Rosenstein\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklasrosenstein%2Fpython-localimport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniklasrosenstein%2Fpython-localimport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklasrosenstein%2Fpython-localimport/lists"}