DLR-RM / DLR-RM/BlenderProc

`blenderproc.command_line:cli` in entrypoints but not importable / side effects in __init__.py

Open
#980 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Python
Stars
3.7k
Forks
518
PR merge metrics
No merged PRs in 30d

Description

### Describe the issue

The setup.py file [specifies](https://github.com/DLR-RM/BlenderProc/blob/3b4c4e4da069798cce36f419da79369fc72a73cb/setup.py#L21) `blenderproc.command_line:cli` as an "entry point", which [implies](https://packaging.python.org/en/latest/specifications/entry-points/#use-for-scripts) that it should be possible to import the `blenderproc.command_line` module and call its `cli` function like so:

```python
import sys
from blenderproc.command_line import cli
sys.exit(cli())
```

This ends up importing [`blenderproc`](https://github.com/DLR-RM/BlenderProc/blob/3b4c4e4da069798cce36f419da79369fc72a73cb/blenderproc/__init__.py#L57-L62) too and currently fails with:

```bash
❯ python
Python 3.10.12 (main, Jun 6 2023, 22:43:10) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from blenderproc.command_line import cli
Traceback (most recent call last):
File "", line 1, in
File "/nix/store/g0clcimnb9pmlil358yw176x3c57m4ph-python3-3.10.12-env/lib/python3.10/site-packages/blenderproc/__init__.py", line 60, in
raise RuntimeError("\n###############\nThis script can only be run by \"blenderproc run\", instead of calling:"
RuntimeError:
###############
This script can only be run by "blenderproc run", instead of calling:
python
call:
blenderproc run
###############
>>>
```

Another reproducer:

```bash
❯ python
>>> import importlib.metadata
>>> importlib.metadata.EntryPoint("blenderproc", value="blenderproc.command_line:cli", group="console_scripts").load()
Traceback (most recent call last):
File "", line 1, in
File "/nix/store/lx8vhp4fxclp494svlfis3sb2g8z4l9h-python3-3.10.12/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
module = import_module(match.group('module'))
File "/nix/store/lx8vhp4fxclp494svlfis3sb2g8z4l9h-python3-3.10.12/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 992, in _find_and_load_unlocked
File "", line 241, in _call_with_frames_removed
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/nix/store/g0clcimnb9pmlil358yw176x3c57m4ph-python3-3.10.12-env/lib/python3.10/site-packages/blenderproc/__init__.py", line 60, in
raise RuntimeError("\n###############\nThis script can only be run by \"blenderproc run\", instead of calling:"
RuntimeError:
###############
This script can only be run by "blenderproc run", instead of calling:
python
call:
blenderproc run
###############
```

It would resolve the issue, for example if all the side effects such as `raise` and `pip install`, weren't unconditionally invoked at import time, but rather were moved to a function that the user (and the `cli()`) would call explicitly. In addition, this would simplify packaging blenderproc in distributions other than PyPi

Thanks!

### Minimal code example

_No response_

### Files required to run the code

_No response_

### Expected behavior

Blenderproc CLI synopsis printed

### BlenderProc version

v2.6.1

### Related

- https://github.com/DLR-RM/BlenderProc/issues/901
- https://github.com/DLR-RM/BlenderProc/issues/840

...are related, except the present issue is more narrowly-scoped

Contributor guide

Open the contributing guide

Research direction

Start with the entry point in setup.py and the import-time behavior in blenderproc/__init__.py. Run the reported `from blenderproc.command_line import cli` reproducer and inspect how the command_line entry point loads. Done means the module imports without the current RuntimeError or other import-time side effects, and the CLI synopsis is printed as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.