Install static files for Python package
(forgive my poor English)
I'm writing setup.py for my Python app.
My app needs some static files (pictures). I should add these files to data_files array in setup.py, and files will be installed in "/usr/xxx" path on Linux.
Then how can my app access these files after installation? I don't think it's good idea to use abslote path (like /us开发者_如何学Pythonr/share/xxx/xxx.png) in my app (it won't work on windows).
You're supposed to use sys.prefix instead of an absolute path:
filename = os.path.join(sys.prefix, "share", "xxx", "xxx.png")
distribute/setuptools provides pkg_resources for this purpose (and will work with package installed by python setup.py develop
or pip install -e
).
api example::
import pkg_resources
assert pkg_resources.resource_exists('fabric', 'README.txt')
assert not pkg_resources.resource_isdir('fabric', 'README.txt')
myfile_path = pkg_resources.resource_filename('fabric', 'README.txt')
myopen_file = pkg_resources.resource_stream('fabric', 'README.txt')
myfile_str = pkg_resources.resource_string('fabric', 'README.txt')
contents_of_package = pkg_resources.resource_listdir('fabric', '')
精彩评论