如何从包中的脚本访问包的模板部分

问题描述

我无法使用setuptools创建软件包。我有一个存储库,正在清理它以打包。目录结构看起来像这样

my-proj
├── setup.py
├── MANIFEST.in
├── MakeFile
├── README.rst
├── setup.py
└── myproj
    ├── __init__.py
    ├── my_code.py
    ├── templates
    │   ├── template1.yaml
    │   ├── template2.yaml

“ my_code.py”的初始版本具有代码段,该代码段将直接引用带有templates文件夹的文件进行一些处理。如果使用安装工具将其打包,则会在这文件中提供以下信息: MANIFEST.in:

include README.rst
include requirements.txt
include LICENSE.txt    
recursive-include myproj/templates *

setup.py:

 setup(
    name='myproj',package_dir={'testbed_init': 'testbed_init'},package_data={'templates': ['templates/*'],'configs': ['configs/*']},include_package_data=True,)

我的问题如下。在“ my_Code.py”中,我曾经直接引用模板而没有任何问题,因为我将从myproj文件夹运行脚本。如果将其打包,则如何确保将模板作为打包的一部分包含在内,并且在脚本运行时,需要打开相对于软件包安装位置的模板。

my_code.py中的代码段:

        if _type == "a":
            temp_file = f"templates/template1.yaml"
        else:
            temp_file = f"templates/template2.yaml"
        build_config(deploy_esx_file,output_file,data)

build_config中发生的情况的代码段:

def build_config(template_file,inputs):
        templateLoader = jinja2.FileSystemloader(searchpath="./")
        templateEnv = jinja2.Environment(loader=templateLoader)
        template = templateEnv.get_template(template_file)
        outputText = template.render(inputs)
        with open(output_file,'w') as h:
            h.write(outputText)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...