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
Create a Python file containing the required functions. Create another Python file and import the previous Python file into it.
Published: August 21, 2024
Discussions

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 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 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
๐ŸŒ
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 - Finally, double-check the function name to make sure it's spelled correctly and that it's defined in the file you're trying to import from. For example, let's say you want to import the runsqlscript function from the sqlideutils module. Here's how you could do it using the from keyword: from sqlideutils import runsqlscript ยท And here's how you could do it using the import keyword: import sqlideutils with runsqlscript = sqlideutils.runsqlscript ยท Organizing your Python code is crucial to make it readable, maintainable, and reusable.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ importing-python-functions-from-other-scripts
Importing Python Functions from Other Scripts - GeeksforGeeks
July 23, 2025 - Modular programming in Python enables reuse and structure of code. To accomplish this, importing functions from other scripts or modules is a potent method. This procedure facilitates collaborative development, promotes concern separation, and improves code readability.
๐ŸŒ
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# โ€ฆ
๐ŸŒ
Medium
wbarillon.medium.com โ€บ the-easiest-way-to-import-your-functions-or-parameters-in-python-2f0a1a71084a
The easiest way to import your functions or parameters in python
November 1, 2020 - Once your file is created, simply copy-paste your functions or parameters in it and save it. As you can see on my screenshot above, my organization is quite messy. At the moment, I wouldnโ€™t even need to go further since my file.py is at the root with my notebook Untilted.ipnyb.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ how-to-import-all-functions-from-a-python-file
How to Import All Functions from a Python File
August 19, 2023 - While the explicit method is recommended, Python also allows you to import all functions from a module using the from module import * syntax. This imports all functions and makes them available in your current namespace.
๐ŸŒ
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
๐ŸŒ
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 - ... #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.
๐ŸŒ
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.
๐ŸŒ
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?

๐ŸŒ
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

๐ŸŒ
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 ... to save different single files for those functions that write on a same .csv file. The problem is that when I try to import those function (using โ€™ from path/to/directo......
๐ŸŒ
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.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-import-function-from-file
Python Import Function from File: A Comprehensive Guide - CodeRivers
February 22, 2026 - In Python, the import statement is used to bring code from external files (modules) into your current script. This allows you to use functions, classes, and variables defined in those files without having to rewrite the code.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ import files from a different folder in python
Python Import From Parent Directory and Subfolders | Sentry
To import functions from libfunctions.py into main.py, we can use the following import statement: ... To import scripts from parent or sibling directories, we can use package relative imports. First, we must ensure that our directories are recognized as packages by Python, which we can do by creating an empty file named __init__.py in each of them.