- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to install and import Python modules at runtime?
You can use pip to install packages at runtime and importlib.import_module(moduleName) to import module by using its name as a string. For example,
import pip import importlib def import_with_auto_install(package): try: return importlib.import_module(package) except ImportError: pip.main(['install', package]) return importlib.import_module(package) # Example if __name__ == '__main__': scrapy = import_with_auto_install('scrapy') print(scrapy)
The above script installs the scrapy module and imports it when installation of the module completes.
- Related Articles
- How to install python modules and their dependencies easily?
- How to install Python modules in Cygwin?
- How to import Python modules by default at the time of program starts?
- How to install python modules without root access?
- How to use multiple modules with Python import Statement?
- How we can import Python modules in Jython?
- How we can import Python modules without installing?
- How to install libxml2 with python modules on Mac?
- How to Install two python modules with same name?
- How do we use easy_install to install Python modules?
- Python import modules from Zip archives (zipimport)
- How to use pip to install python modules in easy way?
- How can I import modules for a Python Azure Function?
- Can we iteratively import python modules inside a for loop?
- Can I import same package twice? Will JVM load the package twice at runtime?

Advertisements