Search code examples
pythonpyinstallermlflow

How to handle mlflow yml package file using pyinstaller?


I am building an executable with pyinstaller in a complex project based on Typer with mlflow for experiment tracking.

I am able to build an exe file, unfortunately no matter what I try, running the exe, I get the error that ml-package-versions.yml is missing (error points toward a temp dir).

If I understand it well, during installation of mlflow, this file is copied somewhere in my local tree in a temp dir. Running the cli, as long as I don't transform it to exe runs fine. But, when I build the exe, I guess the yml file is not copied together.

The code section in question in mlflow is here : https://github.com/mlflow/mlflow/blob/934f5ddb05ba621cab40a19172f5e453b11ac1f7/mlflow/utils/autologging_utils/versioning.py#L62

How do I force the copy of the file in a manner that mlflow would find it ? Or any other strategy..

I tried to run the pyinstaller exe generation in a clean venv in either conda or "official" python installs. I extracted the requirements of the project, installed only those requirements in the venv.

I am at the limit of my knowledge here, no clue what I could do next,

Any help appreciated

This is the traceback I get:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "app.py", line 14, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "database_manager.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "dni\modeling\active_learning\predict_single.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "mlflow\__init__.py", line 32, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "mlflow\tracking\__init__.py", line 20, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "mlflow\tracking\fluent.py", line 27, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "mlflow\utils\autologging_utils\__init__.py", line 27, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "mlflow\utils\autologging_utils\versioning.py", line 67, in <module>
  File "mlflow\utils\autologging_utils\versioning.py", line 63, in _load_version_file_as_dict
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\[USERNAME]\\AppData\\Local\\Temp\\_MEI92042\\mlflow\\ml-package-versions.yml'
[24704] Failed to execute script 'main' due to unhandled exception!

Solution

  • Have you tried doing something like this ?

    C:\Python311\python.exe -m PyInstaller --onefile --name="AppName" --icon=logo.png --collect-data  ml-package-versions.yml app.py
    

    I have used it in the past to force PyInstaller to bundle some tkinter styling metadata, the sun valley ttk theme always needs to be added with the --collect-data argument when compiling, in my experience.