Uncategorized

runtime error – Python ModuleNotFoundError only in command line execution not in IDE


I am a noob in Python and am running into a strange issue where I am seeing the ModuleNotFoundError error when running a simple program.

The project is created using Poetry, and the setup is as shown in the screenshot.
enter image description here

h.py has this:

def add(num1, num2):
    return num1 + num2

s.py has this:

from src.helper.h import add


def main():
    print(add(1, 2))


if __name__ == "__main__":
    main()

If I execute the code from within PyCharm IDE, I see the output correctly. If I execute from command line using python3 src/scripts/s.py, I see an error as below:

Traceback (most recent call last):
  File "/Users/me/dev/python-learn/src/scripts/s.py", line 1, in <module>
    from src.helper.h import add
ModuleNotFoundError: No module named 'src'

I tried adding __init.py__ to each of the directories src, helpers, and scripts. That did not resolve the issue.

Can someone tell me what I am missing? Thanks in advance!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *