how to get static file path without reading the file
hyansuper opened this issue · 2 comments
Hi, I've read your answer on How to read a (static) file from inside a Python package? from stackoverflow.
My question is I want to pack a Sanic project into whl file (Sanic is like an async version of Flask server), sometimes I just need to specifiy the static file path without reading it:
app.static('/', './static/index.html', content_type="text/html; charset=utf-8")
ref,
Do you know how I can do that?
Thank you!
@hyansuper Hi, the github issue tracker is not really the right place for support/Q&A, it's for reporting issues with the project. I suggest to ask your question directly on stackoverflow instead. Thanks!
Hi Wim,
Thanks for the clear example of how to open a file for reading within a package. But sometimes external packages imported into a module are used to take care of that. They only need a string to find the resource file. This looks like the problem hyansuper describes above.
An answer given on stackoverflow.com/questions/779495/access-data-in-package-subdirectory - but down-voted (because, apparently, not zip-safe) on the above discussion - , os.path.join(os.path.dirname(__file__),'')
, seems to work for me.
The equivalent-looking importlib.resources.path(package, resource)
gives a context manager that can be unsuitable for other parsers; see below example).
And another option, pkg_resources.resource_string('pkg_name', 'resources/file')
, would that not generate too much overhead?
Can I therefore suggest a rephrasing of this issue: Could you please elaborate/expand your example of how a module can get access to a resource in a package by using a string describing the resource file name? Maybe an example explains this better:
structure:
project/pkg/module
project/pkg/resources/file.csv
project/pkg/resources/get_resource.py
in module:
import pandas as pd
from pkg.resources.get_resource import RESOURCE
...
df = pd.read_csv(RESOURCE)
EDIT:
in pkg.resource.file-descriptor:
from pathlib import Path
pad = Path(__file__).parent
RESOURCE = f'{pad}/file.csv' # from
RESOURCE = f'{pad}/../../storage/produced-file2.csv' # to get to another, package-created folder 'storage'
#RESOURCE = '../resources/file.csv' # Also works but only when calling script sits correctly in the relative path
Has this before edit:
in pkg.resource.file-descriptor:
import os
pad = os.path.join(os.path.dirname(__file__),'')
RESOURCE = f'{pad}/file.csv' # works but only when calling script sits correctly in the relative path
# RESOURCE = 'file.csv' # No such file or directory
# RESOURCE = 'pkg/resources/file.csv' # No such file or directory
# import importlib.resources
# RESOURCE = str(importlib.resources.path(__package__, 'file.csv')) # <contextlib._GeneratorContextManager object at 0x7fb33602cf10>
I have no idea what the drawbacks are for the solution that seems to work at the moment; at least the resource file is now used as intended.
Could you maybe add a preferred implementation of this scenario to your examples? (with such a resource a python-user like me - a scientist who wants to get his data easy accessible for other scientists via python code - can get on with their work much faster)
Thanks,
Rob
PS
The nearest to your suggestion to hyansuper seems stackoverflow.com/questions/51520/how-to-get-an-absolute-file-path-in-python but the obtained absolute path with pathlib.Path().resolve() relates to the script calling the function that imports the resource. Alternatively, this problem has been very well expressed on https://stackoverflow.com/questions/10174211/how-to-make-an-always-relative-to-current-module-file-path. The discussion there gave the Pathlib version.
This is so horribly confusing