https://github.com/cs01/importation
automagically install missing Python packages 🛸
https://github.com/cs01/importation
Last synced: 6 months ago
JSON representation
automagically install missing Python packages 🛸
- Host: GitHub
- URL: https://github.com/cs01/importation
- Owner: cs01
- License: mit
- Archived: true
- Created: 2020-11-06T07:16:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-06T07:28:56.000Z (over 5 years ago)
- Last Synced: 2025-09-29T18:29:42.787Z (9 months ago)
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Importation 🛸
*automagically install missing imports*
Is this a good idea? Probably not.
Should you use this? Probably not.
## What is this?
`importation` hijacks the `import` keyword in Python to test if the module is importable, and auto-installs missing packages.
If you're in a virtual environment, it installs it to your current virtual environment. If not, it creates one at `__pypackages__/importation`, adds it to `sys.path` so packages are discoverable, and installs missing packages there.
It takes [PEP-582](https://www.python.org/dev/peps/pep-0582/) "Python local packages directory" one step further by resolving packages in `__pypackages__` plus auto-installing to them.
## How do I get it?
```
> python -m pip install importation --user
```
## How do I use it?
Just import it.
The act of importing it has the side effect of hijacking Python's import system.
```python
# example.py
import importation # noqa: 401
import httpx
print("module resolved at", httpx.__file__)
```
Then
```
$ python test.py
# module resolved at /home/__pypackages__/importation/lib/python3.8/site-packages/httpx/__init__.py
```
Another example with an interactive terminal:
```
>>> import httpx
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'httpx'
>>> import importation
>>> import httpx
>>> print(httpx)
```
To debug or view details of what it's doing set the `IMPORTATION_VERBOSE` environment variable:
```
$ IMPORTATION_VERBOSE=1 python test.py
```
## Disclaimer
This has not been tested beyond this extremely simple example.
## Credits
This package was inspired by [PEP-582](https://www.python.org/dev/peps/pep-0582/) and [magicimport.py](https://github.com/dheera/magicimport.py).