Questions tagged [python-importlib]

257 questions
84
votes
3 answers

How to import a module in Python with importlib.import_module

I'm trying to use importlib.import_module in Python 2.7.2 and run into the strange error. Consider the following dir structure: a | + - __init__.py - b | + - __init__.py - c.py a/b/__init__.py has the…
Zaar Hai
  • 7,011
  • 7
  • 31
  • 42
34
votes
5 answers

Python 3.5+: How to dynamically import a module given the full file path (in the presence of implicit sibling imports)?

Question The standard library clearly documents how to import source files directly (given the absolute file path to the source file), but this approach does not work if that source file uses implicit sibling imports as described in the example…
Pedro Cattori
  • 2,345
  • 1
  • 19
  • 39
33
votes
2 answers

Import class from module dynamically

I have class called 'my_class' placed in 'my_module'. And I need to import this class. I tried to do it like this: import importlib result = importlib.import_module('my_module.my_class') but it says: ImportError: No module named…
GhostKU
  • 1,323
  • 3
  • 16
  • 26
21
votes
3 answers

Python: How to import all methods and attributes from a module dynamically

I'd like to load a module dynamically, given its string name (from an environment variable). I'm using Python 2.7. I know I can do something like: import os, importlib my_module = importlib.import_module(os.environ.get('SETTINGS_MODULE')) This is…
sundance
  • 2,555
  • 2
  • 16
  • 28
18
votes
1 answer

How do I use importlib.LazyLoader?

In my module, I have a couple of functions that depend on an external module with a long startup time. How do I use LazyLoader? If I have import veggies or import veggies.brussels.sprouts or from veggies.brussels import sprouts how would I…
gerrit
  • 17,590
  • 12
  • 72
  • 135
17
votes
5 answers

How to modify imported source code on-the-fly?

Suppose I have a module file like this: # my_module.py print("hello") Then I have a simple script: # my_script.py import my_module This will print "hello". Let's say I want to "override" the print() function so it returns "world" instead. How…
Delgan
  • 14,714
  • 6
  • 77
  • 119
17
votes
1 answer

Error when using importlib.util to check for library

I'm trying to use the importlib library to verify whether the nmap library is installed on the computer executing the script in Python 3.5.2 I'm trying to use importlib.util.find_spec("nmap") but receive the following error. >>> import importlib >>>…
DKNUCKLES
  • 293
  • 1
  • 2
  • 8
16
votes
9 answers

Import error: DLL load failed in Jupyter notebook but working in .py file

I installed BreakoutDetection the module in Anaconda environment. When I tried to import the module using import breakout_detection in jupyter notebook, I get the below…
nth-attempt
  • 499
  • 1
  • 4
  • 12
15
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é
  • 774
  • 9
  • 21
12
votes
4 answers

ImportError: cannot import name 'metadata' from 'importlib'

Under a python (Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32) session launched in an Anaconda prompt, I get the error >>> import nbconvert Traceback (most recent call last): File…
10
votes
2 answers

Change environment variables before importlib.reload

I have a c-extension that loads environment variables during static initialization. I need to be able to change these values and reload the module (I cannot change the fact that they are loaded statically). I tried setting os.environ, but there…
JRG
  • 1,715
  • 2
  • 12
  • 27
10
votes
1 answer

Python importlib's analogue for imp.new_module()

PyCharm shows me that imp is deprecated so I wonder if there any analogue of imp.new_module for importlib.
user2558053
  • 255
  • 2
  • 5
  • 12
9
votes
2 answers

Local scope vs relative imports inside __init__.py

I've noticed that asyncio/init.py from python 3.6 uses the following construct: from .base_events import * ... __all__ = (base_events.__all__ + ...) The base_events symbol is not imported anywhere in the source code, yet the module still contains…
Błażej Michalik
  • 3,174
  • 27
  • 41
9
votes
1 answer

How to use importlib.resources.path(package, resource)?

I'm creating a _GeneratorContextManager with the following code. try: import importlib.resources as pkg_resources except ImportError: # Try backported to PY<37 `importlib_resources`. import importlib_resources as pkg_resources from .…
Michael_H
  • 842
  • 10
  • 22
9
votes
1 answer

python importlib no module named

I am using flask and have the following structure manage_server.py cas --- __init__.py --- routes.py --- models.py --- templates --- static --- formmodules ------ __init__.py ------…
Sid Kwakkel
  • 637
  • 2
  • 9
  • 25
1
2 3
17 18