Fix traceback when included environment not present

Fixes #11534.

@basepi only f1d7d6f needs to be cherrypicked, here.