Accessing static files included in a Python module
dir()
won't tell you anything about static files. The correct way (or one of them, at least) to get access to this data is with the resource_*
functions in pkg_resources
(part of setuptools), e.g.:
import pkg_resources
pkg_resource.resource_listdir('glm_plotter', 'templates')
# Returns a list of files in glm_plotter/templates
pkg_resource.resource_string('glm_plotter', 'templates/index.html')
# Returns the contents of glm_plotter/templates/index.html as a byte string
Python read static file from within a pip package that I've deployed
The solution was
Import the library ....
import mylib
with pkg_resources.open_text(mylib, 'to_read.json') as file:
return json.load(file)
So basically everything that I thought shouldn't have applied did apply. RIP
How include static files to setuptools - python package
As pointed out in the comments, there are 2 ways to add the static files:
1 - include_package_data=True + MANIFEST.in
A MANIFEST.in
file in the same directory of setup.py
that looks like this:
include src/static/*
include src/Potato/*.txt
With include_package_data = True
in setup.py
.
2 - package_data in setup.py
package_data = {
'static': ['*'],
'Potato': ['*.txt']
}
Specify the files inside the setup.py
.
Do not use both include_package_data
and package_data
in setup.py
.
include_package_data
will nullify the package_data
information.
Official docs:
https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html
loading data from folder inside package
.pkl
data are probably serialized data using pickle
python module. It can't be imported. You have to deserialize data.
import pickle
data = pickle.load(open("data.pkl", "rb"))
As say in other answer, you can wraps this in a python module.
# filename: data.py
import pickle
def load_data(filename):
return pickle.load(open(filename, "rb"))
If your .pkl
files are in a python package, you can retreive its using pkg_resources
.
import pickle
import pkg_resources
def load_data(resource_name):
return pickle.load(
pkg_resources.resource_stream("my_package", resource_name))
In python >= 3.7, data can be retreived using importlib.resources
to prevent use of thrird-party package.
data = pickle.load(
importlib.resources.open_binary("my_package.data_folder", "data.pkl"))
Access data in package subdirectory
You can use __file__
to get the path to the package, like this:
import os
this_dir, this_filename = os.path.split(__file__)
DATA_PATH = os.path.join(this_dir, "data", "data.txt")
print open(DATA_PATH).read()
Related Topics
How to Import the Class Within the Same Directory or Sub Directory
Difference Between Pip and Conda
Run a Python Script in Terminal Without the Python Command
Linux:Python:Clear Input Buffer Before Raw_Input()
How to Handle Os.System Sigkill Signal Inside Python
Increasing a File's Size Using Mmap
How to Kill Zombie Processes Created by Multiprocessing Module
How to Get the Process Name by Pid in Linux Using Python
Socket Shutdown and Rebind - How to Avoid Long Wait
Modulenotfounderror: No Module Named 'Pydip', Although It's Installed
Fitting Empirical Distribution to Theoretical Ones With Scipy (Python)
What Is the Naming Convention in Python For Variable and Function
On Linux Suse or Redhat, How to Load Python 2.7
Groupby Weighted Average and Sum in Pandas Dataframe
Cannot Get Environment Variables in Django Settings File
How to Cleanly Kill Subprocesses in Python