I'm building an API using PyInstaller 6.4.0, but it's not working.
For some reason, the built file is not recognizing my routes.
The project folder structure is:
markdown Copiar código
API
├── logs
└── v1
└── routes
├── router1.py
└── router2.py
The code is:
[imports]
rprefix = f"/api/v1"
app = FastAPI()
[...]
router_prices = importlib.import_module(f"v1.routes.checkprices_routes")
router_commons = importlib.import_module(f"v1.routes.commons_routes")
[...]
app.include_router(
router_prices.__getattribute__("check_prices_router"), prefix=rprefix
)
app.include_router(router_commons.__getattribute__("commons_router"), prefix=rprefix)
if __name__ == "__main__":
if len(SSL_KEY) > 0 and len(SSL_CERT) > 0:
uvicorn.run(
"main:app",
host=API_HOST,
port=API_PORT,
reload=False,
ssl_keyfile=SSL_KEY,
ssl_certfile=SSL_CERT,
)
else:
uvicorn.run("main:app", host=API_HOST, port=API_PORT, reload=False)
traceback:
<module 'v1.routes.checkprices_routes' from 'C:\\Users\\WA\\Área de Trabalho\\\main\\_internal\\v1\\routes\\checkprices_routes.py'>
<module 'v1.routes.commons_routes' from 'C:\\Users\\WA\\Área de Trabalho\\main\\_internal\\v1\\routes\\commons_routes.py'>
Traceback (most recent call last):
File "main.py", line 36, in <module>
router_prices = importlib.import_module(f"v1.routes.checkprices_routes")
File "importlib\__init__.py", line 127, in import_module
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\WA\Área de Trabalho\main\_internal\v1\routes\checkprices_routes.py", line 10, in <module>
from v1.repositories.checkprices_search.search_repository import \
File "C:\Users\WA\Área de Trabalho\main\_internal\v1\repositories\checkprices_search\search_repository.py", line 13, in <module>
from v1.models.dao.product_classes_DAO import ProductClassesDAO
File "C:\Users\WA\Área de Trabalho\main\_internal\v1\models\dao\product_classes_DAO.py", line 2, in <module>
from main import LOGGER
File "C:\Users\WA\Área de Trabalho\main\_internal\main.py", line 61, in <module>
router_prices.__getattribute__("check_prices_router"), prefix=rprefix
AttributeError: partially initialized module 'v1.routes.checkprices_routes' has no attribute 'check_prices_router' (most likely due to a circular import)
[16600] Failed to execute script 'main' due to unhandled exception!
P.S: The project is currently running on VSCode; it just doesn't work after building.
Pyinstaller can not automatically detect dynamic import (importlib.import_module
in your case)
You could use --hidden-import MODULENAME
to include those modules you imported dynamically.
See more at https://pyinstaller.org/en/stable/when-things-go-wrong.html?highlight=hidden#listing-hidden-imports