Skip to content

Commit

Permalink
Fix 5701: avoid dev_tools/modules.py print_version failing (quantum…
Browse files Browse the repository at this point in the history
…lib#5705)

The implementation of the command `print_version` in `dev_tools/modules.py` involves (eventually) calling the function `list_modules()`.  That function finds modules by searching for files name `setup.py`, starting in the current directory. When the user runs `python dev_tools/modules.py print_version`, the current directory is the Cirq repo top-level directory.

`list_modules()` works by calling `_parse_module()` on every relative directory path found by locating the `setup.py` files recursively.  When it executes with a given directory path, `_parse_module()` tries to exec the `setup.py` file found therein. The specific error comes from the fact that the top-level `setup.py` file, _unlike_ all the sub-modules's `setup.py` files, includes imports.  Specifically, these imports:
```
from dev_tools import modules
from dev_tools.requirements import explode
```

Unfortunately, the current directory (i.e., the top-level Cirq directory) is not in Python's `sys.path` at the time `_parse_module()` exec's the `setup.py` file there, and so the import fails with
```
ModuleNotFoundError: No module named 'dev_tools'
```

This could probably be fixed in a number of ways. One is to make `_parse_module()` temporarily append the current path to `sys.path` before attempting to exec a `setup.py` file. Doing so should hopefully make Python import statements inside `setup.py` files work as expected.

That's what this 2-line code change does.
  • Loading branch information
mhucka authored and rht committed May 1, 2023
1 parent 9568582 commit 3da63e0
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dev_tools/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def _parse_module(folder: Path) -> Dict[str, Any]:

orig_setup = setuptools.setup
cwd = os.getcwd()
sys.path.insert(0, cwd)

def setup(**kwargs):
setup_args.update(kwargs)
Expand All @@ -199,6 +200,7 @@ def setup(**kwargs):
raise
finally:
setuptools.setup = orig_setup
sys.path.remove(cwd)
os.chdir(cwd)


Expand Down

0 comments on commit 3da63e0

Please sign in to comment.