Skip to content

Bug: ImportError: cannot import name 'MultipartSegment' from 'multipart' #4000

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

Closed
1 of 4 tasks
umarbutler opened this issue Feb 14, 2025 · 6 comments
Closed
1 of 4 tasks
Labels
Bug 🐛 This is something that is not working as expected

Comments

@umarbutler
Copy link

umarbutler commented Feb 14, 2025

Description

After upgrading to Litestar 2.14.0, I am now getting this error:

Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 112, in _get_module_details
  File "/home/umar/.../api-server/app/__init__.py", line 1, in <module>
    from .app import jumpstart_app
  File "/home/umar/.../api-server/app/app.py", line 7, in <module>
    from litestar import Litestar
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/__init__.py", line 1, in <module>
    from litestar.app import Litestar
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/app.py", line 19, in <module>
    from litestar._asgi import ASGIRouter
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/_asgi/__init__.py", line 1, in <module>
    from litestar._asgi.asgi_router import ASGIRouter
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/_asgi/asgi_router.py", line 9, in <module>
    from litestar._asgi.routing_trie import validate_node
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/_asgi/routing_trie/__init__.py", line 1, in <module>
    from litestar._asgi.routing_trie.mapping import add_route_to_trie
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/_asgi/routing_trie/mapping.py", line 12, in <module>
    from litestar.types.internal_types import PathParameterDefinition
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/types/__init__.py", line 77, in <module>
    from .internal_types import ControllerRouterHandler, ReservedKwargs, RouteHandlerMapItem, RouteHandlerType
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/types/internal_types.py", line 5, in <module>
    from litestar.utils.deprecation import warn_deprecation
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/utils/__init__.py", line 24, in <module>
    from .scope import (  # type: ignore[attr-defined]
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/utils/scope/__init__.py", line 5, in <module>
    from litestar.serialization import get_serializer
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/serialization/__init__.py", line 1, in <module>
    from .msgspec_hooks import (
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/serialization/msgspec_hooks.py", line 22, in <module>
    from litestar.datastructures.secret_values import SecretBytes, SecretString
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/datastructures/__init__.py", line 2, in <module>
    from litestar.datastructures.headers import (
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/datastructures/headers.py", line 26, in <module>
    from litestar._multipart import parse_content_header
  File "/home/umar/.../.venv/lib/python3.12/site-packages/litestar/_multipart.py", line 7, in <module>
    from multipart import (  # type: ignore[import-untyped]
ImportError: cannot import name 'MultipartSegment' from 'multipart' (/home/umar/.../.venv/lib/python3.12/site-packages/multipart/__init__.py)

I am using the latest version of multipart, which I believe was installed by Litestar.

Could #3872 be relevant here?

URL to code causing the issue

No response

MCVE

# Your MCVE code here

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

Litestar Version

2.14.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@umarbutler umarbutler added the Bug 🐛 This is something that is not working as expected label Feb 14, 2025
@euri10
Copy link
Contributor

euri10 commented Feb 14, 2025

have you tried just recreating your venv entirely ?
just asking because afair there were some name clashes between multipart libraries

@umarbutler
Copy link
Author

Figured it out. There is a packaged named python-multipart that uses the multipart namespace which conflicts with the multipart package???? 🤦

It gets better, python-multipart has 27.8 million monthly downloads whereas multipart has "only" 1 million. Not great.

I never installed python-multipart directly so clearly it must've been installed by another package.

So statistically, there is a high chance this errors pops up for more Litestar users as they upgrade.

For now I just uninstalled python-multipart from my envrionment and I'll have to work out some other time why it got installed.

@euri10
Copy link
Contributor

euri10 commented Feb 14, 2025

Figured it out. There is a packaged named python-multipart that uses the multipart namespace which conflicts with the multipart package???? 🤦

It gets better, python-multipart has 27.8 million monthly downloads whereas multipart has "only" 1 million. Not great.

I never installed python-multipart directly so clearly it must've been installed by another package.

So statistically, there is a high chance this errors pops up for more Litestar users as they upgrade.

For now I just uninstalled python-multipart from my envrionment and I'll have to work out some other time why it got installed.

what happened is previous litestar version used python-multipart so you got that into you venv from us.

for whatever reason the upgrade didnt remove that package, and we now use multipart as you noticed, there is some namespace clashes between both libraries, hence your error.

on the download difference I'd say dont judge package by that, instead take a look at this https://github.com/defnull/multipart_bench and you'll get a sense as to why using the less downloaded library is clearly the better option, it's safer, faster, and you have less chances to get ddos

@euri10 euri10 closed this as completed Feb 14, 2025
@provinzkraut
Copy link
Member

what happened is previous litestar version used python-multipart so you got that into you venv from us.

We didn't use that. Before switching to multipart, we implemented our own parser.


The name clash is a known issue by the way, and there have been steps taken to resolve it: pypa/packaging-problems#818

@euri10
Copy link
Contributor

euri10 commented Feb 14, 2025

what happened is previous litestar version used python-multipart so you got that into you venv from us.

We didn't use that. Before switching to multipart, we implemented our own parser.

The name clash is a known issue by the way, and there have been steps taken to resolve it: pypa/packaging-problems#818

oups my bad, not sure then how you got that into your venv then :)

@umarbutler
Copy link
Author

what happened is previous litestar version used python-multipart so you got that into you venv from us.

We didn't use that. Before switching to multipart, we implemented our own parser.
The name clash is a known issue by the way, and there have been steps taken to resolve it: pypa/packaging-problems#818

oups my bad, not sure then how you got that into your venv then :)

The .venv is shared by multiple projects and I often try new packages and then trash them so the responsible dependency might not even be around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

3 participants