0

I have the follow project structure

- app
  - folder1
    - folder2
      - __init__.py
      - classA.py
    - folder3
      - __init__.py
      - classB.py
    - tests
      - __init__.py
      - test_classA.py
      - test_classB.py

classB.py


from folder2.classA import clsA

I running tests the follow command and it works

cd app\folder1
python -m unittest discover

aslo if in Pycharm I add the folder1 in source, and run classB.py it works, but in vsCode when I running classB.py I get an error

ModuleNotFoundError: No module named 'folder2'

how to add folder1 in vscode to source as in pycharm?

also I tried


from ..folder2.classA import clsA

and got

ImportError: attempted relative import with no known parent package

I trying to fix it a second day, but nothing work in vscode works only in pycharm

Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
Piter
  • 61
  • 1
  • 4
  • A longer read but totally worth it: https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – waykiki May 04 '22 at 06:44

1 Answers1

0

Do not use ".." in vscode. It cannot be recognized.

You can use absolute paths. And add it into sys.path by using method sys.path.append().

You could refer to this page what I answered before.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13