user2977230 user2977230 - 1 year ago 341
Python Question

How to call a function from another file in Python?

I've seen this topic here covered numerous times, but none of the answers I've seen seem to work for me, so I'll try to be as specific to my problem as possible.

Set_up: I have a .py file for each function I need to use in a program.

In this program, I need to call the function from the external files.

I've tried:

from import function(a,b)

but I get the error:

ImportError: No module named ''; file is not a package

Answer Source

No need to add while importing. Just write from file import function, and then call the function using function(a, b). The reason why this may not work, is because file is one of Python's core modules, so I suggest you change the name of your file.

Note, that if you're trying to import functions from to a file called, you will need to make sure that and are in the same directories.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download