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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.