Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] opencv-python vs opencv-python-headless dependency issue #298

Closed
tfurmston opened this issue Jan 20, 2021 · 8 comments · Fixed by #312
Closed

[Bug] opencv-python vs opencv-python-headless dependency issue #298

tfurmston opened this issue Jan 20, 2021 · 8 comments · Fixed by #312
Labels
documentation Improvements or additions to documentation

Comments

@tfurmston
Copy link

Important Note: We do not do technical support, nor consulting and don't answer personal questions per email.
Please post your question on reddit or stack overflow in that case.

If your issue is related to a custom gym environment, please use the custom gym env template.

🐛 Bug

Currently you have your setup.py file configured to install opencv-python. However, in your docker file you then uninstall opencv-python and install opencv-python-headless. I assume this is because you get a segmentation fault in the docker container unless you do this step.

I have a repo with stable-baselines3 as a dependency. I had my tests working locally (macOS), but got a segmentation fault in CI (ubuntu).

I use Poetry, so I have resolved the issue using environment markers in my dependency configuration.

`
stable-baselines3 = "0.10.0"

atari_py = {version = "~0.2"}

opencv-python-headless = {version = "~4.5", markers = "sys_platform == 'linux'"}

opencv-python = {version = "~4.5", markers = "sys_platform == 'darwin'"}
`

If you used poetry you could take a similar approach, which would be cleaner than your current solution, I think.

To Reproduce

Install stable-baselines3[extra] in ubuntu and then run the following:

from stable_baselines3 import ...
Current thread 0x00007f0eee355740 (most recent call first):
  File "/usr/local/lib/python3.7/ctypes/__init__.py", line 364 in __init__
  File "/usr/local/lib/python3.7/site-packages/torch/__init__.py", line 142 in _load_global_deps
  File "/usr/local/lib/python3.7/site-packages/torch/__init__.py", line 189 in <module>
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/usr/local/lib/python3.7/site-packages/stable_baselines3/a2c/a2c.py", line 3 in <module>
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/usr/local/lib/python3.7/site-packages/stable_baselines3/a2c/__init__.py", line 1 in <module>
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/usr/local/lib/python3.7/site-packages/stable_baselines3/__init__.py", line 3 in <module>
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 953 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 953 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/build/offpolicydqn/envs/crmsubscriber/env.py", line 19 in <module>
  File "<frozen importlib._bootstrap>", line 219 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/Users/thomas.furmston/farfetch/code/marketplace-marketing-offpolicydqn/tests/offpolicydqn/envs/crmsubscriber/test_env.py", line 7 in <module>
  File "/usr/local/lib/python3.7/site-packages/_pytest/assertion/rewrite.py", line 152 in exec_module
  File "<frozen importlib._bootstrap>", line 677 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 967 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 983 in _find_and_load
  File "/usr/local/lib/python3.7/site-packages/py/_path/local.py", line 704 in pyimport
  File "/usr/local/lib/python3.7/site-packages/_pytest/python.py", line 511 in _importtestmodule
  File "/usr/local/lib/python3.7/site-packages/_pytest/python.py", line 443 in _getobj
  File "/usr/local/lib/python3.7/site-packages/_pytest/python.py", line 261 in obj
  File "/usr/local/lib/python3.7/site-packages/_pytest/python.py", line 459 in _inject_setup_module_fixture
  File "/usr/local/lib/python3.7/site-packages/_pytest/python.py", line 446 in collect
  File "/usr/local/lib/python3.7/site-packages/_pytest/runner.py", line 264 in <lambda>
  File "/usr/local/lib/python3.7/site-packages/_pytest/runner.py", line 244 in from_call
  File "/usr/local/lib/python3.7/site-packages/_pytest/runner.py", line 264 in pytest_make_collect_report
  File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/usr/local/lib/python3.7/site-packages/_pytest/runner.py", line 382 in collect_one_node
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 681 in genitems
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 684 in genitems
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 490 in _perform_collect
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 452 in perform_collect
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 257 in pytest_collection
  File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 246 in _main
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 191 in wrap_session
  File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 240 in pytest_cmdline_main
  File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87 in <lambda>
  File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 93 in _hookexec
  File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__
  File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 125 in main
  File "/usr/local/bin/pytest", line 8 in <module>
Segmentation fault

Expected behavior

No segmentation fault.

### System Info

Describe the characteristic of your environment:

  • Describe how the library was installed (pip, docker, source, ...) - pip
  • GPU models and configuration - cpu
  • Python version - 3.7
  • PyTorch version - 1.7
  • Gym version - 1.7
  • Versions of any other relevant libraries

Additional context

Add any other context about the problem here.

Checklist

  • [ x ] I have checked that there is no similar issue in the repo (required)
  • [ x ] I have read the documentation (required)
  • [ x ] I have provided a minimal working example to reproduce the bug (required)
@tfurmston tfurmston added the bug Something isn't working label Jan 20, 2021
@araffin
Copy link
Member

araffin commented Jan 20, 2021

Hello,

I had my tests working locally (macOS), but got a segmentation fault in CI (ubuntu).

you mean that it works locally but not in the CI?
The reason we install the headless version for the CI and for docker is that there is no X-server (so you cannot use the render(mode="human")) and I think it results in an error due to missing library/x-server (but not a seg fault) if you don't do that.

I think the current setup is correct for the normal user: open cv non-headless version is required when possible (to use cv2.imshow() if I recall, unless you can do that too with the headless version...) but is installed only as an extra (pip install stable-baselines3 does not install it, but gym probably does...).

@tfurmston
Copy link
Author

tfurmston commented Jan 20, 2021

you mean that it works locally but not in the CI? -> Yes, exactly.

Sorry, I was installing pip install stable-baselines3[extra], not pip install stable-baselines3. I believe gym[atari] and stable-baselines3[extra] both install opencv-python, so the issue is the same with both. I did check both options.

@tfurmston
Copy link
Author

The reason we install the headless version for the CI and for docker is that there is no X-server (so you cannot use the render(mode="human")) and I think it results in an error due to missing library/x-server (but not a seg fault) if you don't do that. I was getting a segmentation fault. If you try installing into the standard python 3.7 docker image, I believe you will get the error.

@araffin
Copy link
Member

araffin commented Jan 20, 2021

I did a quick test and the SB3 import works fine in the docker image (I can run a PPO model), however I get this error (not a seg fault) when I try to import cv2:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

However, if you use a fake display, as we do in the rl zoo (see this folder), then you can use opencv-python in docker.

@tfurmston
Copy link
Author

Sorry, I was probably not clear. I am sure the docker image itself is fine.

My point is more that the dependencies of the Python package are set up in a confusing mannner, which requires this deletion of a opencv-python and then installation of opencv-python-headless. I was suggesting this could be improved, or atleast more clearly documented.

@tfurmston
Copy link
Author

tfurmston commented Jan 21, 2021

An alternative, at least with poetry, would be something like the following:

`
atari_py = {version = "~0.2"}

opencv-python-headless = {version = "~4.5", optional = true}

opencv-python = {version = "~4.5", optional = true}

[tool.poetry.extras]

extra = ["atari_py", "opencv-python"]

extra-headless = ["atari_py", "opencv-python-headless"]
`

and then people can install either opencv-python or opencv-python-headless as they require, including in your docker file. It would be something like poetry install --extras "extra" or poetry install --extras "extra-headless".

Not sure if this is possible with setup.py as I don't have a lot of experience with setting up projects in that manner.

I would be happy to try to submit a PR that sets the project using poetry, if you are interested.

@araffin araffin added documentation Improvements or additions to documentation and removed bug Something isn't working labels Jan 21, 2021
@araffin
Copy link
Member

araffin commented Jan 21, 2021

I was suggesting this could be improved, or atleast more clearly documented.

As it is quite a specific problem. I would rather document it, we already have that [extra] option and I don't think we need to add an option that may not be used that often.
I would be happy if you could submit a PR to update the documentation =) (I'm not sure what is the best section: probably "installation" or "developer guide")

@tfurmston
Copy link
Author

OK, cool.

Yes, it is a bit niche, I suppose. :)

I'll submit a PR for the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants