Search code examples
pythonpyinstaller

PyInstaller --add-data from script


I'm trying to create an executable file out of my python project. I'm using the function 'make_executable' to build an executable file.

Running the command to add data raises an error like: pyinstaller: error: unrecognized arguments: --add-data C:\Users<>... --add-data: C:\Users<>...

def make_executable(main_script: str, files_to_add: List[str], target_location: str = None,
name: str = 'app', single_file: bool = True) -> None:
    """
    Creating an executable file out of the provided parameters.
    :param main_script: Main script of the application.
    :param files_to_add: A list of files that are crucial to the project.
    :param target_location: Where to store the executable file.
    :param name: Name of the executable file.
    :param single_file: Determine whether a single file or a directory is to be created. 
    :return: None.
   """
   args = [main_script, f'--name {name}']
        if target_location is not None:
            args.append(f'--distpath {target_location}')
        if single_file:
            args.append('--onefile')
        for file in files_to_add:
            args.append(f'--add-data {file}')
        PyInstaller.__main__.run(args)

I've also tried using something like

  • --add-data C:\Users<>\xyz;. --add-data C:\Users<>\abc;.
  • --add-data "C:\Users<>\xyz;." --add-data "C:\Users<>\abc;."

Note: Each backslash is escaped.

What can I do to add the required data? Any help on this issue is appreciated!


Solution

  • Per the PyInstaller docs:

    --add-data <SRC;DEST or SRC:DEST>
    Additional non-binary files or folders to be added to the executable. The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can be used multiple times.

    Also you need to split up the flags and the arguments inside the list. like this:

    if target_location is not None:
        args.append("--dispath")
        args.append(target_location)
    if single_file:
        args.append('--onefile')
    for file in files_to_add:
        args.append("--add-data")
        args.append(f'{file};{some destination}')