0

I usually assume that a similar answer is available on the internet, so please direct me if so. I have been searching for several hours, so decided to ask the community.

I am new to testing and attempting to set up a structure as follows (in a Windows env):

-parentFolder
  -__init__.py
  -src
    -__init__.py
    -fileA.py
    -fileB.py
  -test
    -__init__.py
    -test_fileA.py

Answer I followed

FileA contents:

import fileB

# do something

test_fileA contents (just trying to get imports working first):

import sys
sys.path.append('..')
from src import fileA

if __name__ == "__main__":
    print('success')

The code I am running/error I am receiving back is

C:\parentFolder >> python -m tests.test_fileA

...
File "C:\parentFolder\src\fileA.py", line 14, in <module>
    import fileB
ModuleNotFoundError: No module named 'fileB'

I am guessing that this has to do with relative paths? I was able to work around using from . import fileB but this feels hacky. What would the proper solution be?

Alex D
  • 46
  • 1
  • 8
  • Check if the current working directory is set to the right path. If you are running from the parent folder then it should be `from src import fileB` – Arun Sg Jun 02 '21 at 16:24
  • So will all files that call each other in the folder require the append there? Running python fileA.py runs fine from my command line. – Alex D Jun 02 '21 at 16:42

1 Answers1

0
import src.fileB

should work if you really dont want to use a relative path, but as far as I can tell using import .fileB has no disadvantages.

If you want to read more about importing stuff I would suggest this post: https://stackoverflow.com/a/16985066/15906059