Attempts to verify and auto-install missing Python packages at runtime or startup.
Project description
check-install (check_install)
Automatically detect and install missing Python modules at runtime or pre-execution using pip.
📥 Installation
pip install check-install
💡 What It Does
When imported and invoked early in your Python scripts, check_install scans for missing imports:
- Prompts to install them using
pip. - Restarts the script seamlessly after installation.
- Works both during import time and runtime (
ModuleNotFoundError). - Optionally displays nice output with
rich, but works without it. - Great for prototyping, scratch scripts, or educational notebooks.
✅ Quick Start
Add this to the very top of your script:
import check_install
check_install.install_hooks_and_preflight()
That’s it! If any module is missing, you’ll be prompted to install it.
📦 Example
Say your script contains:
import check_install
check_install.install_hooks_and_preflight()
import numpy
import yaml
And you don't have numpy or PyYAML installed — you’ll see an interactive prompt:
Missing Dependencies Detected
Import name PyPI package
────────────── ─────────────
numpy numpy
yaml PyYAML
Attempt to auto-install missing 2 package(s) now? [Y/n]:
It will install and re-execute the script.
🧠 How It Works
- Uses
astto statically analyze the current script and extract imports. - Catches
ModuleNotFoundErrorat runtime using a customsys.excepthook. - Attempts installation via
subprocess.run([python -m pip install ...]). - Tracks attempted installations to avoid loops.
⚠️ Notes
- Meant for dev/test scripts — not production!
- Respects existing venvs, shows interpreter context.
- Maps known import names to their PyPI equivalents (
PIL→pillow, etc). - Requires no external config, arguments, or setup.
🔒 Security & Safety
- Does not use
eval,exec, or shell calls. - Uses
subprocesssecurely to invokepip. - Gracefully exits or restarts using
os.execv.
🚫 Limitations
- Won’t resolve C/C++ build errors or OS-level dependencies.
- Assumes
pipis functional and available in the current Python environment.
🧪 Want Tests?
Let me know and I’ll whip up a test suite for you! 🧪
📜 License
MIT License — see the LICENSE file for full text.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file check_install-0.1.1.tar.gz.
File metadata
- Download URL: check_install-0.1.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba6a7d99288602cf9f6cf6cf28b109feca93a66404e4ac6d282e24997d6dfa27
|
|
| MD5 |
f04730506328b2ec6554876b4a8da263
|
|
| BLAKE2b-256 |
3ae6095e202eff6a2f83102a934c1e0e897fd2d9348d6a1a56f9d4c60377f34f
|
File details
Details for the file check_install-0.1.1-py3-none-any.whl.
File metadata
- Download URL: check_install-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a35777fb25b8b3a0b2dfc62b0e1ea09dc44111bebc61f07fafe43f499fc62dd0
|
|
| MD5 |
3d43afd42812e882debe1cef00eca0f7
|
|
| BLAKE2b-256 |
854f0c85de07b90de314e1697e97b7c479b94b61dd57f35b4fc848bc2ddbbd2d
|