Questions tagged [python-importlib]

a Python package that provides the implementation of the language's import statement. Use this tag for questions about using the "importlib" module.

importlib is a Python package that implements the language's import statement:

import antigravity

The common usage of this package is to import modules dynamically:

module_name = "sys"
sys = importlib.import_module(module_name)

It also handles module specs, package finders and loaders.

396 questions
119
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
  • 9,152
  • 8
  • 37
  • 45
46
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,898
  • 6
  • 23
  • 32
42
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,735
  • 1
  • 25
  • 43
25
votes
5 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…
24
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
  • 24,025
  • 17
  • 97
  • 170
23
votes
10 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
  • 649
  • 1
  • 5
  • 12
23
votes
6 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
  • 18,571
  • 11
  • 90
  • 141
22
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,905
  • 4
  • 21
  • 31
20
votes
2 answers

imp module is deprecated in favour of importlib

I'm using pandas in my code and in pandas they use the imp nodule. Now I get the following error/warnning C:\Users\refaelc\AppData\Local\Temp\collection_id-96deaf03-9b39-46c0-a843-63f6101481c1-5289121858290797008.csv Step07: Compare the downloaded…
NotSoShabby
  • 3,316
  • 9
  • 32
  • 56
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
19
votes
3 answers

Python how to alias module name (rename with preserving backward compatibility)

I have a python package named foo, which i use in imports: import foo.conf from foo.core import Something Now i need to rename the foo module into something else, let's say bar, so i want to do: import bar.conf from bar.core import Something but i…
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
18
votes
2 answers

Python: How do I find which pip package a library belongs to?

I got a script transferred from someone else. And there is a module imported into the script. I'm wondering what is the best way to find out which pip package installed this library (other than search online). I tried to import the package and then…
Suanmeiguo
  • 1,365
  • 3
  • 17
  • 28
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
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
  • 749
  • 3
  • 11
  • 31
14
votes
2 answers

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
  • 435
  • 2
  • 6
  • 12
1
2 3
26 27