Home » Python

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


Comments and Discussions!

Load comments ↻





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