Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically importing module via importlib - __path__ not set?

I'm trying to import a sub-module programmatically. My file tree looks like this:

oopsd/__init__.py
oopsd/oopsd.py
oopsd/driver/__init__.py
oopsd/driver/optiups.py

The optiups.py simply prints "Hello World".

The oopsd.py looks like this:

import importlib
importlib.import_module('oopsd.driver.optiups')

Now with this, I'm getting this exception:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1521, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "oopsd/oopsd.py", line 29, in <module>
    sys.exit(main())
  File "oopsd/oopsd.py", line 23, in main
    loaddriver()
  File "oopsd/oopsd.py", line 26, in loaddriver
    importlib.import_module('oopsd.driver.optiups')
  File "/usr/lib/python3.3/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1514, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1524, in _find_and_load_unlocked
ImportError: No module named 'oopsd.driver'; oopsd is not a package

Does __path__ even still exist in Python 3?

I also tried importing .driver.optiups instead, but this yields:

TypeError: relative imports require the 'package' argument

__package__ seems unset, so I'm lost.

How do I do this the right way?

like image 640
The Compiler Avatar asked Sep 26 '13 13:09

The Compiler


People also ask

How do you set the import path in Python?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.

What does __ import __ do in Python?

The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method. The import in python returns the object or module that we specified while using the import module.

What is import Importlib in Python?

The importlib package provides the implementation of the import statement in Python source code portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.

How do I import absolute path?

import * as file4 from './file4'; An absolute import path is a path that starts from a root, and you need to define a root first. In a typical JavaScript/TypeScript project, a common root is the src directory. For file1.


1 Answers

This is an old question, but since it was bumped, the other answer is totally wrong, and this is a common problem:

You're probably doing this.

python oopsd/oopsd.py

Don't do this. :)

Specifically, NEVER try to directly run a file that's part of a parent package. When you run python FILENAME, Python adds the file's containing directory to sys.path, and DOESN'T add the current directory. So you have oopsd/ in your path, and every module in oopsd/ just became a top-level module. Python has no way of even knowing that any of them are supposed to have an oopsd. prefix, because the parent directory doesn't exist anywhere in sys.path.

If you want to execute a module directly, do this:

python -m oopsd.oopsd

This puts the current directory in sys.path and ensures that imports of your source tree work as you'd expect them to.

Alex Z's answer is wrong because it doesn't actually fix this problem, and it's not a relative import — implicit relative imports no longer exist in Python 3.

like image 157
Eevee Avatar answered Sep 25 '22 20:09

Eevee