[python] 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 following code:

    import importlib

    mod = importlib.import_module("c")

(In real code "c"has a name.)

Trying to import a.b, yields the following error:

    >>> import a.b
    Traceback (most recent call last):
      File "", line 1, in 
      File "a/b/__init__.py", line 3, in 
        mod = importlib.import_module("c")
      File "/opt/Python-2.7.2/lib/python2.7/importlib/__init__.py", line 37, in   import_module
        __import__(name)
    ImportError: No module named c

What am I missing?

Thanks!

This question is related to python import module python-importlib

The answer is


And don't forget to create a __init__.py with each folder/subfolder (even if they are empty)


I think it's better to use importlib.import_module('.c', __name__) since you don't need to know about a and b.

I'm also wondering that, if you have to use importlib.import_module('a.b.c'), why not just use import a.b.c?


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to import

Import functions from another js file. Javascript The difference between "require(x)" and "import x" pytest cannot import module while python can How to import an Excel file into SQL Server? When should I use curly braces for ES6 import? How to import a JSON file in ECMAScript 6? Python: Importing urllib.quote importing external ".txt" file in python beyond top level package error in relative import Reading tab-delimited file with Pandas - works on Windows, but not on Mac

Examples related to module

How to fix: fatal error: openssl/opensslv.h: No such file or directory in RedHat 7 How to import functions from different js file in a Vue+webpack+vue-loader project Typescript ReferenceError: exports is not defined ImportError: No module named tensorflow ModuleNotFoundError: What does it mean __main__ is not a package? ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import module.exports vs. export default in Node.js and ES6 What's the difference between an Angular component and module Export multiple classes in ES6 modules Python - Module Not Found

Examples related to python-importlib

How to import a module in Python with importlib.import_module