Hacker News new | past | comments | ask | show | jobs | submit login

    try:
        try:
            import orjson as json
        except:
            try:
                import rapidjson as json
            except:
                try:
                    import fast_json as json
                except:
                    import json
        foo = json.loads(string)
    except:
        try:
            import yaml
        except:
            # try harder
            import os
            try:
                assert(os.system("pip3 install yaml") == 0)
            except:
                # try even harder
                try:
                    assert(os.system("sudo apt install python3-pip && pip3 install yaml") == 0)
                except:
                    assert(os.system("sudo yum install python3-pip && pip3 install yaml") == 0)
            import yaml
        try:
            foo = yaml.loads(string)
        except:
            try:
                ....



Great idea.

  pip install --user yaml
increases the chances it will work


A note to readers: it's not always a good idea to put automated software installation in a place that users don't expect it.

I've seen that kind of approach cause a ton of issues the moment that the software was used in a different environment than the author expected.

It's much better IMO to fail with a message about how to install the missing dependency.


This is why there should be a way to automatically install software into a sandboxed location, e.g. a virtualenv.

Considering we are having software drive cars today it should be trivial and I would say even arguably expected that software should be able to autonomously "figure out" how to run itself and avoid conflicts with other software since that's a trivial task in comparison to navigating city streets.


Brilliant! What license is this published under?


Free Art License




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

Search: