Import Modules From Another Folder in Python
Last Updated :
17 Jun, 2021
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder.
It can be done in two ways:
- Using sys.path
- Using PythonPath.
Create a module for demonstration:
File name: module0.py
Python3
def run():
print("Module 0 imported successfully")
sys.path: It is a built-in variable within the python sys module. It contains a list of directories that the interpreter will search in for the required modules.
Python3
import sys
# Prints the list of directories that the
# interpreter will search for the required module.
print(sys.path)
Output:
In this approach, Insert or Append the path of the directory containing the modules in sys.path.
Syntax:
sys.path.insert(0, path)
sys.path.append(path)
Example: Suppose we need to import the following modules from "Desktop\\Task\\modules" in "Desktop\\VScode\\Projects\\ImportModule\\main.py".
Insert/Append the path to sys.path and import module0 present in the directory and call its run function.
Python3
import sys
# Insert the path of modules folder
sys.path.insert(0, "C:\\Users\\anila\\Desktop\\Task\\modules")
# Import the module0 directly since
# the current path is of modules.
import module0
# Prints "Module0 imported successfully"
module0.run()
Output:
PYTHONPATH : It is an environment variable which you can set to add additional directories where python will look for modules and packages.
Open a terminal or command prompt and enter the following command:
Syntax: set PYTHONPATH=path_to_module_folder
Add the path to PYTHONPATH and import module0 present in the directory and call its run function.
Below is the implementation:
Python3
# Import the module0 directly since
# the current path is of modules.
import module0
# Prints "Module0 imported successfully"
module0.run()
Output:
Similar Reads
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Python â Import module from different directory While working on big projects we may confront a situation where we want to import a module from a different directory. But for some reason, the module may not be imported correctly. Now donât worry if your module is not imported correctly. In this article, we will discuss ways to import a module fro
4 min read
How to import a class from another file in Python ? In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 min read
How to import variables from another file in Python? To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your cur
4 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
Python - Call function from another file Given a Python file, we need to call a function in it defined in any other Python file. Example: Suppose there is a file test.py which contains the definition of the function displayText(). #test.py>def displayText(): print( "Geeks 4 Geeks!")We need to call the function displayText() in any other
5 min read