1

I am trying to deploy my app to Streamlit and it requires detectron2. I had to install detectron2 using

python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'

It requires torch to be already present as it'll throw an error there and then.

so when I did, pip freeze requirements.txt, the snippet looks like:

torch==1.12.1
torchaudio==0.12.1
torchvision==0.13.1
detectron2 @ git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55

so when I pip install -r requirements.txt, it gives me error as:

Collecting detectron2@ git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55
  Cloning https://github.com/facebookresearch/detectron2.git (to revision 717ab9f0aeca216a2f800e43d705766251ba3a55) to /tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5
  Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/detectron2.git /tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5
  Running command git rev-parse -q --verify 'sha^717ab9f0aeca216a2f800e43d705766251ba3a55'
  Running command git fetch -q https://github.com/facebookresearch/detectron2.git 717ab9f0aeca216a2f800e43d705766251ba3a55
  Resolved https://github.com/facebookresearch/detectron2.git to commit 717ab9f0aeca216a2f800e43d705766251ba3a55
  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 "<string>", line 36, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5/setup.py", line 10, in <module>
          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.

Deshwal
  • 3,436
  • 4
  • 35
  • 94
  • Can you install torch and then install this package? So two pip install commands. – jkr Sep 02 '22 at 10:26
  • I have control over my local but over `streamlit` cloud, they do the whole deployment part. You just need to put to put up `app.py` , `requirements.txt` – Deshwal Sep 02 '22 at 10:35
  • 3
    Build-time dependencies, install-time dependencies and run-time dependencies are 3 different kinds of dependencies, you cannot put all of them into a single `requirements.txt` and expect it to work. You need to go with `setup.cfg`, `setup.py` and `pyproject.toml`. – phd Sep 02 '22 at 11:29
  • take a look at the `pyproject.toml` specification ([PEP 518](https://peps.python.org/pep-0518/)) and in particular the [build-system section](https://peps.python.org/pep-0518/#build-system-table) – jkr Sep 02 '22 at 12:07

1 Answers1

0

Not a great work but I found a kind of way around it. So I just removed detectron2 from requirements.txt and installed at runtime using subprocess inside app.py. Not recommended but it just did the work for me. Please suggest a better way to do it. Thanks :)

try:
    from detectron2.config import get_cfg  
except ModuleNotFoundError:
    import subprocess
    import sys
    subprocess.check_call([sys.executable, "-m", "pip", "install", 'git+https://github.com/facebookresearch/detectron2.git'])

Deshwal
  • 3,436
  • 4
  • 35
  • 94