简体   繁体   中英

A very simple question on python import, functions, sub-directories

I have a super simple python project, but I can't get it to work.

In the directory called "demo", I have a file called "demo.py", reading:

#!/usr/bin/python
from calc import plus
print(plus(1, 4))

I also have a sub-directory called "calc" with the following files:

__init__.py
minus.py
plus.py

The init .py is empty, whereas "minus.py" and "plus.py" read respectively:

def minus(a, b):
  return a - b

and

def plus(a, b):
  return a + b

When I run the demo.py, I get the error:

Traceback (most recent call last):
  File "./demo.py", line 3, in <module>
    print(plus(1, 4))
TypeError: 'module' object is not callable

It must be something absurdly simple, but I just can't figure it out.

Any help and advice would be highly appreciated.

Cheers

You need fix your import.

You have a file and one function with same name, then you need import the module(file) and after import the function.

from calc.plus import plus

Or custom your init.py

https://docs.python.org/3/tutorial/modules.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM