Programmatically importing a module via importlib - __path__ not installed?

I am trying to import a submodule programmatically. My file tree is as follows:

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

optiups.py just prints "Hello World".

oopsd.py looks like this:

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

Now with this, I get 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 

Is there still __path__ in Python 3?

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

 TypeError: relative imports require the 'package' argument 

__package__ seems to be uninstalled, so I'm lost.

How do I do it right?

+7
python python-import
source share
2 answers

This is an old question, but since it was full, another answer is completely wrong, and this is a common problem:

Perhaps you will do it.

 python oopsd/oopsd.py 

Do not do this.:)

In particular, NEVER try to directly run a file that is part of the parent package. When you run python FILENAME , Python adds the file containing the directory to sys.path , and does NOT add the current directory. So you have oopsd/ in your path, and each module in oopsd/ just became a top-level module. Python does not know that any of them must have the oopsd. prefix oopsd. because the parent directory does not exist anywhere in sys.path .

If you want to execute the module directly, do the following:

 python -m oopsd.oopsd 

This puts the current directory in sys.path and ensures that importing your source tree works as you expected.

Alex Z's answer is incorrect, because in fact this problem has not been fixed, and not relative import - implicit relative import no longer exists in Python 3.

+16
source share

This works for me (relative path to the driver):

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

(karthikr was close in the comments, but you don't seem to need a lead . )

-one
source share

All Articles