简体   繁体   中英

How to convert flask project into executable .exe

I need to convert my flask project into.exe

It's small project intended to run on local browser (offline).

When I run the exe script it tells me that flask has not been imported. It's not true as it works when fired directly from main.py. Seems like pyinstaller skips some libs. What can I do?

main.py

from app import create_app

if __name__ == "__main__":
    app = create_app()
    app.run()

app/ init .py

from flask import  Flask
SECRET_KEY = ""
UPLOAD_FOLDER = ""
MAX_SIZE = 500000

def create_app():
    app = Flask(__name__)
    app.config["SECRET_KEY"] = SECRET_KEY
    app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
    app.config['MAX_CONTENT_PATH'] = MAX_SIZE

    from .views import views

    app.register_blueprint(views, url_prefix="/")

    return app

Command to pack pyinstaller -F main.py

main.exe error

Traceback (most recent call last):
  File "main.py", line 1, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "app\__init__.py", line 1, in <module>
ModuleNotFoundError: No module named 'flask'
[32112] Failed to execute script 'main' due to unhandled exception!

run the pip show pip you will need the -p to point to the pip libs example

PS D:\GitHub\ShittyChatApp> pip show pip
Name: pip
Version: 22.1.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: distutils-sig@python.org
License: MIT
Location: c:\users\jack\appdata\local\programs\python\python310\lib\site-packages
Requires:
Required-by: pipenv

or

    pyinstaller -p c:\users\jack\appdata\local\programs\python\python310\lib\site-packages \
    -p myapp \
    --add-data="myapp/templates;templates" \
    --add-data="myapp/static;static" \
    --hidden-import=dns.versioned \
    --hidden-import=dns.tsigkeyring \
    --hidden-import=dns.namedict \
    --hidden-import=dns.e164 \
    --hidden-import=dns \
    --hidden-import=dns.asyncresolver \
    --hidden-import=dns.asyncquery \
    --hidden-import=dns.asyncbackend \
    --hidden-import=eventlet.hubs.epolls \
    --hidden-import=eventlet.hubs.kqueue \
    --hidden-import=eventlet.hubs.selects -F main.py --onefile

This is what my make file look like, I am using this for windows and linux

PIP_LIBS=c:\users\jack\appdata\local\programs\python\python310\lib\site-packages

all: install_deps clean build

install_deps:
    pip install -r requirements.txt
build:
    pyinstaller -p c:\users\jack\appdata\local\programs\python\python310\lib\site-packages \
    -p myapp \
    --add-data="myapp/templates;templates" \
    --add-data="myapp/static;static" \
    --hidden-import=dns.versioned \
    --hidden-import=dns.tsigkeyring \
    --hidden-import=dns.namedict \
    --hidden-import=dns.e164 \
    --hidden-import=dns \
    --hidden-import=dns.asyncresolver \
    --hidden-import=dns.asyncquery \
    --hidden-import=dns.asyncbackend \
    --hidden-import=eventlet.hubs.epolls \
    --hidden-import=eventlet.hubs.kqueue \
    --hidden-import=eventlet.hubs.selects -F main.py --onefile
    rm -rf build
run_server:
    ./dist/main
clean:
    rm -rf build
    rm -rf dist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM