From time to time, we may be asked to meet all of the following:
I think there are various ways, but
This time we will use:
(By the way, with FastAPI x uvicorn, there was a problem as of March 30, 2020. [https://github.com/pyinstaller/pyinstaller/pull/4664](https://github.com/pyinstaller/pyinstaller/ pull / 4664)))
Install Flask and PyInstaller.
At the command prompt etc.
pip install flask pyinstaller
Create a Python file that exposes the Web API.
main.py
from flask import Flask
app = Flask(__name__)
@app.route('/predict')
def predict():
"""
Intention of API to return prediction by AI
"""
return {'result': 'Prediction by AI.'}
if __name__ == '__main__':
app.run()
Move to the directory containing the above files and
pyinstaller main.py --onefile
There is main.exe
in the dist
directory, so let's try it.
dist\main.exe
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module>
File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pkg_resources\__init__.py", line 86, in <module>
ModuleNotFoundError: No module named 'pkg_resources.py2_warn'
[24508] Failed to execute script pyi_rth_pkgres
If you get ModuleNotFoundError
as above, edit the main.spec
file generated in the same hierarchy as main.py
with a text editor.
Specifically, include the module name that was not found in hiddenimports
.
main.spec
# --Abbreviation--
hiddenimports=[],
# --Abbreviation--
↓ Modify and save as follows.
main.spec
# --Abbreviation--
hiddenimports=['pkg_resources.py2_warn'],
# --Abbreviation--
Run pyinstaller with the modified spec file instead of the py file.
pyinstaller main.spec --onefile
Check the operation again.
dist\main.exe
When you access it, you can roughly confirm that the Web API is open.
Actually, this EXE is executed from an application such as C # (process management required), and it will be accessed by the Http client from the same application.
Recommended Posts