marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.32k stars 216 forks source link

Cx_freeze packaging tortoise-orm application error 'PackageNotFoundError: No package metadata was found for tortoise-orm' #2557

Closed opsboy closed 5 days ago

opsboy commented 2 weeks ago

I wrote a web service using fastapi+tortoiser orm, and then packaged it using cx_freeze. After successful packaging, I ran the exe file and received the following error message: image

setup.py

import logging

import re
import sys
from pathlib import Path
from typing import List, Iterator

from cx_Freeze import setup, Executable

PROJECT_DIR: Path = Path(__file__).parent

def build():

    def get_python_packages(file_path: Path) -> Iterator[str]:
        if not file_path.is_dir():
            return
        for child in file_path.iterdir():
            if child.is_dir():
                is_python_package = set(child.glob("__init__.py"))
                if is_python_package:
                    # print(f"init-{is_python_package}")
                    yield child.name

    def get_all_packages() -> Iterator[str]:

        site_regex = re.compile("[\\\\|/]site-packages")
        site_path = None
        for path in sys.path:
            if site_regex.search(path):
                site_path = Path(path)
                break
        if site_path:
            yield from get_python_packages(site_path)

    def get_all_requirements() -> Iterator[str]:
        package_regex = re.compile("[a-zA-Z\-_]+")

        for line_content in open(PROJECT_DIR / "requirements.txt", "r"):
            pack_name = package_regex.match(line_content)
            if pack_name:
                yield pack_name.group().replace("-", "_")

    packages: List = []

    requirements_str = "-".join(get_all_requirements())
    for pack in get_all_packages():
        if pack in requirements_str:
            packages.append(pack)

    build_exe_options = {
        "packages": packages,
        "include_files" : ["project_env", "config.toml"],
        "includes": ["src.apps"],
    }
    logging.info(" ---- cx_Freeze build --------")
    setup(
        name=PROJECT_DIR.name,
        version="1.0.1",
        description=PROJECT_DIR.name,
        options={"build_exe": build_exe_options},
        executables=[Executable("main.py")],
    )

if __name__ == '__main__':
    build()
marcelotduarte commented 2 weeks ago

I'm a little confused by your setup, but first, I need a sample application to test. I found this example, which can be a test base. I want to run it using Python, and then build it with cx_Freeze to reproduce the error. I used these requirements: pip install "fastapi[standard]" tortoise-orm uvicorn But I got this error: ModuleNotFoundError: No module named 'routers' Can you suggest the correct requirements?

opsboy commented 1 week ago

@marcelotduarte You can use this template to test it.

marcelotduarte commented 5 days ago

Release 7.2.1 is out! Documentation