EXE Web API by Python

Introduction

From time to time, we may be asked to meet all of the following:

I think there are various ways, but

  1. Allow AI functionality to be exposed as a Web API in Python's web framework
  2. Convert the Python script to an EXE file and bundle it with the application itself,
  3. You can achieve this by running the EXE from the app itself.

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)))

procedure

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. browser.png

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

EXE Web API by Python
Web API with Python + Falcon
Hit the web API in Python
[Python] Web application from 0! Hands-on (3) -API implementation-
Python: Reading JSON data from web API
Procedure to use TeamGant's WEB API (using python)
The first web app created by Python beginners
Explosive speed with Python (Bottle)! Web API development
Primality test by Python
Django python web framework
Visualization memo by Python
Communication processing by Python
Evernote API in Python
OpenCV3 Python API list
Beamformer response by python
C API in Python 3
Python web scraping selenium
TensorFlow API memo (Python)
Knowledge base + Web API operated by NoSQL x PaaS
Speech file recognition by Google Speech API v2 using Python
[python] [Gracenote Web API] A little customization of pygn
Hit Mastodon's API in Python
Use Trello API with python
Speech recognition by Python MFCC
Web scraping with python + JupyterLab
Web scraping notes in python3
AWS CDK-Lambda + API Gateway (Python)
Use Twitter API with Python
Newcomer training program by Python
Parameter setting by python configparser
Pin python managed by conda
Image collection by calling Bing Image Search API v5 from Python
Python web programming article summary
Keyword extraction by MeCab (python)
[SAP CP] Web API created with python in CF environment
Separate numbers by 3 digits (python)
Comparison of 4 Python web frameworks
Image collection by web scraping
Markov switching model by Python
Web scraping using Selenium (Python)
Play RocketChat with API / Python
Image processing by python (Pillow)
Python started by C programmers
Blender Python API in Houdini (Python 3)
Call the API with python3.
Web application with Python + Flask ② ③
Use subsonic API with python3
Google Drive Api Tips (Python)
Web scraping beginner with python
Streamline web search with python
One-liner web scraping by tse
Platform (OS) determination by Python
Use e-Stat API from Python
Web application with Python + Flask ④
Sort by date in python
Obtaining Azure Access Token by Python for using Microsoft Graph API
[Python / Django] Create a web API that responds in JSON format
[FastAPI] Getting started with FastAPI, an ASGI web framework made by Python
Get US stock price from Python with Web API with Raspberry Pi
Play by hitting the Riot Games API in Python First half
python web scraping-get elements in bulk