-1

I have the following folder structure:

src
 |_ __init__.py
    example.py
test
 |_ test.py
# __init__.py
class API:
    def something(self):
        print('folder src | file __init__')
# example.py
class Example:
    def doingSomething(self):
        print('folder src | file example')
# test.py
import src
from src.example import Example

class Test:
    def somethingElse(self):
        print('folder test | file test')

when I run the test.py file, I get the following error:

Traceback (most recent call last):
  File "<my path>\test\test.py", line 1, in <module>
    import src
ModuleNotFoundError: No module named 'src'
Little Ball
  • 5,632
  • 3
  • 4
  • 21

1 Answers1

1

Unless you import the example module in your src/__init__.py file, you need to specify the module name (i.e., example) within the package (i.e., src).

from src.example import Example
abc
  • 10,886
  • 2
  • 22
  • 46
  • ok thanks, but I'm still getting the same error from the first line of test.py – Little Ball Dec 19 '20 at 20:39
  • That means that the src package is not in your `PYTHONPATH` – abc Dec 19 '20 at 20:42
  • How do I put it in my PYTHONPATH? I'm using vscode – Little Ball Dec 19 '20 at 20:43
  • [This question](https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code) or [this one](https://stackoverflow.com/questions/58570503/how-to-use-pythonpath-with-vscode-python-extension-for-debugging) might be useful. – abc Dec 19 '20 at 20:50
  • 1
    try using the below import sys sys.path.append('../') – Mandar Autade Dec 19 '20 at 20:52
  • What should I add in my `.vscode/settings.json` file? I am on windows 10. The import sys didn't work and I can't find an answer on both links posted by @abc – Little Ball Dec 19 '20 at 21:02