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.

asked Feb 1, 2018 at 21:51

1 Answer 1

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.

answered Feb 1, 2018 at 21:55
Sign up to request clarification or add additional context in comments.

3 Comments

I get a ModuleNotFoundError: No module named '__main__.example'; '__main__' is not a package
Did you also add the dot to the tests import, from .tests import test_foo?
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.