Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I created this fun hack that taught me a LOT about the import system. Basically it allows you to import anything you want, even specify a version, and it will fetch it from PyPI live. Might be interesting to flesh this out in a way that's deployable.

Basically instead of

    import tornado
and hoping and praying that the user has read you README and pip installed the right version of tornado, you can do

    from magicimport import magicimport
    tornado = magicimport("tornado", version = "4.5")
and you have exactly what you need, as long as there is an internet connection.

https://github.com/dheera/magicimport.py



Cool! But why not just throw the code a package with a version dependency on tornado?


Because then you'd still have to install the package.

It's nice to have something that "just works" without having to install it. I like to call it Level 5 autonomous software -- software that can figure out how to run itself with zero complaints.

I actually use this for a lot of personal non-production scripts, I can just clone the script on any system and just run it, it will figure itself out.

Also, packages with version dependencies fuck up other packages with other version dependencies, unless you set up virtualenvs or dockers or condas for them, and those take additional steps.

magicimport.py uses virtualenv behind the scenes, so when it imports tornado 4.5 because some script wants 4.5, it won't mess up the 6.0 that's already on your system. In some sense it just automates the virtualenv and pip install process on-the-fly, to give the effect that the script "just works".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: