Skip to content

Commit e80b14b

Browse files
authored
Fix 5701: avoid dev_tools/modules.py print_version failing (#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.
1 parent b8bb277 commit e80b14b

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

dev_tools/modules.py

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def _parse_module(folder: Path) -> Dict[str, Any]:
183183

184184
orig_setup = setuptools.setup
185185
cwd = os.getcwd()
186+
sys.path.insert(0, cwd)
186187

187188
def setup(**kwargs):
188189
setup_args.update(kwargs)
@@ -199,6 +200,7 @@ def setup(**kwargs):
199200
raise
200201
finally:
201202
setuptools.setup = orig_setup
203+
sys.path.remove(cwd)
202204
os.chdir(cwd)
203205

204206

0 commit comments

Comments
 (0)