How To Import Function From Another Python File

How To Import Function From Another Python File

In this tutorial, you will learn how to import function from another python file.

In Python, you can import functions from another Python file in several ways.

First, we will create the file from where we have to import.

Then, we will see some of the possible ways to import functions

Let’s create greetings.py which will hold all of the functions.

def hello():
    print("Hello, World!")

def good_bye():
    print("Goodbye, World!")

def good_morning():
    print("Good Morning!")
    
def good_evening():
    print("GOod  Evening!")

def good_night():
    print("Good Night!")

Here are some ways to import functions from another python file.

1: Import the entire module

This method imports the entire module and allows you to access all the functions and variables defined in the module using dot notation.

import greetings

greetings.hello()
greetings.good_morning()

Output

Hello, World!
Good Morning!

This method is useful when you need to access multiple functions and variables defined in the module.

2: Import a specific function

This method allows you to import a specific function from a module.

And use it directly in your code without having to use the dot notation.

For example, if you only need to use the hello() function from the greetings.py module, you can import it directly into your Python code using the from statement

from greetings import hello

hello()

Output

Hello, World!

This method is useful when you only need to use a single function from a module.

3: Import multiple functions

This method allows you to import multiple functions from a module.

And use them directly in your code without having to use the dot notation.

For example, if you need to use both the hello() and good_bye() functions from the greetings.py module, you can import them directly into your Python code using the from statement:

from greetings import hello, good_bye

hello()
good_bye()

Output

Hello, World!
Goodbye, World!

This method is useful when you need to use multiple functions from a module.

4: Import all functions

This method allows you to import all functions from a module using the * wildcard.

This imports all the functions and variables defined in the module into your Python code.

For example:

from greetings import *

hello()
good_morning()
good_evening()
good_night()
good_bye()

Output

Hello, World!
Good Morning!
GOod Evening!
Good Night!
Goodbye, World!

This method is generally not recommended because it can lead to naming conflicts if there are similarly named functions or variables defined in your Python code.

It is also not clear which functions and variables are being imported from the module.

It is recommended to use either the second or third method to import specific functions or variables from a module.

Can you import a function from another Python file?

Yes, you can import a function from another Python file.

You can use the import statement followed by the name of the Python module or file containing the function, and then the name of the function you want to import.

How to import a function from another file in Python?

You can import a function from another file in python using the import.

Here is the basic syntax of doing this:

import file.py
#or
from file.py import * #to import all the functions and variables
Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x