sanic icon indicating copy to clipboard operation
sanic copied to clipboard

Sanic failed to start when installed using PDM

Open pyx opened this issue 2 years ago • 11 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

As mentioned in the the book Python Web Developmentwith Sanic, I installed sanic with pdm:

$ mkdir test-sanic
$ cd test-sanic
$ pdm init
... with all default settings
$ pdm run sanic

It failed with: ModuleNotFoundError: No module named 'pkg_resources'

Same error within virtual environment created by pdm as:

$ . .venv/bin/activate
[.venv]$ sanic

python version 3.11 sanic version 23.6.0 pdm version 2.9.2

Code snippet

No response

Expected Behavior

Should be able to run sanic CLI, it worked with virtual environment I created manually.

How do you run Sanic?

Sanic CLI

Operating System

Linux

Sanic Version

23.6.0

Additional context

No response

pyx avatar Sep 25 '23 04:09 pyx

Confirmed. This appears to be an upstream issue with tracerite

[💻 ssadowski@q|test-sanic]$ pdm run sanic
Traceback (most recent call last):
  File "/playground/test-sanic/.venv/bin/sanic", line 5, in <module>
    from sanic.__main__ import main
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/__init__.py", line 6, in <module>
    from sanic.app import Sanic
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/app.py", line 55, in <module>
    from sanic.application.state import ApplicationState, ServerStage
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/application/state.py", line 13, in <module>
    from sanic.server.async_server import AsyncioServer
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/server/__init__.py", line 5, in <module>
    from sanic.server.runners import serve
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/server/runners.py", line 6, in <module>
    from sanic.config import Config
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/config.py", line 13, in <module>
    from sanic.errorpages import DEFAULT_FORMAT, check_error_format
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/errorpages.py", line 26, in <module>
    from sanic.pages.error import ErrorPage
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/sanic/pages/error.py", line 3, in <module>
    import tracerite.html
  File "/playground/test-sanic/.venv/lib/python3.11/site-packages/tracerite/__init__.py", line 1, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

Tagging in @Tronic and it may make sense to open a correlating issue there.

sjsadowski avatar Sep 25 '23 20:09 sjsadowski

Looks like it is missing setuptools (where that module comes from). Normally Python comes with that installed, but in virtual envs it is apparently not available. Can you try pip install setuptools manually within PDM virtual env and report back?

If that fixes it, we are just missing that dependency in tracerite.

Tronic avatar Sep 26 '23 18:09 Tronic

I added setuptools and it failed without error (correctly, I might add, because there's nothing for it to actually run yet) so I'd say that seems to be accurate. I'll create a quick stub for sanic after work and verify.

sjsadowski avatar Sep 26 '23 18:09 sjsadowski

Looks like it is missing setuptools (where that module comes from). Normally Python comes with that installed, but in virtual envs it is apparently not available. Can you try pip install setuptools manually within PDM virtual env and report back?

If that fixes it, we are just missing that dependency in tracerite.

Yes, you are correct. After pdm add setuptools, it works fine.

pyx avatar Sep 26 '23 22:09 pyx

Fixed (not tested) on Tracerite git now. Needs a new tracerite release, @ahopkins can you push one out?

Tronic avatar Sep 26 '23 23:09 Tronic

@Tronic I am trying to revitalize some of my sanic plugin projects, and found this, it mentioned Tracerite, seems to be related, you might find it interesting.

$ pdm run pytest tests/
============================= test session starts =============================
platform linux -- Python 3.11.5, pytest-7.4.2, pluggy-1.2.0
rootdir: /home/pyx/projects/sanic-wtf
plugins: anyio-3.7.1, cov-4.1.0
collected 12 items                                                            

tests/test_form.py ........                                             [ 66%]
tests/test_utils.py .                                                   [ 75%]
tests/test_validators.py ...                                            [100%]

============================== warnings summary ===============================
.venv/lib/python3.11/site-packages/tracerite/__init__.py:1
  /home/pyx/projects/sanic-wtf/.venv/lib/python3.11/site-packages/tracerite/__init__.py:1: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
    import pkg_resources

.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871
  /home/pyx/projects/sanic-wtf/.venv/lib/python3.11/site-packages/pkg_resources/__init__.py:2871: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('sphinxcontrib')`.
  Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
    declare_namespace(pkg)

.venv/lib/python3.11/site-packages/websockets/connection.py:9
  /home/pyx/projects/sanic-wtf/.venv/lib/python3.11/site-packages/websockets/connection.py:9: DeprecationWarning: websockets.connection was renamed to websockets.protocol and Connection was renamed to Protocol
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

pyx avatar Oct 03 '23 13:10 pyx

Upcoming tracerite-1.1.1 (git main) no longer uses pkg_resources so this issue is resolved for good. Still needs Tracerite pypi release and then Sanic depending on the new one.

Tronic avatar Oct 09 '23 23:10 Tronic

Tracerite 1.1.1 still depends on pkg_resources so I still get this error.

rmorshea avatar Dec 08 '23 06:12 rmorshea

Tracerite 1.1.1 still depends on pkg_resources so I still get this error.

Sorry about that. Can you try if the PR solves this issue so we could get that released as another version... https://github.com/sanic-org/tracerite/pull/10

Tronic avatar Dec 11 '23 08:12 Tronic

confirmed still an issue with sanic v23.12.1 as of Feb 13, 2024.

installing setuptools to the virtualenv fixed the issue for me.

edit: I was not using PDM, just new virtualenv, pip install sanic, copy the hello world code, and sanic server and got the error, until i added setuptools to the virtualenv.

rayanth avatar Feb 13 '24 14:02 rayanth

BTW https://github.com/astral-sh/uv

bgraceful avatar Apr 22 '24 00:04 bgraceful