Доступ к статическим файлам, включенным в модуль Python - PullRequest
0 голосов
/ 10 ноября 2018

Я устанавливаю свой модуль с помощью pip. Ниже приведен файл setup.py:

from setuptools import setup, find_packages

with open('requirements.txt') as f:
    required = f.read().splitlines()

print(required)
setup(
    name='glm_plotter',
    packages=find_packages(),
    include_package_data=True,
    install_requires=required
)

и MANIFEST.in:

recursive-include glm_plotter/templates *
recursive-include glm_plotter/static *

При установке каталоги и файлы, кажется, устанавливаются:

  ...
  creating build/lib/glm_plotter/templates
  copying glm_plotter/templates/index.html -> build/lib/glm_plotter/templates
...
  creating build/bdist.linux-x86_64/wheel/glm_plotter/templates
  copying build/lib/glm_plotter/templates/index.html -> build/bdist.linux-x86_64/wheel/glm_plotter/templates
 ...

Когда я иду, чтобы импортировать этот модуль:

>>> import g_plotter
>>> dir(g_plotter)
['CORS', 'Flask', 'GLMparser', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'app', 'controllers', 'views']

Я не вижу статических файлов. Я не уверен, что это правильный способ получения доступа к статическим файлам.

1 Ответ

0 голосов
/ 10 ноября 2018

dir() не расскажет вам ничего о статических файлах. Правильный (или хотя бы один из них) способ получить доступ к этим данным - использовать функции resource_* в pkg_resources (часть setuptools), например ::

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
...