0

I have trouble creating a package with setuptools. I have a repository which I'm cleaning up to make it a package. The directory structure looks something like this

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

Initial version of "my_code.py" had code snippet which would directly reference the files withing templates folder to do some processing. If I package this using setup tools, I provide the following information in these files: 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 question is as follows. In "my_Code.py" I used to reference templates directly without any problem as I would run script from the myproj folder. If I package this, how can I make sure, I include the templates as part of package and when script runs, I need to open the templates relative to where the package is installed.

Code snippet from 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)

Code snippet of what happens in build_config:

def build_config(template_file, output_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)
sinoroc
  • 18,409
  • 2
  • 39
  • 70
Ani
  • 149
  • 1
  • 1
  • 8
  • 1
    https://docs.python.org/3/library/importlib.html#module-importlib.resources – sinoroc Oct 29 '20 at 08:48
  • Does this answer your question? [How to read a (static) file from inside a Python package?](https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package) – sinoroc Dec 30 '20 at 09:29

0 Answers0