Error getting request body: The `python-multipart` library must be installed to use form parsing.
(venv) G:\projects\python\fastprojects>pip list
python-dateutil 2.8.1 python-multipart 0.0.5 rope 0.17.0
@app.post('/token', tags=['safe'])
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
print('form_data', form_data)
user_dict = fake_users_db.get(form_data.username)
if not user_dict:
raise HTTPException(
status_code=400,
detail='Incorrect username or password'
)
user = UserInDB(**user_dict)
hashed_password = fake_hash_password(form_data.password)
if not hashed_password == user.hashed_password:
raise HTTPException(
status_code=400,
detail='Incorrect username or password'
)
return {'access_token': user.username, 'token_type': 'bearer'}
I don't know how to troubleshoot this problem
@q98765543221 pip install python-multipart
Using legacy setup.py install for python-multipart, since package 'wheel' is not installed. Installing collected packages: python-multipart Running setup.py install for python-multipart ... done Successfully installed python-multipart-0.0.5
?[31mERROR?[0m: Error getting request body: The python-multipart library must be installed to use form parsing
.
?[32mINFO?[0m: 127.0.0.1:13645 - "?[1mPOST /token HTTP/1.1?[0m" ?[31m400 Bad Request?[0m
A new project, same error
Are you sure you're running your app in the same environment as the one you're installing python-multipart?
(venv) G:\projects\python\fastapi2>uvicorn main:app --reload --port 8090
?[32mINFO?[0m: Uvicorn running on ?[1mhttp://127.0.0.1:8090?[0m (Press CTRL+C to quit)
?[32mINFO?[0m: Started reloader process [?[36m?[1m10000?[0m] using ?[36m?[1mstatreload?[0m
?[32mINFO?[0m: Started server process [?[36m14376?[0m]
?[32mINFO?[0m: Waiting for application startup.
?[32mINFO?[0m: Application startup complete.
?[32mINFO?[0m: 127.0.0.1:12865 - "?[1mGET /docs HTTP/1.1?[0m" ?[32m200 OK?[0m
?[32mINFO?[0m: 127.0.0.1:12865 - "?[1mGET /openapi.json HTTP/1.1?[0m" ?[32m200 OK?[0m
?[31mERROR?[0m: Error getting request body: The python-multipart library must be installed to use form parsing.
?[32mINFO?[0m: 127.0.0.1:12873 - "?[1mPOST /token HTTP/1.1?[0m" ?[31m400 Bad Request?[0m
?[31mERROR?[0m: Could not stop child process 14376: [Errno 10] No child processes
?[32mINFO?[0m: Stopping reloader process [?[36m?[1m10000?[0m]
(venv) G:\projects\python\fastapi2>pip list Package Version
fastapi 0.55.1 pip 20.1.1 pydantic 1.5.1 python-multipart 0.0.5 setuptools 47.1.1 six 1.15.0 starlette 0.13.2 wheel 0.34.2
This problem occurs when I use a virtual environment and does not occur when I use a system environment, where the project was created using pyCharm
Thanks for the help here @Kludex ! :clap: :bow:
I understand you solved your problem, so thanks for reporting back and closing the issue @q98765543221 :+1:
Hitting this exact same problem. It happens with the virtualenv
I figured it out. The error being generated right here:
https://github.com/tiangolo/fastapi/blob/5c576e42be8f8832f0e225beaa72153a256892d7/fastapi/dependencies/utils.py#L106
Prints out that the missing library is python-multipart. But this is wrong. If the error is printed out like the following:

It will show that the missing package is multipart, and adding this package resolves the issue.
Prints out that the missing library is python-multipart. But this is wrong.
Why is this wrong?
The error output instructs the user to install python-multipart, instead of multipart.
It's also possible that both need to be installed.
I don't understand. You need to install python-multipart as per the documentation. Why is it wrong that the error states that you need to install it? pip install multipart will install a different package than pip install python-multipart, both are imported with import multipart. Maybe that is the confusion? Either way, the error message is correct.
Do you have python-multipart and multipart NOT installed?
The program tells you to install python-multipart
Do you have python-multipart and NOT multipart installed?
The program still tells you to install python-multipart
The error that is swallowed up? It tells you that the program is missing multipart.
How is this error message correct? It's telling me invalid correction action. If both python-multipart and multipart are installed with multipart, then perhaps the error message should be changed to install multipart.
Do you have python-multipart and NOT multipart installed? The program still tells you to install python-multipart
This is not the behaviour on my machine. I do not have multipart installed in my virtual environment, and I do not get this error (macOS, Python 3.10, FastAPI 0.79). What is your environment (os/python/fastapi)?
Windows 10, python 3.9, FastAPI unknown
Do you have a minimal reproducible example of code that triggers this error for you? And please do check your FastAPI version, if it is not current then the first order of business would be to update FastAPI obviously ;)
The code is in a private repo that I can't share at this time.
But the reproduction case should be equivalent to the following:
@app.post("/upload")
async def upload(file: UploadFile = File(...)) -> PlainTextResponse:
"""Uploads a file to the server."""
return "ok"
Alight. Unfortunately, I am unable to reproduce this behaviour. At this point my suspicion is either an older version of FastAPI or a misconfiguration with your virtual environment but it's impossible to say for sure.
@zackees Would it be possible that you create a new public repo with a MWE and a requirements.txt that shows the problem you are facing?
I stripped down the code to reproduce this bug and I can't reproduce it anymore.
Regardless, swallowing up an exception (which shows that the missing library is multipart) is not a good idea.
I would have to disagree with that. The name of a package is independent of how you import it (unfortunately). For multipart support, there are two that are imported by import multipart. The exception will tell you wat the name of the package is by import statement, in this case multipart. However, the documentation is clear on that you should install python-multipart. I think it is warranted to replace the exception with a clearer instruction on what to do.
It even has a 'incorrect' statement for when you actually have multipart installed. So I really don't understand what the issue is now, especially if you cannot reproduce it on your end anymore.