Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importlib can't find module

cat test.py

from importlib import import_module

bar = import_module('bar', package='project')

ls project/

__init__.py  
__init__.pyc
bar.py
bar.pyc

python test.py

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    bar = import_module('bar', package='project')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named bar

Listing imported modules (sys.modules) doesn't show any module project.

I can import bar using the python shell.

Any ideas?

like image 943
kev Avatar asked Oct 21 '25 06:10

kev


1 Answers

It needs a dot in front of bar .. :-(

bar = import_module('.bar', package='project')
like image 180
kev Avatar answered Oct 23 '25 20:10

kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!