如何在python中使用包或模块中的数据

时间:2018-09-07 22:42:23

标签: python python-3.6 setup.py

我有文件,需要包装。所以我想把它放在包里。这样,当我调用使用该文件的函数时,便会知道在哪里可以找到它。我还需要放置文件,以便在测试阶段将其用作模块时,无需在python中构建包即可

你们知道我可以使用的任何工具吗?我们尝试了data_files和package_data。但是,我什么都做不了。

package_data={
        'my_package_name': ['templates/header.html', 'templates/footer.html', 'templates/Logo.png', 'templates/styles.css',
                                      'templates/custom_headers/header.html'],
    },

我还尝试了以下data_files

data_files=[ ('snap_report_lab', ['templates/*'])],

但是,它没有用。

def report_header(title, sub_title):
    """
        Create header for the report.

        :param str title: Title of the report.
        :param str sub_title: Subtitle of the report.

        :return: Message displaying Header created.
        :rtype: str

        Example:

        >>> report_header(title='Annual Report', sub_title='Sam Levitz - Prince')

    """

    THIS_DIR = '../snap_report_lab/templates/'

    j2_env = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)

    header_file = j2_env.get_template('header.html').render(title=title, sub_title=sub_title)

    with open('../my_package_name/templates/custom_headers/header.html', 'w') as file:
        file.write(header_file)
    file.close()

    return 'Header created'

这是我们在模块中使用的方式,以便如果我在项目目录中工作。

0 个答案:

没有答案