Skip to content

Commit

Permalink
auto install module
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed May 3, 2023
1 parent b4ce868 commit 9cdaf87
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions snippets/dev/auto_install_python_pip_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Auto install python module

import sys
import importlib
import subprocess

def install_module(module_name, package_name=None):
'''Install a python module with pip or return it if already installed'''
try:
module = importlib.import_module(module_name)
except ModuleNotFoundError:
print(f'Installing Module {module_name} ....')

subprocess.call([sys.executable, '-m', 'ensurepip'])
subprocess.call([sys.executable, '-m', 'pip', 'install', package_name or module_name])

module = importlib.import_module(module_name)

return module

0 comments on commit 9cdaf87

Please sign in to comment.