
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Import Python Modules Without Installing
In Python, there are several ways to import modules without requiring installation. This can be particularly useful when you do not have administrative privileges or need to manage different module versions. Below are some common approaches:
- Using 'sys.path' to Include Additional Directories
- Using 'virtualenv' for Isolated Environments
- Using 'importlib' for Dynamic Imports
Using 'sys.path' to Include Additional Directories
We can add directories to Python's search path at runtime using the sys.path list. This allows Python to look for modules in custom locations and include directories where you manually store Python module files.
Example
In the following example code, os.path.dirname(file_path) function retrieves the directory path of the specified file path. The sys.path.append(...) adds the directory to the list of paths that Python searches for modules.
import os, sys file_path = 'AdditionalModules/' sys.path.append(os.path.dirname(file_path)) #Now python also searches AdditionalModules folder for importing modules as we have set it on the PYTHONPATH
Using 'virtualenv' for Isolated Environments
The virtualenv is a tool that allows you to create separate Python environments. This is particularly useful for managing specific package versions or avoiding conflicts with globally installed packages.
Installing 'virtualenv'
If you're unable to install Python modules directly on a machine because of permission issues, there are alternative methods to bypass this restriction. One option is to use virtualenv, a tool that enables you to create isolated Python environments.
We can install virtualenv locally, even without administrative privileges, you can do so with pip using the following command:
pip install --user virtualenv
Using 'Importlib' Package
The importlib package allows for dynamic imports of modules during runtime. This means you can load modules as needed without a static import statement at the beginning of your script. It also allows users to create and utilize custom objects, known as importers.
Example
In the following example, we are going to dynamically import the module named 'mod1' by using importlib.import_module('mod1').
import importlib if __name__ == '__main__': # Import the custom module dynamically module1 = importlib.import_module('mod1') print(module1.__name__) module1.run() # Call the run function from mod1 module2 = importlib.import_module('mod2') print(module2.__name__) module2.run() # Call the run function from mod2
Contents of 'mod1.py'
def run(): print("This is the run function from mod1.")
Contents of 'mod2.py'
def run(): print("This is the run function from mod2.")
Output
When you run the main script, the output will be:
mod1 This is the run function from mod1. mod2 This is the run function from mod2.