Open dmutters opened 3 weeks ago
Something like this should work for installing python3-pip on Debian-based Linux:
import subprocess
def install_package(package_name):
try:
# Update the package list
subprocess.run(['sudo', 'apt-get', 'update'], check=True)
# Install the package
subprocess.run(['sudo', 'apt-get', 'install', '-y', package_name], check=True)
print(f"Package {package_name} installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to install package {package_name}. Error: {e}")
# Example usage
install_package('curl')
For modules (colorama):
import subprocess
import sys
def install_package(package_name):
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])
print(f"Package {package_name} installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to install package {package_name}. Error: {e}")
# Example usage
install_package('colorama')
Install python3-pip on Windows:
import os
import subprocess
import urllib.request
def install_pip():
try:
# Download get-pip.py
url = 'https://bootstrap.pypa.io/get-pip.py'
file_path = 'get-pip.py'
urllib.request.urlretrieve(url, file_path)
# Run get-pip.py using the current Python interpreter
subprocess.check_call([os.path.abspath(sys.executable), file_path])
# Cleanup: Remove the get-pip.py script after installation
os.remove(file_path)
print("pip installed successfully.")
except Exception as e:
print(f"Failed to install pip. Error: {e}")
# Example usage
install_pip()
The same method can probably be used on Windows to install modules.
Install python3-pip on MacOS: probably the same as on Windows, since that doesn't rely on OS commands. For that matter, it might work for Linux, too.
Windows 10+, MacOS12+; supported versions of Debian, Ubuntu, Fedora, Arch, etc.
python3-pip colorama ctypes