Comments (4)
@rajat-packt Yep we can close this, thanks so much to frankie567 for being so willing to share knowledge
from building-data-science-applications-with-fastapi.
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 path
The path chapter3.chapter3_custom_response_04
is what we usually call the dot notation in Python. It's a way to tell Python the path of a module. In Python, a module is any valid Python file from which we can import functions or variables: chapter3_custom_response_04.py
is a module.
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 __init__.py
file (which can be totally empty) at the root of this directory. chapter3
has an __init__.py
file, so it's a package.
This is why we can tell Python to look for chapter3.chapter3_custom_response_04
: chapter3_custom_response_04
is a valid module inside chapter3
package.
The tricky __file__
variable
We use the __file__
variable to know the path of the current module. This variable adds a .
at some point to tell us where we run the script from. Here how we are getting the root directory from the example:
root_directory = path.dirname(path.dirname(__file__))
Basically, we take the parent of the parent of the current file. If we run it from the root directory, __file__
has the following value:
/Users/fvoron/Development/Building-Data-Science-Applications-with-FastAPI/./chapter3/chapter3_custom_response_04.py
So taking the parent twice returns us the right path:
/Users/fvoron/Development/Building-Data-Science-Applications-with-FastAPI/.
However, if we run it from chapter3
, __file__
has the following value:
/Users/fvoron/Development/Building-Data-Science-Applications-with-FastAPI/chapter3/./chapter3_custom_response_04.py
If we take the parent twice here, we get chapter3
(because the parent of .
is chapter3
...), which is not the path we want:
/Users/fvoron/Development/Building-Data-Science-Applications-with-FastAPI/chapter3
How to solve this?
A more robust solution would probably have been to make Python resolve the path so we don't have this problem. Doing this solves the problem whether we run the script from the root or chapter3
:
from os import path
from pathlib import Path
from fastapi import FastAPI
from fastapi.responses import FileResponse
app = FastAPI()
@app.get("/cat")
async def get_cat():
root_directory = Path(__file__).parent.parent
picture_path = path.join(root_directory, "assets", "cat.jpg")
return FileResponse(picture_path)
Basically, Path
is a more high-level library to help us manage path. When we pass it a string path, it'll resolve it completely so it'll discard the .
. Again, we get the parent twice.
About chapter6
and name clashing
This is a more or less related problem of where we run the script from. If we run it from the root directory like this, no problem:
uvicorn chapter6.sqlalchemy.app:app
We get the module app
inside the package sqlalchemy
, which is itself inside the chapter6
package.
Problems arise if we want to run it from chapter6
. My clumsiness here was to call the package sqlalchemy
which is in conflict with the library sqlalchemy
. In this context, Python will always choose the "closest" package, i.e. our package, and ignore the library. This is why we get cannot import name 'text' from 'sqlalchemy'
.
Why does it work when running from the root folder? Simply because there is no sqlalchemy
package in the root folder. If we had one, we would get the same problem. Once again, the "bad" thing I did was to name my package exactly like the library.
Hope it clarifies things!
from building-data-science-applications-with-fastapi.
@gitgithan let me know if we can close this issue now.
@frankie567 do tell me if there is something regarding this exchange that needs to be added to the README if it can benefit our other reader!
from building-data-science-applications-with-fastapi.
@gitgithan let me know if we can close this issue now.
@frankie567 do tell me if there is something regarding this exchange that needs to be added to the README if it can benefit our other reader!
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)
from building-data-science-applications-with-fastapi.
Related Issues (13)
- What is the use of fastapi.params? HOT 1
- Chapter 11 ndarray.copy is not deep copy HOT 5
- Refering to python built-in `id` instead of `comment.post_id` in HTTPException? HOT 2
- [Chapter 6] comment argument in create_comment() as CommentCreate instance HOT 1
- Chapter 9 external api test is missing the test function HOT 1
- Delete irrelevant files from the project
- Discussion: consider merging `with` statements HOT 2
- Too much data for declared Content-Length error in DELETE post (Chapter 3) HOT 1
- excute(update_query) return influenced rows HOT 6
- Chapter 4 `PostPartialUpdate` model errors after meeting `response_model=PostPublic` HOT 3
- Chapter3_headers_cookies_02.py calling from automatic docs forces user-agent entry that is ignored HOT 1
- Directory problems in Chapter 6 Alembic section HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from building-data-science-applications-with-fastapi.