How to import a Python module given the full path? - GeeksforGeeks

09 May, 2021
The python module is a file consisting of Python code with a set of functions, classes, and variables definitions. The module makes the code reusable and easy to understand. The program which needs to use the module should import that particular module. In this article, we will discuss how to import a Python module given its full path.
There are various methods that can be used to import the module by using its full path:
Using sys.path.append() Function
Using importlib Package
Using SourceFileLoader Class
Consider the following file arrangement and let’s see how the above-listed methods can be used to import gfg.py module in main.py:
python
     |--main.py
     |articles
        |--gfg.py  
Below is the code for gfg.py:
Python3
# classclass GFG:      # method  def method_in():    print("Inside Class method")      # explicit function    def method_out():  print("Inside explicit method")
This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.
Syntax :
sys.path.append("module_path")
Example :
Python3
# importing moduleimport sys  # appending a pathsys.path.append('articles')  # importing required moduleimport gfgfrom gfg import GFG  # aceesing its contentGFG.method_in()gfg.method_out()
Output:
Inside Class method
Inside explicit method
The importlib package provides the implementation of the import statement in Python source code portable to any Python interpreter. This enables users to create their custom objects which helps them to use the import process according to their needs. The importlib.util is one of the modules included in this package that can be used to import the module from the given path.
Syntax :
module = importlib.util.spec_from_file_location(“module_name”, “module_path”)
Example:
Python3
import importlib.util  # specify the module that needs to be # imported relative to the path of the # modulespec=importlib.util.spec_from_file_location("gfg","articles/gfg.py")  # creates a new module based on specfoo = importlib.util.module_from_spec(spec)  # executes the module in its own namespace# when a module is imported or reloaded.spec.loader.exec_module(foo)  foo.GFG.method_in()foo.method_out()
Output:
Inside Class method
Inside explicit method
SourceFileLoader class is an abstract base class that is used to implement source file loading with help of load_module() function which actually imports the module.
Syntax :
module = SourceFileLoader("module_name","module_path").load_module()
Example :
Python3
from importlib.machinery import SourceFileLoader  # imports the module from the given pathfoo = SourceFileLoader("gfg","articles/gfg.py").load_module()  foo.GFG.method_in()foo.method_out()
Output:
Inside Class method
Inside explicit method
Picked
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()