The Wayback Machine - https://web.archive.org/web/20210119193541/https://github.com/realpython/python-guide/issues/1013
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structuring your project guide leads to a ModuleNotFoundError in the tests module #1013

Open
isaiahstjohn opened this issue Oct 20, 2019 · 2 comments

Comments

@isaiahstjohn
Copy link

@isaiahstjohn isaiahstjohn commented Oct 20, 2019

Following this section, adding from .context import sample in my tests module gives me the following error: ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a package

I was able to fix it by removing the . from from .context import sample, but I suspect that's not the ideal solution. I know that the from .context import sample is a relative import statement as described in PEP 328, but I just do not understand modules, packages, absolute and relative imports, etc. enough to say what's going on here.

I tried creating ./__init__.py and ./tests/__init__.py files, but that did not help.

Finally, I tried the following:

~> git clone [email protected]:navdeep-G/samplemod.git
~> python3 samplemod/tests/test_basic.py
Traceback (most recent call last):
  File "samplemod/tests/test_basic.py", line 3, in <module>
    from .context import sample
ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a package

I'm not alone in hitting this issue. @rileyweber13 originally reported this problem a year ago.

@isaiahstjohn isaiahstjohn changed the title I'm not sure if this is pertinent, but I didn't want to open a new issue... I'm trying to write tests, and I'm working on [this section](https://docs.python-guide.org/writing/structure/#test-suite). In my tests modules, adding `from .context import sample` give me the following error: `ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a package` Structuring your project guide leads to a ModuleNotFoundError in the tests module Oct 20, 2019
@AghastComet
Copy link

@AghastComet AghastComet commented Oct 23, 2019

Relative imports only work in a package that is being imported due to using the __name__ variable that is set to "__main__" if the file is ran directly. If you import test_basic.py, it should work to run test_basic.sample. If you take out the period, you lose the ability to import the tests from elsewhere so what you want depends on how you're using the tests.

@nuno-andre
Copy link

@nuno-andre nuno-andre commented Nov 30, 2019

Python tests are usually coded to be executed through a testing framework. In this case

cd <yourproject>
python -m pytest tests
# either:
# py.test tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.