(I appreciate there are many similar questions on StackOverflow already, but none seemed to help me.)
I have the following structure:
code/
foo/
__init__.py
foo.py
bar/
__init__.py
bar.py
I want to be able to import foo
as a package in bar.py
without messing with sys
.
This is my foo.py
:
hello = 'Hello, world!'
This is my bar.py
:
import foo
print(foo.hello)
But if I execute python foo/bar/bar.py
I get the following error:
ModuleNotFoundError: No module named 'foo'
Of all things, the one that's driving me crazy the most is that if I add the following snippet on top of bar.py
:
import pkgutil
search_path = ['.']
all_modules = [x[1] for x in pkgutil.iter_modules(path=search_path)]
print(all_modules)
Then I actually get foo
as an available module (still failing), here:
['foo']
ModuleNotFoundError: No module named 'foo'