Questions tagged [import-hooks]

12 questions
20
votes
2 answers

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

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…
André
  • 914
  • 1
  • 10
  • 23
16
votes
1 answer

Package-specific import hooks in Python

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…
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
12
votes
1 answer

PEP 302 Example: New Import Hooks

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…
brad
  • 73,826
  • 21
  • 73
  • 85
4
votes
0 answers

inspect.getmodule returns None for import hooks

I am creating a runtime module in my python code similar to string_code = """ import existing_module a = "my_string" existing_module.register(a) """ mod = ModuleType("mymodule") sys.modules["mymodule"] = mod exec(string_code, mod.__dict__) Inside…
user3828311
  • 907
  • 4
  • 11
  • 20
4
votes
2 answers

importing same module more than once

So after a few hours, I discovered the cause of a bug in my application. My app's source is structured like: main/ __init__.py folderA/ __init__.py fileA.py fileB.py Really, there are about 50 more files. But that's…
Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62
2
votes
1 answer

Proper way of implementing post import hooks in python-3.4+

I want to modify the import behavior. I found an example with post import hook in the Book "Python Cookbook" by David Beazley and Brian K. Jones, which should suit my problem. Since the latest edition was published at the times of python-3.3 the…
SilvergL
  • 51
  • 6
2
votes
1 answer

Fake Namespace Packages in Python 2

PEP 302 -- New Import Hooks specifies ways to hook the import mechanism of Python. One of them is to create a module finder/loader and add it to sys.meta_path. I seek to create a module finder that is capable of re-routing the import of…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
1
vote
1 answer

python shipping package to remote machine and import from memory

I a trying to run and use a python package on a remote machine without deploying or installing the package. In the following link it's nicely described how to create a string importer for modules. modules files are read and stored as text in a…
Cobry
  • 4,348
  • 8
  • 33
  • 49
1
vote
0 answers

Import hook that prints any time something is doing an import

I have a python module and a unit test for it. When i run the unit test i get this: flumotion test test_common_gstreamer ... …
10001a
  • 53
  • 1
  • 6
1
vote
1 answer

Python import hooks: no filenames in trackback of import errors

I've written an import hook according to PEP 302 and it seems to work fine except one annoying detail. When there's an import error, say code that tries to import a module that doesn't exist, I get a trackback with lines like: File "", line…
shoosh
  • 76,898
  • 55
  • 205
  • 325
0
votes
1 answer

Unable to find the functions defined in dynamically loaded module

I'm very new to python. Here is the problem Im having. I have hooked the builtin._import_ with my custom hook which loads a module from a string. def import_hook(name, globals=None, locals=None, fromlist=None): if name in sys.modules: …
ajay singh
  • 135
  • 1
  • 7
0
votes
0 answers

Write a PEP-302 import hook that knows which module is importing which

Let's say I have modules foo.py and bar.py and both have import baz statement in their code. I want to write an import hook that I can insert into sys.meta_path that knows whether baz is being imported from foo or from bar. The reason why I want to…
Vojislav Stojkovic
  • 8,043
  • 4
  • 35
  • 48