[Python] PyInstaller Imports Outside of Runner Script not Found

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to package a Flask app for the first time using PyInstaller. The command I'm using to package it looks like: pyinstaller --onefile --paths .venv/Lib/site-packages --add-data "apps;apps" apps/backend/routes/run.py This should allow for imports from packages that are in my virtual environment's site packages or are children of the apps/ folder.

    To note, this command is called from the root directory of the project. The folder structure looks like the following:

    root/
    |-- apps/
    |-- backend/
    |-- routes/
    |-- app.py
    |-- run.py
    |-- utils/
    |-- frontend/
    |-- build/
    |-- dist/
    |-- run.exe
    |-- run.spec


    The run.py file looks like this:

    """Entry-point file. Runs the Flask app"""

    import os
    import sys
    import logging
    import multiprocessing
    from waitress import serve
    def resource_path(relative_path:str):
    """
    Translate paths relative to the root directory to paths relative
    to either the root or PyInstaller's temporary folder

    Parameters
    ----------
    relative_path (str): The path relative to the project's root directory

    Returns
    -------
    out (str): A path relative to either the project's executable or root
    """
    try: # prod mode, base is the path of the executable
    base_path = sys._MEIPASS
    except AttributeError: # dev mode, not using executable, use project root instead
    base_path = os.path.abspath(
    os.path.join(os.path.dirname(__file__), "../../../")
    )
    return os.path.join(base_path, relative_path)
    # Add apps/ to the sys.path to allow for below imports
    sys.path.insert(0, resource_path("apps"))
    from apps.backend.routes.app import create_app
    from apps.backend.utils.shutdown_manager import ShutdownManager
    # ...


    While app.py, which run.py calls to in the second to last line looks like:

    """App Factory. Creates instances of a configured Flask app."""

    from flask import Flask
    from flask_cors import CORS
    import logging

    def create_app(config_obj:str, shutdown_manager = None)->Flask:
    app = Flask(__name__)
    return app


    I'm running the executable directly through the terminal with ./dist/run.exe. This gives me an error message like:

    Traceback (most recent call last):
    File "backend\routes\run.py", line 33, in <module>
    File "C:\Users\<user>\AppData\Local\Temp\_MEI274562\apps\backend\routes\app.py", line 3, in <module>
    from flask import Flask
    ModuleNotFoundError: No module named 'flask'
    [PYI-18800:ERROR] Failed to execute script 'run' due to unhandled exception!


    This confused me, not only because Flask is in my virtual environment, as pip show can confirm, but that this error wasn't triggered by the Waitress import in run.py. Thinking that this was related to the app.py file, I added from flask import Flask into run.py and, indeed, the error message changed to:

    Traceback (most recent call last):
    File "backend\routes\run.py", line 34, in <module>
    File "C:\Users\<user>\AppData\Local\Temp\_MEI113522\apps\backend\routes\app.py", line 4, in <module>
    from flask_cors import CORS
    ModuleNotFoundError: No module named 'flask_cors'
    [PYI-10220:ERROR] Failed to execute script 'run' due to unhandled exception!


    This means that the problem is just in importing any package outside of the run.py file. I tried calling other files with imports from run.py and this remained consistently true. The packages themselves seem to have been added and are recognized, but only if they're in run.py.

    Why is this happening and how do I fix this?

    Thank you in advance

    Continue reading...

Compartilhe esta Página