0

I have a project that is structured as below:

folderA
  |
  -> classA.py
  -> classB.py
  -> __init__.py
  -> folderB
      |
      -> test.py

I need to import classA in test.py and it doesn't work. I've tried the following

  1. from folderA import classA. This throws an error saying No module named folderA
  2. from ...folderA import classA. This results in attempted relative import with no known parent package

Can anyone help me in solving this problem?

Thanks!

Doctor
  • 35
  • 8
  • What is your PYTHONPATH? Can you post the result of `import sys; print(sys.path)`? – TQA Jun 09 '20 at 16:17
  • 1
    [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/10315163). Hope this help. – Ynjxsjmh Jun 09 '20 at 16:27
  • How are you running your test.py script? (ie `python test.py` from inside folderB? `python -m folderA.folderB.test`?) – Gabriela Melo Jun 09 '20 at 16:28

1 Answers1

0

I believe the correct python syntax is from ...folderA.classA import Function. I've also often found it easier to just append the specific folder I need to use to sys.path like this: sys.path.append(folderA) . Note that you probably need the full path to FolderA for this to work.

DerekG
  • 3,555
  • 1
  • 11
  • 21