import-hooks

Package-specific import hooks in Python

耗尽温柔 提交于 2019-12-17 22:37:52
问题 I'm working on creating a Python module that maps API provided by a different language/framework into Python. Ideally, I would like this to be presented as a single root package that exposes helper methods, and which maps all namespaces in that other framework to Python packages/modules. For the sake of convenience, let's take CLR as an example: import clr.System.Data import clr.System.Windows.Forms Here clr is the magic top-level package which exposes CLR namespaces System.Data and System

How to implement an import hook that can modify the source code on the fly using importlib?

会有一股神秘感。 提交于 2019-12-04 08:09:40
问题 Using the deprecated module imp , I can write a custom import hook that modifies the source code of a module on the fly, prior to importation/execution by Python. Given the source code as a string named source below, the essential code needed to create a module is the following: module = imp.new_module(name) sys.modules[name] = module exec(source, module.__dict__) Since imp is deprecated, I would like to do something similar with importlib . [EDIT: there are other imp methods that need to be

PEP 302 Example: New Import Hooks

瘦欲@ 提交于 2019-11-30 01:51:43
问题 Where can I find an example implementation of the "New Import Hooks" described in PEP 302? I would like to implement a custom finder and loader in the most forward compatible way possible. In other words, the implementation should work in python 2.x and 3.x. 回答1: You can find thousands of open-source examples e.g. with a google code search, here it is: http://www.google.com/codesearch?hl=en&lr=&q="imp.find_module"+"imp.load_module"&sbtn=Search Edit: as the questioner clarified he's looking

Package-specific import hooks in Python

只愿长相守 提交于 2019-11-28 18:53:52
I'm working on creating a Python module that maps API provided by a different language/framework into Python. Ideally, I would like this to be presented as a single root package that exposes helper methods, and which maps all namespaces in that other framework to Python packages/modules. For the sake of convenience, let's take CLR as an example: import clr.System.Data import clr.System.Windows.Forms Here clr is the magic top-level package which exposes CLR namespaces System.Data and System.Windows.Forms subpackages/submodules (so far as I can see, a package is just a module with child modules