[Python] Pyinstaller with FileNotFoundError: [Errno 2] No such file or directory after added...

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use pyinstaller to pack my application, but I met some problems. I developed software in venv environment, and my structure is like this.

    enter image description here

    func_a.py has read json file from resources like

    with open('resources/requirement.json') as jsonfile:
    request_Doc = json.load(jsonfile)


    But I still got the error from pyinstaller

    File "functions/func_a.py", line 15, in <module>
    FileNotFoundError: [Errno 2] No such file or directory: 'resources/request_Doc.json'


    However, I do add the json file in the spec. I am wondering why func_a.py couldn't find the path of json file?

    I try to put functions/*py, resources/*json and main.py in bin to pack but the problems still exists. What should I design my structure when developing the application like this?

    ==========================================> Edited on 2023.11.04

    This is my project structure. enter image description here

    This is how I write in py files func_a.py

    import json
    import os
    import func_b

    with open('resources/requirement.json') as jsonfile:
    requirement = json.load(jsonfile)
    jsonfile.close()
    jsonfile.close()


    def func():
    if os.path.exists(requirement['token_path']):
    print('Get Token')
    func_b.go()


    requirement.json

    {
    "token_path":"resources/token.json"
    }


    main.py

    import functions.func_a as func_a

    f = func_a.func()


    AND I try to use command pyinstaller main.py --add-data (resources/*.json, resources)

    Continue reading...

Compartilhe esta Página