Open In App

How to Import Other Python Files?

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python’s modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse code, maintain a clean codebase, and enhance collaboration among team members. In this guide, we’ll explore how to import other Python files with three practical code examples.

How to Import Other Python Files?

Below are some examples of how to import other Python Files in Python:

Example 1: Basic Import

The most straightforward way to import a Python file is by using the import statement. Consider the following example, where we have two files: main.py and module.py.

module.py: In the below code, a greet function that prints a personalized greeting. If executed independently, it also prints a message indicating that it is the module.py file.

Python3
# module.py
def greet(name):
    print(f"Hello, {name}!")


if __name__ == "__main__":
    print("This is the module.py file.")

main.py: In main.py, the module file is imported, and the greet function from module.py is called within the main function, greeting “Alice” when the script is executed.

Python3
# main.py
import module


def main():
    module.greet("Alice")


if __name__ == "__main__":
    main()

Output

Hello, Alice!

Example 2: Importing Specific Functions

Sometimes, you may only need specific functions or variables from a module rather than importing the entire module. You can achieve this using the from ... import ... syntax. Let’s modify the previous example to demonstrate this.

module.py: In module.py, the file defines a welcome function printing a personalized welcome message. When executed independently, it also outputs a statement indicating that it is a different version of the module.py file.

Python3
# module.py
def welcome(name):
    print(f"Welcome, {name}!")

if __name__ == "__main__":
    print("This is a different version of the module.py file.")

main.py : In main.py, the module file is imported with the alias mod, and the welcome function from module.py is invoked, welcoming “Charlie” when the script is executed. The use of an alias (mod) provides a shorter reference to the imported module.

Python3
# main.py
import module as mod

def main():
    mod.welcome("Charlie")

if __name__ == "__main__":
    main()

Output

Welcome, Charlie!

Example 3: Imports Alias

In some cases, you might want to rename a module or function for clarity or to avoid naming conflicts. The as keyword allows you to create an alias for the imported module or function. Let’s illustrate this with an example:

module.py: In the below code, a greet function that prints a personalized greeting. If executed independently, it also prints a message indicating that it is the module.py file.

Python3
# module.py
def greet(name):
    print(f"Hello, {name}!")

if __name__ == "__main__":
    print("This is the module.py file.")

main.py: In main.py, the module file is imported with the alias mod, and the greet function (assuming it's present in the module) is called, greeting "Charlie" when the script is executed. The use of an alias (mod) provides a shorter reference to the imported module.

Python3
# main.py
import module as mod

def main():
    mod.greet("Charlie")

if __name__ == "__main__":
    main()

Output

Hello, Charlie

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads