0

I learning python module , I want to put some file to one mudole dir, and then call it at the project root. but it not work. I try to solve it , but it still not success!

what is the problem ?

├── calculator
│   ├── __init__.py
│   ├── addTwo.py
│   ├── mulTwo.py
│   └── sum.py
└── main.py

# calculator/__init__.py
#nothing

# calculator/addTwo.py
def addFunc(lhs, rhs):
    return lhs + rhs

# calculator/mulTwo.py
def mulFunc(lhs, rhs):
    return lhs * rhs
    
# calculator/sum.py
import addTwo

def sumFunc(arg1, arg2, arg3):
    ans = addTwo.addFunc(arg1,arg2)
    ans = addTwo.addFunc(ans,arg3)
    return ans


#main.py
from calculator import addTwo
from calculator import mulTwo
from calculator import sum


def test():
    print(addTwo.addFunc(1,2))
    print(mulTwo.mulFunc(1,2))
    print(sum.sumFunc(1,2,3))


if __name__ == '__main__':
    test()

run it

 cong.lin@C02YN3P4LVCK  ~/Program/python/module_test  python3 main.py
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from calculator import sum
  File "/Users/cong.lin/Program/python/module_test/calculator/sum.py", line 1, in <module>
    import addTwo
ModuleNotFoundError: No module named 'addTwo'
 ✘ cong.lin@C02YN3P4LVCK  ~/Program/python/module_test 

1 Answer 1

1

You have to provide PYTHONPATH to root of your project
Try

export PYTHONPATH=$PWD

before running

python3 main.py
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.