snippetpythonCriticalCanonical
How do I call a function from another .py file?
Viewed 0 times
howfromfunctionfilecallanother
Problem
file.py contains a function named function. How do I import it?from file.py import function(a,b)The above gives an error:
ImportError: No module named 'file.py'; file is not a package
Solution
First, import
Later, call the function using:
Note that
Note that if you're trying to import functions from
function from file.py:from file import functionLater, call the function using:
function(a, b)Note that
file is one of Python's core modules, so I suggest you change the filename of file.py to something else.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 a.py and b.py are in the same directory.Code Snippets
from file import functionfunction(a, b)Context
Stack Overflow Q#20309456, score: 874
Revisions (0)
No revisions yet.