Call a function from another file in Python
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.
from file.py import function(a,b)
But I get the error:
ImportError: No module named 'file.py'; file is not a package
How do I fix this problem?
There isn't any need to add
file.py 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
a.py to a file called
b.py, you will need to make sure that
b.py are in the same directory.
Read more... Read less...
First of all you do not need a
If you have a file
a.py and inside you have some functions:
def b(): # Something return 1 def c(): # Something return 2
And you want to import them in
z.py you have to write
from a import b, c
You can do this in 2 ways. First is just to import the specific function you want from file.py. To do this use
from file import function
Another way is to import the entire file
import file as fl
Then you can call any function inside file.py using
If your file is in the different package structure and you want to call it from a different package, then you can call it in that fashion:
Let's say you have following package structure in your python project:
com.my.func.DifferentFunction python file you have some function, like:
def add(arg1, arg2): return arg1 + arg2 def sub(arg1, arg2) : return arg1 - arg2 def mul(arg1, arg2) : return arg1 * arg2
And you want to call different functions from
Example3.py, then following way you can do it:
Define import statement in
Example3.py - file for import all function
from com.my.func.DifferentFunction import *
or define each function name which you want to import
from com.my.func.DifferentFunction import add, sub, mul
Example3.py you can call function for execute:
num1 = 20 num2 = 10 print("\n add : ", add(num1,num2)) print("\n sub : ", sub(num1,num2)) print("\n mul : ", mul(num1,num2))
add : 30 sub : 10 mul : 200
You can call the function from a different directory as well, in case you cannot or do not want to have the function in the same directory you are working. You can do this in two ways (perhaps there are more alternatives, but these are the ones that have worked for me).
Alternative 1 Temporarily change your working directory
import os os.chdir("**Put here the directory where you have the file with your function**") from file import function os.chdir("**Put here the directory where you were working**")
Alternative 2 Add the directory where you have your function to sys.path
import sys sys.path.append("**Put here the directory where you have the file with your function**") from file import function
Came across the same feature but I had to do the below to make it work.
If you are seeing 'ModuleNotFoundError: No module named', you probably need the dot(.) in front of the filename as below;
from .file import funtion