Getting Started
This tutorial provides a quick overview of common pipmaster tasks.
1. Import the Library
Start by importing pipmaster:
import pipmaster as pm
import asyncio # Needed for async examples later
2. Install a Package
Use install()
to ensure a package is installed (it will upgrade if already present by default):
package_name = "requests"
print(f"Ensuring '{package_name}' is installed...")
if pm.install(package_name):
print(f"'{package_name}' installed or already up-to-date.")
else:
print(f"Failed to install '{package_name}'.")
3. Check if a Package is Installed
Use is_installed()
and get_installed_version()
:
package_to_check = "packaging" # A dependency of pipmaster
if pm.is_installed(package_to_check):
version = pm.get_installed_version(package_to_check)
print(f"'{package_to_check}' is installed, version: {version}")
else:
print(f"'{package_to_check}' is not installed.")
4. Conditional Installation Based on Version
Use install_if_missing()
with the version_specifier
argument:
# Ensure 'requests' version 2.25.0 or higher, but less than 3.0.0
specifier = ">=2.25.0,<3.0.0"
print(f"Ensuring 'requests' meets specifier: '{specifier}'")
if pm.install_if_missing("requests", version_specifier=specifier):
print("'requests' requirement met or installation/update succeeded.")
else:
print("Failed to meet 'requests' requirement.")
5. Install Multiple Packages (Only if Missing)
Use install_multiple_if_not_installed()
:
extras = ["rich", "tqdm"] # Example utility packages
print(f"Installing missing packages from: {extras}")
if pm.install_multiple_if_not_installed(extras):
print("Checked/installed extra utility packages.")
else:
print("Failed to install some utility packages.")
6. Uninstall a Package
Use uninstall()
(uncomment to run):
# print("Uninstalling 'tqdm'...")
# if pm.uninstall("tqdm"):
# print("'tqdm' uninstalled successfully.")
# else:
# print("Failed to uninstall 'tqdm' (or it wasn't installed).")
7. Asynchronous Installation (Example)
Use the async_ functions within an async def function:
async def install_async_package():
pkg = "aiohttp"
print(f"Asynchronously installing '{pkg}'...")
if await pm.async_install(pkg):
print(f"Async install of '{pkg}' succeeded.")
else:
print(f"Async install of '{pkg}' failed.")
# To run the async function:
# asyncio.run(install_async_package())
Next Steps
Explore the User Guide for detailed explanations of features like environment targeting, vulnerability scanning, and more advanced installation options. Check the API Reference for the full reference of all functions and classes.