Search code examples
pythonpyinstaller

How to pass a parameter to pyinstaller?


Is it possible to give Pyinstaller a parameter for a script? I have written a build.py and a main.py script. The build.py script should create an exe from the main.py script using Pysintaller. Since I want to pass the main.pyscript something like a password or similar, I would like to do this directly during the build to get different "versions". With sys.argv or os.environ I could not solve my problem, because I would like to set a parameter as default. To publish some code below is my build.py script.

import PyInstaller.__main__
import sys
from sys import platform

def build_func():

    if platform == "linux" or platform == "linux2":
        PyInstaller.__main__.run([
            'main.py',
            '--onefile',
            '--windowed',
            '--add-data=picture.png:img'
        ])
    elif platform == "win32":
        PyInstaller.__main__.run([
            'main.py',
            '--onefile',
            '--windowed',
            '--add-data=picture.png;img'
        ])


if __name__ == "__main__":
     build_func()

Solution

  • To put my question in some context, I will answer it. Imagine you want to give your program a version number, so you need to bind such a number to the main.py. In my case, I call build.py from the command line with python3 build.py "1.0.0", where "1.0.0" is the version number. With sys.argv I catch the argument and write it to a file. With Pyinstaller I can include the file in the exe and take the version number from it as a fixed parameter in my program.

    build.py

    import PyInstaller.__main__
    import sys
    from sys import platform
    import os 
    
    def build_func(version=None):
    
        with open("temp_file", "w") as file:
            file.write(str(version))
        file.close()
    
        if platform == "linux" or platform == "linux2":
            PyInstaller.__main__.run([
                'main.py',
                '--onefile',
                '--windowed',
                '--add-data=picture.png:img',
                '--add-data=temp_file:file'
            ])
        elif platform == "win32":
            PyInstaller.__main__.run([
                'main.py',
                '--onefile',
                '--windowed',
                '--add-data=picture.png;img',
                '--add-data=temp_file;file'
            ])
       os.remove("temp_file")
    if __name__ == "__main__":
        try:
            Version = sys.argv[1]
            build_func(Version)
    
        except IndexError:
            build_func()
    
    

    In the main.py I get the file with:

    path = resource_path("temp_file","file")
    with open(path, "r") as file:
         data = file.read()
    file.close()
    

    and the resource_path function

    def resource_path(relative_path,dist):
        try:
           base_path = os.path.join(sys._MEIPASS, dist)
        except AttributeError:
           base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)