0

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.

italianfoot
  • 199
  • 3
  • 8

1 Answers1

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.

Community
  • 1
  • 1
Joshua R.
  • 2,282
  • 1
  • 18
  • 21
  • I get a `ModuleNotFoundError: No module named '__main__.example'; '__main__' is not a package` – italianfoot Feb 01 '18 at 22:02
  • Did you also add the dot to the tests import, `from .tests import test_foo`? – Joshua R. Feb 01 '18 at 22:06
  • What if you add an `__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` – Joshua R. Feb 01 '18 at 22:18