File access failure in GitHub repo when deploying an app


I’m developing an app that generates a report from user inputs and a .docx template using python-docxtpl. The idea is to keep both the code and the template in a repository on GitHub, so that the program can access the template.

My first version of the program works on localhost, however when I deploy it I get the following error:

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/", line 565, in _run_script
    exec(code, module.__dict__)
File "/app/autoeya_v3/4_Python_source_code/", line 120, in <module>
File "/app/autoeya_v3/4_Python_source_code/", line 100, in main
    document = fill_docx(st.session_state)
File "/app/autoeya_v3/4_Python_source_code/", line 57, in fill_docx
File "/home/appuser/venv/lib/python3.9/site-packages/docxtpl/", line 359, in render
File "/home/appuser/venv/lib/python3.9/site-packages/docxtpl/", line 52, in render_init
File "/home/appuser/venv/lib/python3.9/site-packages/docxtpl/", line 48, in init_docx
    self.docx = Document(self.template_file)
File "/home/appuser/venv/lib/python3.9/site-packages/docx/", line 25, in Document
    document_part =
File "/home/appuser/venv/lib/python3.9/site-packages/docx/opc/", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
File "/home/appuser/venv/lib/python3.9/site-packages/docx/opc/", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
File "/home/appuser/venv/lib/python3.9/site-packages/docx/opc/", line 30, in __new__
    raise PackageNotFoundError(
        docx.opc.exceptions.PackageNotFoundError: Package not found at 'Report_template.docx'

According to my (small) experience, the problem lies in the fact that python-docxtpl is not able to find the document in the indicated filepath. However, this only happens in the deployed version of the application - and the position of the files is the same on my personal machine and in the GitHub repository.

I already tried passing the relative path, the absolute path and using file handling, but nothing worked so far. Here is the code snippet that gives me problems:

def fill_docx(session_state):
    # Get the absolute path of the template
    template_path = 'Report_template.docx'
    template_fullpath = os.path.abspath(template_path)
    template_file = open(template_fullpath, "rb")
    doc = DocxTemplate(template_file)
    data = {
        # data to be inserted in the template
    return doc

If anyone knows a way to fix this I would be very grateful.

Hi @GuilhermeFTVS

Could you try activating enableStaticServing in the .streamlit/config.toml as in:

enableStaticServing = true

Then in the app, you can use relative paths to refer to your files as in app/static/file.docx where file.docx is inside a folder ./static/ in the app repo.

This is from the Streamlit Docs (Static file serving - Streamlit Docs)

Hope this helps!

Best regards,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.