4

I have a python program that takes in as input two text files. I have converted this python program(a .py file) to a .exe file using pyinstaller. The .exe file when run gives FileNotFoundError. But when the .txt file is copied to the path where .exe is present it works fine. My intention is to not copy the .txt file but bundle the .txt file along with .exe so that the .txt file is inaccessible. All .txt file dependencies i want to bundle it with .exe, ultimately there should be just one .exe file, when i run it, it should work the same way as when i run the python program. Please suggest me ways in achieving the same

I am new to pyinstaller. I had tried adding the .txt files to data parameter in the .spec file. But this fails to copy the text files into the dist folder where my .exe is present. But as i have mentioned i just need the .exe file alone. Even if the .exe file is shared to someone else who do not have access to any of the text files, the .exe must run sccessfully

a.datas+=[('D:/Trial/src/readme_text_files/readme1.txt','readme_text_files/readme1.txt','readme_text_files'), ('D:/Trial/src/readme_text_files/readme2.txt','readme_text_files/readme2.txt','readme_text_files'), ]

The above code has been added to .spec file, as a result i assume that the readme_text_files must be copied to the folder where .exe is present when you run: pyinstaller spec_filename.spec

Varsha
  • 55
  • 1
  • 9

1 Answers1

1

I wanted a .txt file to be bundled with .exe file, thus i did the following:

  1. I edited the .spec file in the following way:

    a = Analysis(['mainProgram_edited_for_datas.py'],
             pathex=['D:\\Trial\\src'],
             binaries=[],
             datas=[
             ('D:/Trial/src/readme_text_files/readme1.txt','readme_text_files'),
             ('D:/Trial/src/readme_text_files/readme2.txt','readme_text_files'),
             ], ...........(Rest of the .spec file contents as it is)
    

    Or simply you can directly include a directory itself like below:

    datas=[('C:/Users/njv5kor/eclipse-workspace/Trial/src/readme_text_files/','readme_text_files'),
                        ],
    
  2. In the python code i added the below code:

    def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try: 
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    
    return os.path.join(base_path, relative_path)
    
    file = resource_path("readme_text_files\\readme1.txt")
    

Basically the pyinstaller bundles the .txt files to the .py file and creates a single .exe For details regarding _MEIPASS plese refer link: https://pyinstaller.readthedocs.io/en/v3.3.1/operating-mode.html#how-the-one-file-program-works

Varsha
  • 55
  • 1
  • 9