How to import a Python module given the full path?

The python module is a record comprising of Python code with a bunch of capacities, classes, and factors definitions. The module makes the code reusable and straightforward. The program which needs to utilize the module should import that specific module. In this article, we will talk about how to import a Python module given its full way.

There are different strategies that can be utilized to import the module by utilizing its full way:

For example

import sys
sys.path.append('/foo/bar/my_module')
# Considering your module contains a function called my_func, you could import it:
from my_module import my_func
# Or you could import the module as a whole,
import my_module

Consideration nerd! Reinforce your establishments with the Python Programming Foundation Course and become familiar with the fundamentals.

How to import a Python module

Regardless, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. Also regardless your Machine Learning Journey, join the Machine Learning – Basic Level Course

  • Utilizing sys.path.append() Function
  • Utilizing importlib Package
  • Utilizing SourceFileLoader Class

Consider the accompanying record plan and we should perceive how the above-recorded strategies can be utilized to import gfg.py module in main.py:

python
     |--main.py
     |articles
        |--gfg.py

Below is the code for gfg.py:

# class
class GFG:
   
  # method
  def method_in():
    print("Inside Class method")
     
# explicit function    
def method_out():
  print("Inside explicit method")

Utilizing sys.path.append() Function

This is the simplest method for bringing in a Python module by adding the module way to the way factor. The way factor contains the catalogs Python translator searches in for observing modules that were imported in the source records.

Syntax :

sys.path.append("module_path")

Example :

# importing module
import sys
 
# appending a path
sys.path.append('articles')
 
# importing required module
import gfg
from gfg import GFG
 
# aceesing its content
GFG.method_in()
gfg.method_out()

Output:

Inside Class method
Inside explicit method

Utilizing importlib Package

The importlib bundle gives the execution of the import articulation in Python source code convenient to any Python translator. This empowers clients to make their custom items which assists them with utilizing the import cycle as indicated by their requirements. The importlib.util is one of the modules remembered for this bundle that can be utilized to import the module from the given way.

Syntax :

module = importlib.util.spec_from_file_location(“module_name”, “module_path”)

Example:

import importlib.util
 
# specify the module that needs to be 
# imported relative to the path of the 
# module
spec=importlib.util.spec_from_file_location("gfg","articles/gfg.py")
 
# creates a new module based on spec
foo = 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

Utilizing SourceFileLoader Class

SourceFileLoader class is a theoretical base class that is utilized to carry out source record stacking with assistance of load_module() work which really imports the module.

Syntax :

module = SourceFileLoader("module_name","module_path").load_module()

Example :

from importlib.machinery import SourceFileLoader
 
# imports the module from the given path
foo = SourceFileLoader("gfg","articles/gfg.py").load_module()
 
foo.GFG.method_in()
foo.method_out()

Output:

Inside Class method
Inside explicit method

Also Read: How to install Jupyter Notebook on Windows?

Leave a Reply

Your email address will not be published. Required fields are marked *