×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Importing files from different folder in Python

Importing files in Python: Here, we are going to learn how to import files from different folder in Python program?
Submitted by Sapna Deraje Radhakrishna, on December 04, 2019

In order to learn about importing the files from a different folder, assume that the project is having the below folder structure,

Project_name
	__init__.py
	modules
		__init__.py
		file.py
		sub_module_1
			__init__.py
			file1.py

In the above structure, there is an __init__.py file added to all the folders. This file ensures that the folder is included as a python package. This file is typically empty, however, we could also add the generic module import statement in this file.

Import files:

Import the files using the syntax from <folder>.<filename> import <module>.

Example:

from modules.file import test_method
from modules.sub_module_1.file1 import test1_method
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.