0

The accepted answer from the following have not resolved my issue:

I am developing on Ubuntu 20.04.1 LTS.

Structure

  • TopLevel
    • task
      • init.py
      • file1.py
      • main.py
    • tests
      • init.py
      • testFile.py

Code that causes the error is inside of testFile.py

  • from task.file1 import Class

Error message:

  • ModuleNotFoundError: No module names 'Class'

I realize it has to do with python searching only the test directory and I followed what this answer suggested: https://stackoverflow.com/a/61806535/14179793 It reveals that the task/ directory is not being searched but even when I implement what is suggested it still does not work.

Cogito Ergo Sum
  • 592
  • 6
  • 18
  • 1
    Please also check the answers to [this question](https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named-yadayadayada). – MrBean Bremen Dec 22 '20 at 22:15
  • How about `from task.file1 import Class`? – Jzbach Dec 22 '20 at 22:22
  • @Jzbach So that is actually how I have it I just left it out. I have updated the question. – Cogito Ergo Sum Dec 22 '20 at 22:25
  • @MrBeanBremen #1 of the question you linked does not resolve the issue and I am not sure how to do #2. I am running pytest from the TopLevel and I have to try to keep it that way. – Cogito Ergo Sum Dec 22 '20 at 22:26
  • How are you running the tests? Are you using the command line (i.e. `python -m pytest [...]`) or some IDE like PyCharm? If the command line, which command are you issuing? – Jzbach Dec 22 '20 at 22:29
  • @Jzbach I am using the terminal inside of PyCharm. The command I use is `pytest tests`. Interestingly this worked fine on the Windows machine i was using but not on Ubuntu but I have to use Ubuntu now. – Cogito Ergo Sum Dec 22 '20 at 22:30
  • PyCharm already has this preconfigured testing configs. If you go to Run>Edit Configurations and add a new configuration, you can scroll down and see "Pytest" under "Python Tests". There you can select your target *and put the working directory as your TopLevel*. If you're lost let me know and I'll try to make some screenshots – Jzbach Dec 22 '20 at 22:37
  • @Jzbach Okay, I am pretty sure I did it correctly. I created a run configuration with the target /.../testFile.py and working directory TopLevel but I get the same error. I will note that these tests will be run from the command line in a GitHub action so even if this were to work it wouldn't be a final solution. – Cogito Ergo Sum Dec 22 '20 at 22:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226330/discussion-between-jzbach-and-michael). – Jzbach Dec 22 '20 at 22:47
  • 1
    @MrBeanBremen please don't hesitate to vote a dupe next time :-) – hoefling Dec 23 '20 at 16:03

1 Answers1

0

It seems I have found a solution working from here: https://stackoverflow.com/a/10253916/14179793

The proposed solution did not work: sys.path.insert(0, myPath + '/../') but the following did: sys.path.insert(0, myPath + '/../task')

I did not realize the difference in directory structure.

Cogito Ergo Sum
  • 592
  • 6
  • 18