I have the following folder structure:
project/
setup.py
example/
__init__.py
foo.py
tests/
__init__.py
test_foo.py
test_foo.py contains the 'a' variable with some integer value.
foo.py contains the following:
from tests import test_foo
def load_a():
value = test_foo.a
return value
setup.py contains the following:
from example import foo
a = foo.load_a()
print(a)
When I run setup.py by calling python setup.py I get a ModuleNotFoundError.
Why is that? I am running python 3.6.3.
1 Answer 1
See Intra-package References in https://docs.python.org/3/tutorial/modules.html
You can also write relative imports, with the from module import name form of import statement. These imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module for example, you might use:
from . import echo
from .. import formats
from ..filters import equalizer
i.e. I think you need to e.g. from .example import foo.
3 Comments
ModuleNotFoundError: No module named '__main__.example'; '__main__' is not a packagefrom .tests import test_foo?__init__.py file to the project directory? Or alternatively, you may not need the '.' in from .example import foo after all, just for importing test_foo