-
Notifications
You must be signed in to change notification settings - Fork 152
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
How does the way the app is run affect whether book code works? #8
Comments
Well, that may be something I overlooked because of the cat picture which is located at the root folder, not in chapter3. If you are in the root folder, you can run the example like this without any issue: uvicorn chapter3.chapter3_custom_response_04:app About dotted pathThe path Modules are contained in packages. Packages are special directories for Python. To make Python treat a directory as a package, you need to create an This is why we can tell Python to look for The tricky
|
@gitgithan let me know if we can close this issue now. |
@rajat-packt Yep we can close this, thanks so much to frankie567 for being so willing to share knowledge |
I got tripped up with the sqlalchemy directory name issue myself, and Google searches did not find this closed issue (I just happened to find this when looking to file a new issue on the matter) - would suggest an update to the README or any other online resource to suggest using a different name for that directory (and a note that it is never a good idea to name anything in your code the same as a reserved word, package/subpackage/module name, etc) |
Since
chapter3_first_endpoint_01
, we have been instructed to run uvicorn asuvicorn chapter3_first_endpoint_01:app
, implicitly meaning we should go inside the chapter3 folder first.Later I got stuck at
chapter3_custom_response_04
because I ran it asuvicorn chapter3_custom_response_04:app
, which starts uvicorn proper, but upon httpie query gives Internal Server Error.To investigate:
Output:
At that moment I just hacked my way through by wrapping another
path.dirname
after observing the folder structure to seeassets
folder is on parallel level as chapter folders.It is pretty impossible for the average reader to know that it has to be ran from outside chapter3, with
uvicorn chapter3.chapter3_custom_response_04:app
for the httpie GET to work, after being accustomed to all previous exercises being run from inside chapter3.Same 3 prints when run from outside chapter3:
Question 1/2
Why is there a dot added somewhere along the path? (It caused me to have to wrap a 3rd path.dirname to move up 1 more level past the dot when I ran it wrongly).
Is that vscode/fastapi/uvicorn's behaviour? What's the logic to where in the path, or when it is added? (making a simple
test.py
withprint(__file__)
inside chapter3 folder andpython -m test
doesn't show this extra dot)Only at
chapter6/sqlalchemy
did i realize what i did wrong in chapter3.Again I ran
uvicorn sqlalchemy.app:app
(inside chapter6 folder) and gotThen i saw
from chapter6.sqlalchemy.models import
in app.py and realized I must run it outside chapter6 withuvicorn chapter6.sqlalchemy.app:app
.Question 2/2
From the error I see
databases/core.py
is trying to import something from sqlalchemy.Is this a case of clashing names, and the sqlalchemy folder overwrote the sqlalchemy library because the folder came first in import path?
If this is true, why does running
uvicorn chapter6.sqlalchemy.app:app
outside chapter 6 correctly look for the sqlachemy library since the folder should still come first in import path? Is it because python does not recursively search into theBuilding-Data-Science-Applications-with-FastAPI
folder, so doesn't know there is a sqlachemy folder there too and thus no clash this time?P.S I tested
uvicorn sqlalchemy_relationship.app:app
inside chapter6 hoping there's no clash but get same errorThe text was updated successfully, but these errors were encountered: