First, import function from file.py:

from file import function

Later, 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.

Answer from Nafiul Islam on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-call-function-from-another-file
Python - Call function from another file - GeeksforGeeks
The functions defined in calc.py is called in another Python file. ... # importing limited functions # defined in calc.py from calc import addNumbers, multiplyNumbers # calling functions addNumbers(2, 5) multiplyNumbers(5, 4)
Published: August 21, 2024
Discussions

Importing functions from other folders
If you're running run_file.py, then class_file.py can access function_file.py as folder1.function_file. If you're running class_file directly in order to test it, then that won't work, but that's not how you should write tests for your code. More on reddit.com
🌐 r/learnpython
4
0
December 27, 2021
Importing a function
For some reason, I can’t import a function from another file. I’ve tried: # levels.py def level(): return 1 # main.py from levels import * print(level()) But it outputs an error: root@1de6ab8b7b69:~/my-project# /bin/python3 /home/workspace/my-project/main.py Traceback (most recent call ... More on discuss.python.org
🌐 discuss.python.org
7
0
July 22, 2024
importing functions from another file
Can someone explain this? import isn't a macro - when you import a module, it doesn't dump the module's contents into your local namespace. You access them via the imported module's namespace: functions.my_function(), etc. More on reddit.com
🌐 r/learnpython
4
2
June 4, 2021
How to call function from another python file
Hi all, This is probably a dumb question. I’m making a test project through Script Editor in Rhino 8. There are only 2 py files in the same folder. #! python3 # m1.py def main(): print("Hello I'm m1") main() #! … More on discourse.mcneel.com
🌐 discourse.mcneel.com
3
0
July 4, 2024
People also ask

How do I call a function defined in another Python file?
Put both files on the same import path (usually the same directory or a package), then use from module import function or import module and call module.function; Python must be able to resolve the module name when you run the entry script.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python call function from another file
Python Call Function from Another File: Same Folder, Different ...
What does importlib.import_module do?
It is the supported programmatic API around Python’s import machinery: you pass a dotted module name as a string and get a module object back, equivalent to an import statement at runtime without hard-coding the name in source.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python call function from another file
Python Call Function from Another File: Same Folder, Different ...
Why do I get ModuleNotFoundError when importing my own file?
Usually the current working directory or package layout is wrong, the filename does not match the module name, a parent directory is missing from sys.path, or you run a module inside a package without the -m flag so relative imports break.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python call function from another file
Python Call Function from Another File: Same Folder, Different ...
🌐
Reddit
reddit.com › r/learnpython › importing functions from other folders
r/learnpython on Reddit: Importing functions from other folders
December 27, 2021 -

Hello. I have a code divided over different files like this:

-run_file

folder1

- function_file

folder2

-class_file

I can easily import functions defined in the function_file into the run_file. However I cannot import functions into the class_file because I somehow need to write a code so that the class_file can "look out" of folder2 and look in to folder1. I looked into the code of a person who had a similar project and there were these 'things':

sys.path.append("..")

dirname = os.path.dirname(__file__)

He told me I could do it like this but it did not work for me. Could somebody give me a few hints on how I could achieve this?

🌐
YouTube
youtube.com › teachingcs
Python For Beginners - Importing Functions From Another File Explained - YouTube
00:00 - Start00:17 - Why you should modularise your code01:04 - How to make a new module02:17 - Importing functions from another file03:15 - Import just a sp...
Published: July 31, 2022
Views: 8K
🌐
Dive into Python
diveintopython.org › home › learn python programming › functions in python › ways to import functions
Examples of Ways to Import Functions in Python from Another Files
May 3, 2024 - There are several ways to import multiple functions in Python. One way is to define functions in a separate file and then import them into the main script using the import function from file syntax.
🌐
Python.org
discuss.python.org › python help
Importing a function - Python Help - Discussions on Python.org
July 22, 2024 - For some reason, I can’t import a function from another file. I’ve tried: # levels.py def level(): return 1 # main.py from levels import * print(level()) But it outputs an error: root@1de6ab8b7b69:~/my-project# …
Find elsewhere
🌐
Medium
medium.com › python-how-to › python-how-to-use-a-function-from-another-file-105b2a19dad8
Python: How to Use a Function From Another File | by nunacode | Python How To | Medium
May 1, 2026 - If you name your file math.py, Python might import your file instead of the built-in math module, causing confusing bugs elsewhere. To keep your future self happy, follow these rules: Use explicit imports: (from module import function) is usually clearer than import *.
🌐
YouTube
youtube.com › case digital
How To Import Functions From Another Python File - YouTube
In this python tutorial, I show you how to import functions from another python file! I give you two different syntaxes you can use! Let's get coding!=======...
Published: January 22, 2024
Views: 611
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python call function from another file
Python Call Function from Another File: Same Folder, Different Directory (2026)
June 20, 2026 - Calling import_module with a path string that is not a dotted package name (use spec_from_file_location for arbitrary paths). Call functions in other files by placing modules where Python can resolve them: same directory as the script you run, or a package tree imported with dotted names from the project root on sys.path.
🌐
YouTube
youtube.com › watch
How to Import a Python Function from Another File
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Towards Data Science
towardsdatascience.com › home › programming › importing python functions from other scripts
Importing Python Functions from Other Scripts | Towards Data Science
April 12, 2022 - Simplicity Compared to the copy-and-paste method, the import method makes it much easier to reuse functions. Lines of code are not accidentally dropped. Additional scripts do not need to be opened. The streamlined process eliminates possible errors. Learn this step by step with the interactive Computer Science roadmap. For clarity, here are some terms used often when describing import statements: Library: a collection of related modules such as NumPy, Pandas, etc. Module: a file with the ".py" extension; used interchangeably with script
🌐
Reddit
reddit.com › r/learnpython › importing functions from another file
r/learnpython on Reddit: importing functions from another file
June 4, 2021 -

Hello,

I have the following file structure (names are just placeholders):

app_dir
---> functions.py
---> function_tests.py

When I import functions at the top of the function_tests.py file, everything works fine.

However, I've lost the docstrings that normally pop up when I start entering a function name, when I use them in function_tests.

Can someone explain this? I am hella hazy on Python importing in general but it's on the to-learn list! For reference, this might be IDE-specific, in which case I am using Spyder.

Thanks :D

🌐
GeeksforGeeks
geeksforgeeks.org › python › importing-python-functions-from-other-scripts
Importing Python Functions from Other Scripts - GeeksforGeeks
July 23, 2025 - The second script (`main.py`) imports only the `area_of_square` function from the geometry module using the `from geometry import area_of_square` statement.
🌐
Medium
medium.com › @codingcampus › python-call-a-function-from-another-file-c41b9502ce70
Python — call a function from another file | by CodingCampus | Medium
November 23, 2023 - Next, we need to create another script where we call functions from the first file. Let’s name the new script file2.py. ... #storing Hello_world() return value to world variable world = Hello_World()#print world varaible to the screen print(world) from file import * tells the compiler to import all the available functions in file.py.
🌐
McNeel Forum
discourse.mcneel.com › scripting
How to call function from another python file - Scripting - McNeel Forum
July 4, 2024 - Hi all, This is probably a dumb question. I’m making a test project through Script Editor in Rhino 8. There are only 2 py files in the same folder. #! python3 # m1.py def main(): print("Hello I'm m1") main() #! python3 # m2.py import m1 def main(): m1.main() main() What m2 does is call m1.main(), and it works well in Script Editor.
🌐
Python.org
discuss.python.org › python help
Stuff undefined importing functions from other file - Python Help - Discussions on Python.org
February 10, 2023 - I’m writing a little software for managing infos for where I work (nothing about IT, coding or stuff like that…). As a total newbie doing by-my-self, I’m writing it in a single file, but something doesn’t work properly and my IDE doesn’t raise any error.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-import-a-class-from-another-file-in-python
How to import a class from another file in Python ? - GeeksforGeeks
November 27, 2024 - Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of invoking the import machinery, ...
🌐
Computer Science Atlas
csatlas.com › python-import-file-module
Python 3: Import Another Python File as a Module — Computer Science Atlas
It also allows us to change one line of code inside mymodule.py rather than in many different scripts if we ever decide to change the message we want to show in all the scripts that use this function. Let's say we have two Python files in the same directory: ... The name used in the import statement is simply the module's filename without the .py extension at the end. Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory.
🌐
AskPython
askpython.com › python › built-in-methods › import-other-python-files
How Do I Import Other Python Files? - AskPython
May 25, 2023 - The import statement is used to import another Python file or package into the file. It is a form of inheritance that is one of the property of Object Oriented Programming language. You can even import a particular function or class from a file.