Error when starting the fast api application:
Traceback (most recent call last):
File "project/app.py", line 1, in
from fastapi import FastAPI
File "/project/venv/lib/python3.8/site-packages/fastapi/__init__.py", line 7, in
from .applications import FastAPI
File "project/venv/lib/python3.8/site-packages/fastapi/applications.py", line 3, in
from fastapi import routing
File "project/venv/lib/python3.8/site-packages/fastapi/routing.py", line 6, in
from fastapi import params
File "project/venv/lib/python3.8/site-packages/fastapi/params.py", line 14, in
class Param(Schema):
TypeError: function() argument 'code' must be code, not str
Can you define your environment? Python version and installed packages:
python --version
python -m pip freeze
Running into the same issue. I wonder if it's related to Python 3.8 because it started happening when I upgraded my Docker container to python:3.8.0-slim.
Version: Python 3.8.0
Packages:
aiofiles==0.4.0
aiosqlite==0.10.0
asyncpg==0.18.3
ciso8601==2.1.2
Click==7.0
fastapi==0.43.0
h11==0.8.1
httptools==0.0.13
migri==0.1.3
pydantic==1.1.1
PyPika==0.35.16
sqlparse==0.3.0
starlette==0.12.9
tortoise-orm==0.15.1
typing-extensions==3.7.4.1
uvicorn==0.10.8
uvloop==0.14.0
websockets==8.1
@AndreiToader1 I found the issue. FastAPI doesn't work w/ pydantic v1. Modifying my requirements to pydantic==0.32.2 solved the problem for me.
See https://gitter.im/tiangolo/fastapi?at=5db558949c39821509922795
For what it鈥檚 worth, the pydantic v1 was almost certainly the source of the issue, but as of v0.44 fastapi now does support pydantic v1. Be aware there are a number of breaking changes that you may need to address when you upgrade to pydantic v1.
Sorry for the late response guys. I also solved the problem by changing the version of Pydantic. Since I'm new to python took me a while to figure it out. Thanks
Have the same issue after 0.45.0 release.
Installation removed pydantic 0.32.0 and force installing of version 1.2
Collecting fastapi==0.45.0
Downloading https://files.pythonhosted.org/packages/6b/f4/8aa81e9e5de3877a3149f7e53b21cc3de28d2a474692f17b79b6baf1deb8/fastapi-0.45.0-py3-none-any.whl (43kB)
|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 51kB 469kB/s
Collecting pydantic<2.0.0,>=0.32.2 (from fastapi==0.45.0)
Using cached https://files.pythonhosted.org/packages/c2/66/b084a7bb0e9762b3e2e03f5051a3f645208e6c8b266e8e253f9b26bf42b9/pydantic-1.2-py36.py37.py38-none-any.whl
Requirement already satisfied: starlette<=0.12.9,>=0.12.9 in c:\users\gosha\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (from fastapi==0.45.0) (0.12.9)
Installing collected packages: pydantic, fastapi
Found existing installation: pydantic 0.32
Uninstalling pydantic-0.32:
Successfully uninstalled pydantic-0.32
Found existing installation: fastapi 0.44.1
Uninstalling fastapi-0.44.1:
Successfully uninstalled fastapi-0.44.1
Successfully installed fastapi-0.45.0 pydantic-1.2
And after it i getting this:
Traceback (most recent call last):
File "C:/Users/gosha/PycharmProjects/nw_backend/main.py", line 4, in <module>
from newwheel_backend.fastapi_app_instance import app as initial_app
File "C:\Users\gosha\PycharmProjects\nw_backend\newwheel_backend\fastapi_app_instance.py", line 1, in <module>
from fastapi import FastAPI
File "C:\Users\gosha\PycharmProjects\sas\venv\lib\site-packages\fastapi\__init__.py", line 7, in <module>
from .applications import FastAPI
File "C:\Users\gosha\PycharmProjects\sas\venv\lib\site-packages\fastapi\applications.py", line 3, in <module>
from fastapi import routing
File "C:\Users\gosha\PycharmProjects\sas\venv\lib\site-packages\fastapi\routing.py", line 6, in <module>
from fastapi import params
File "C:\Users\gosha\PycharmProjects\sas\venv\lib\site-packages\fastapi\params.py", line 14, in <module>
class Param(Schema):
TypeError: function() argument 1 must be code, not str
My python version is 3.7.5.
I can solve the problem only by downgrading the version of Pydantic back to 0,32.0.
@ShagonRU It looks like there might be something messed up in your environment -- try adding
from fastapi import __version__; print(__version__) somewhere in your fastapi_app_instance.py. And make sure the actual runtime version of fastapi is the same as the version you just installed.
@dmontagu
Thank you! Your reply helped me to resolve this issue.
My version was 0.43.0 instead of installer 0.45.0 by some reason...
Thanks @prostomarkeloff and @dmontagu for the help here! :cake: :tada:
I think the original problem was solved, right @AndreiToader1 ?
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.
Most helpful comment
For what it鈥檚 worth, the pydantic v1 was almost certainly the source of the issue, but as of v0.44 fastapi now does support pydantic v1. Be aware there are a number of breaking changes that you may need to address when you upgrade to pydantic v1.