The Python Oracle

How do I call a function from another .py file?

Python terms, remembered forever. Memorabit: https://memorab.it/y/py

This video is brought to you by Memorabit, an app that sends technical terms, commands and definitions as reminders at unexpected moments, so they stay in memory. Built on cognitive science principles. Start free on web, iOS and Android:
iOS: https://apps.apple.com/app/id6782301691
Android: https://play.google.com/store/apps/detai...
--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Melt

--

Chapters
00:00 How Do I Call A Function From Another .Py File?
00:21 Accepted Answer Score 877
00:50 Answer 2 Score 317
01:09 Answer 3 Score 102
01:30 Answer 4 Score 54
02:03 Thank you

--

Full question
https://stackoverflow.com/questions/2030...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #file #function #import

#avk47



ACCEPTED ANSWER

Score 877


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 2

Score 317


Do not write .py when importing.

Let file_a.py contain some functions inside it:

def f():
  return 1

def g():
  return 2

To import these functions into file_z.py, do this:

from file_a import f, g



ANSWER 3

Score 102


Method 1. Import the specific function(s) you want from file.py:

from file import function

Method 2. Import the entire file:

import file as fl

Then, to call any function inside file.py, use:

fl.function(a, b)



ANSWER 4

Score 54


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