0

I have created the following files under the package name Operation.

# C:\Python27\Operation\addExample.py
def add(a, b):
   result = a + b
   return result

# C:\Python27\Operation\SubExample.py
def sub(a, b):
   result = a - b
   return result

# C:\Python27\Operation\__init__.py (an empty file)
# C:\Python27\Operation\test.py

from  Operation import addExample,SubExample

print(addExample.add(4,5))
print(SubExample.sub(4,5))

When I run the test.py , I get the following error. Can some one tell me how to fix the problem.

Traceback (most recent call last):
  File "C:\Python27\Operation\test.py", line 1, in <module>
    from  Operation import addExample,SubExample
ModuleNotFoundError: No module named 'Operation'
knh190
  • 2,744
  • 1
  • 16
  • 30
  • 1
    `C:\Python27\Operation__init__.py (an empty file)` is this line correct? or did you miss a `\ `? – Nullman Mar 18 '19 at 08:53
  • You cannot import `Operation` from its root directory without introducing it to `sys.path`. You may use local import though. Check out possible duplicate question: https://stackoverflow.com/questions/1699108/local-import-statements-in-python – knh190 Mar 18 '19 at 08:55
  • Did you *install* the `Operation` module? (In other words, is it on your $PYTHONPATH?) It won't magically become importable just because it exists. – Aran-Fey Mar 18 '19 at 08:55
  • No.It's just typo error. – T.Chithra Mar 18 '19 at 08:55
  • Sorry, this link is more helpful: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – knh190 Mar 18 '19 at 09:01
  • "it on your $PYTHONPATH?) It won't magically become importable just because it exists. – T.Chithra Mar 22 '19 at 03:52
  • "is it on your $PYTHONPATH?) It won't magically become importable just because it exists. " - Iam a novice to Python programming.Please let me know the process to include in the $PYTHONPATH. – T.Chithra Mar 22 '19 at 03:54

0 Answers0