Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ugly exception warning when only cirq-core is installed #4106

Closed
balopat opened this issue May 13, 2021 · 3 comments · Fixed by #4324
Closed

Ugly exception warning when only cirq-core is installed #4106

balopat opened this issue May 13, 2021 · 3 comments · Fixed by #4324
Labels
area/imports area/install area/packages kind/bug-report Something doesn't seem to work. triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@balopat
Copy link
Contributor

balopat commented May 13, 2021

Description of the issue

We produce an ugly warning that cirq_google is missing if the user only installs cirq-core.

How to reproduce the issue

  1. Create an empty virtual env.
  2. pip install cirq-core
  3. python -c 'import cirq'
>>> import cirq
--- Logging error ---
Traceback (most recent call last):
  File "/Users/balintp/.virtualenvs/tempenv-4e8e54466376/lib/python3.8/site-packages/cirq/__init__.py", line 576, in <module>
    _compat.deprecated_submodule(
  File "/Users/balintp/.virtualenvs/tempenv-4e8e54466376/lib/python3.8/site-packages/cirq/_compat.py", line 546, in deprecated_submodule
    new_module = importlib.import_module(new_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cirq_google'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1081, in emit
    msg = self.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 925, in format
    return fmt.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 664, in format
    record.message = record.getMessage()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 369, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/balintp/.virtualenvs/tempenv-4e8e54466376/lib/python3.8/site-packages/cirq/__init__.py", line 585, in <module>
    warning("Can't import cirq.google: ", ex)
Message: "Can't import cirq.google: "
Arguments: (ModuleNotFoundError("No module named 'cirq_google'"),)

Cirq version
v0.11

@mpharrigan
Copy link
Collaborator

This is now a problem with aqt and ionq

@mpharrigan
Copy link
Collaborator

Ideas:

  1. Don't print the full exception, which is long and says its an Error when it's actually a warning. I was legitimately surprised to see that import cirq had actually succeeded since I saw so many exceptions in red text

  2. Upon import error, define the subpackages to a shim class that will throw a meaningful exception when you try to use it

try:
  deprecated_submodule(cirq_google)
except ImportError:
  sys.modules['cirq.google'] = ValueError("Not installed")

@balopat
Copy link
Contributor Author

balopat commented Jul 15, 2021

  1. I created a better error message - it does print the whole exception though for prosperity, otherwise we won't surface the true error.
  2. I followed this idea of "deferred/lazy error throwing" though I was skeptical first, and came up with better error messaging when deprecated module can't be imported #4324 - I think it works out pretty well in the end.

CirqBot pushed a commit that referenced this issue Jul 15, 2021
Adds better error messaging when deprecated module can't be imported.

Now, `import cirq` prints zero warnings, even if there are errors importing the modules.
Instead, when the deprecated modules are used, an informative error is thrown, including the root cause error: 

When `cirq_pasqal` can't be imported, `import cirq.pasqal` results in: 

```
Traceback (most recent call last):
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 590, in deprecated_submodule
    new_module = importlib.import_module(new_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cirq_pasqal'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 914, in _find_spec
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 504, in find_spec
    raise self.broken_module_exception
cirq._compat.DeprecatedModuleImportError: cirq_pasqal cannot be imported. The typical reasons are that
 1.) cirq_pasqal is not installed, or
 2.) when developing Cirq, you don't have your PYTHONPATH setup. In this case run `source dev_tools/pypath`.

 You can check the detailed exception above for more details or run `import cirq_pasqal to reproduce the issue.

```

Note that `from cirq import pasqal` will still run and return a dummy module that will throw the same error when trying to access any of its attributes.

Fixes #4106.
Fixes #4291.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…tumlib#4324)

Adds better error messaging when deprecated module can't be imported.

Now, `import cirq` prints zero warnings, even if there are errors importing the modules.
Instead, when the deprecated modules are used, an informative error is thrown, including the root cause error: 

When `cirq_pasqal` can't be imported, `import cirq.pasqal` results in: 

```
Traceback (most recent call last):
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 590, in deprecated_submodule
    new_module = importlib.import_module(new_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cirq_pasqal'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 914, in _find_spec
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 504, in find_spec
    raise self.broken_module_exception
cirq._compat.DeprecatedModuleImportError: cirq_pasqal cannot be imported. The typical reasons are that
 1.) cirq_pasqal is not installed, or
 2.) when developing Cirq, you don't have your PYTHONPATH setup. In this case run `source dev_tools/pypath`.

 You can check the detailed exception above for more details or run `import cirq_pasqal to reproduce the issue.

```

Note that `from cirq import pasqal` will still run and return a dummy module that will throw the same error when trying to access any of its attributes.

Fixes quantumlib#4106.
Fixes quantumlib#4291.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/imports area/install area/packages kind/bug-report Something doesn't seem to work. triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants