Call a function from another file?


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

How do I fix this problem?

This question is tagged with python file function import

~ Asked on 2013-12-01 06:34:10

The Best Answer is


There isn't any 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 directory.

~ Answered on 2013-12-01 06:36:16


First of all you do not need a .py.

If you have a file and inside you have some functions:

def b():
  # Something
  return 1

def c():
  # Something
  return 2

And you want to import them in you have to write

from a import b, c

~ Answered on 2013-12-01 06:37:14

Most Viewed Questions: