# Buggy code
def ensure(self):
    """Ensure that a virtual environment exists, creating it if needed"""
    if not os.path.exists(self.path):
        logger.debug("%s does not exist; creating", self.path)
        self.create()
    elif not os.path.isdir(self.path):
        message = "%s exists but is not a directory" % self.path
        logger.error(message)
        raise VirtualEnvironmentError(message)
    elif not self._directory_is_venv():
        message = "Directory %s exists but is not a venv" % self.path
        logger.error(message)
        raise VirtualEnvironmentError(message)
    else:
        logger.debug("Found existing virtual environment at %s", self.path)

    self.ensure_interpreter()
    self.ensure_pip()


# Traceback (mu.virtual_environment.VirtualEnvironmentError)
2021-02-11 08:24:50,511 - root:172(run) INFO:

-----------------

Starting Mu 1.1.0.beta.1
2021-02-11 08:24:50,516 - root:173(run) INFO: uname_result(system='Darwin', node='Carlos-MBP-8.local', release='18.7.0', version='Darwin Kernel Version 18.7.0: Tue Jan 12 22:04:47 PST 2021; root:xnu-4903.278.56~1/RELEASE_X86_64', machine='x86_64', processor='i386')
2021-02-11 08:24:50,517 - root:174(run) INFO: Python path: ['/', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python37.zip', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/lib-dynload', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/setuptools-49.6.0-py3.7.egg', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/pip-20.2.2-py3.7.egg', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/IPython/extensions']
2021-02-11 08:24:50,517 - root:175(run) INFO: Language code: en_GB
2021-02-11 08:24:50,776 - mu.virtual_environment:371(ensure) DEBUG: Found existing virtual environment at /Users/microbit-carlos/Library/Application Support/mu/mu_venv
2021-02-11 08:24:50,776 - mu.virtual_environment:383(ensure_interpreter) ERROR: Interpreter not found where expected at /Users/microbit-carlos/Library/Application Support/mu/mu_venv/bin/python
2021-02-11 08:24:50,776 - root:106(excepthook) ERROR: Unrecoverable error
Traceback (most recent call last):
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/app.py", line 98, in run
venv.ensure()
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/virtual_environment.py", line 373, in ensure
self.ensure_interpreter()
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/virtual_environment.py", line 384, in ensure_interpreter
raise VirtualEnvironmentError(message)
mu.virtual_environment.VirtualEnvironmentError: Interpreter not found where expected at /Users/microbit-carlos/Library/Application Support/mu/mu_venv/bin/python
2021-02-11 08:24:50,777 - mu.settings:169(save) DEBUG: Saving to /Users/microbit-carlos/Library/Application Support/mu/session.json
2021-02-11 08:24:50,778 - mu.settings:169(save) DEBUG: Saving to /Users/microbit-carlos/Library/Application Support/mu/venv.json
2021-02-11 08:25:06,987 - root:172(run) INFO:

-----------------

Starting Mu 1.1.0.beta.1
2021-02-11 08:25:06,987 - root:173(run) INFO: uname_result(system='Darwin', node='Carlos-MBP-8.local', release='18.7.0', version='Darwin Kernel Version 18.7.0: Tue Jan 12 22:04:47 PST 2021; root:xnu-4903.278.56~1/RELEASE_X86_64', machine='x86_64', processor='i386')
2021-02-11 08:25:06,987 - root:174(run) INFO: Python path: ['/', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python37.zip', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/lib-dynload', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/setuptools-49.6.0-py3.7.egg', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/pip-20.2.2-py3.7.egg', '/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/IPython/extensions']
2021-02-11 08:25:06,987 - root:175(run) INFO: Language code: en_GB
2021-02-11 08:25:07,184 - mu.virtual_environment:371(ensure) DEBUG: Found existing virtual environment at /Users/microbit-carlos/Library/Application Support/mu/mu_venv
2021-02-11 08:25:07,184 - mu.virtual_environment:383(ensure_interpreter) ERROR: Interpreter not found where expected at /Users/microbit-carlos/Library/Application Support/mu/mu_venv/bin/python
2021-02-11 08:25:07,184 - root:106(excepthook) ERROR: Unrecoverable error
Traceback (most recent call last):
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/app.py", line 98, in run
venv.ensure()
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/virtual_environment.py", line 373, in ensure
self.ensure_interpreter()
File "/Users/microbit-carlos/Downloads/Mu Editor.app/Contents/Resources/Python/lib/python3.7/site-packages/mu/virtual_environment.py", line 384, in ensure_interpreter
raise VirtualEnvironmentError(message)
mu.virtual_environment.VirtualEnvironmentError: Interpreter not found where expected at /Users/microbit-carlos/Library/Application Support/mu/mu_venv/bin/python
2021-02-11 08:25:07,185 - mu.settings:169(save) DEBUG: Saving to /Users/microbit-carlos/Library/Application Support/mu/session.json
2021-02-11 08:25:07,186 - mu.settings:169(save) DEBUG: Saving to /Users/microbit-carlos/Library/Application Support/mu/venv.json
2021-02-11 08:25:15,345 - root:172(run) INFO:

-----------------

# Fixed code
def ensure(self):
    """Ensure that virtual environment exists and is in a good state"""
    self.ensure_path()
    self.ensure_interpreter()
    self.ensure_interpreter_version()
    self.ensure_pip()
    self.ensure_key_modules()