facebookresearch / facebookresearch/detectron2

setup.py treats torch as a setup-time dependency

Open
#4,472 7 comments 8 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34.7k
Forks
7.9k
PR merge metrics
No merged PRs in 30d

Description

## Instructions To Reproduce the 🐛 Bug:
1. create fresh virtualenv with python 3.8.5
2. run `pip install pip==22.2.2`
3. create requirements.txt file:

```
torch
torchvision
detectron2 @ git+https://github.com/facebookresearch/detectron2.git
```

4. run `pip install -r requirements.txt`
5. you will get an error

```
Collecting detectron2@ git+https://github.com/facebookresearch/detectron2.git
Cloning https://github.com/facebookresearch/detectron2.git to /tmp/pip-install-et917ffp/detectron2_f3545bf0ffe44ebcbd7f1785148fbfef
Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/detectron2.git /tmp/pip-install-et917ffp/detectron2_f3545bf0ffe44ebcbd7f1785148fbfef
Resolved https://github.com/facebookresearch/detectron2.git to commit 5aeb252b194b93dc2879b4ac34bc51a31b5aee13
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [6 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 34, in
File "/tmp/pip-install-et917ffp/detectron2_f3545bf0ffe44ebcbd7f1785148fbfef/setup.py", line 10, in
import torch
ModuleNotFoundError: No module named 'torch'
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
```

## Expected behavior:

Package should be installed successfully.

## Root of the problem:

setup.py checks minimal torch version [here](https://github.com/facebookresearch/detectron2/blob/main/setup.py#L14) by importing it. This requires torch to be not just runtime dependency, but also a setup-time dependency. Instead minimal torch version should be declared via `install_requires` (if its possible to avoid importing torch on installation) or torch should be declared as a setup-time dependency [this way](https://peps.python.org/pep-0518/#build-system-table).

## Workaround:
Install everything, except detectron2, then install detectron2.

``` shell
pip3 install --no-cache-dir --no-deps -r <(grep -vE '^detectron2' requirements.txt) && \
pip3 install --no-cache-dir --no-deps -r <(grep -E '^(detectron2|--find-links)' requirements.txt)
```
## Environment:

Everything should be clear from reproduce steps

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.