Handling specific Python error within Bash call?
Question: I am using the `line_profiler`, which allows you to drop `@profile` decorators
anywhere in a python codebase and returns line output.

However, if you try to execute python code that contains one such `@profile`
decorator without loading this `line_profiler` module, the code will fail with
a NameError, for such a decorator is defined and injected by this external
library.

I'd like a bash command that attempts to run my python script with vanilla
python. Then, if and only if the error consists of `NameError`, I want to give
it a second try. This is what I have got so far:

    
    
    python -u $file || python -m kernprof -l -v --outfile=/dev/null $file"
    

The problem is of course that if my python code has ANY errors at all, be it
`ValueError` or `IndentationError` or anything, it tries the profiler. I want
to ONLY run the profiler if the error contains a string `NameError: name
'profile' is not defined` is found within `stderr`.


Answer: Wouldn't be better to monkey patch profile when no line_profiles is present ?

Something like

    
    
    try:
        import line_profiles
    except:
        import warnings
        warnings.warn("Profile disabled")
    
        def profile(fn):
            def wrapper(*args, **kw):
                return fn(*args, **kw)
            return wrapper
    

This way your code runs in either case without complicating matters.

