Giter VIP home page Giter VIP logo

importation's Introduction

Importation ๐Ÿ›ธ

automagically install missing imports

PyPI version

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 "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.

# 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 "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'httpx'
>>> import importation
>>> import httpx
>>> print(httpx)
<module 'httpx' from '/home/importation/__pypackages__/importation/lib/python3.8/site-packages/httpx/__init__.py'>

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 and magicimport.py.

importation's People

Contributors

cs01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.