如何将基于spaCy的Python程序及命名实体识别脚本转为可执行文件
Hey there! Let's break down how to convert your spaCy-based Python programs (including those with Named Entity Recognition) into standalone executables. I’ve worked through this process multiple times, so here’s a practical, step-by-step guide that covers the common pitfalls:
The go-to tool for this job is PyInstaller—it wraps your Python script and all dependencies into a single executable file. It works for most spaCy use cases, including NER scripts that rely on pre-trained models.
Before you start packaging, make sure your script runs perfectly on its own. For NER scripts, double-check that you can load your spaCy model (like en_core_web_sm) and extract entities without errors.
Here’s a quick example of a working NER script to reference:
import sys import os import spacy def get_model_path(model_name): # Handle both regular and frozen (packaged) environments if getattr(sys, 'frozen', False): # Running as executable: use the temporary unpack path base_path = sys._MEIPASS else: # Running as script: use the current file's directory base_path = os.path.dirname(os.path.abspath(__file__)) return os.path.join(base_path, model_name) try: # Load the model from the correct path nlp = spacy.load(get_model_path("en_core_web_sm")) except OSError: # Fallback: download the model if it's missing from spacy.cli import download download("en_core_web_sm") nlp = spacy.load("en_core_web_sm") def extract_entities(text): doc = nlp(text) return [(ent.text, ent.label_) for ent in doc.ents] if __name__ == "__main__": sample_text = "Apple is looking to buy a startup in London for $1 billion." entities = extract_entities(sample_text) print("Extracted Entities:") for ent, label in entities: print(f"{ent} → {label}")
Open your terminal/command prompt and run:
pip install pyinstaller
Pro tip: Use a clean virtual environment for this—this avoids packaging unnecessary dependencies and keeps your executable size smaller.
This is the most common pain point. SpaCy’s pre-trained models aren’t just Python code—they’re directories with data files. You have two options:
Option 1: Package the Model With the Executable
This makes your app self-contained (no internet needed to run), but increases the executable size (models are ~100MB+).
First, find where your spaCy model is installed. Run this in Python to get the path:
import spacy print(spacy.util.get_package_path("en_core_web_sm"))
Then, use PyInstaller’s --add-data flag to include the model directory. The syntax varies by OS:
Windows
pyinstaller --onefile --add-data "C:\Path\To\en_core_web_sm;en_core_web_sm" your_script.py
(Note the semicolon separating the source and destination paths)
Mac/Linux
pyinstaller --onefile --add-data "/path/to/en_core_web_sm:en_core_web_sm" your_script.py
(Use a colon here instead of a semicolon)
Option 2: Let the Executable Download the Model On First Run
If you want a smaller executable, add logic to your script to download the model automatically if it’s missing (like in the example script above). This requires an internet connection the first time someone runs the exe, but keeps the file size tiny.
Run the PyInstaller command from Step 3. When it finishes, you’ll find your executable in the dist folder created in your project directory.
Navigate to the dist folder and run the exe. For the NER example, it should print the extracted entities without errors. If it fails, check the troubleshooting tips below.
- "Model not found" error: Double-check your
--add-datapath matches the actual model directory. Also, make sure your script uses theget_model_pathfunction (from the example) to load the model correctly in the frozen environment. - Missing dependencies: If you use other third-party libraries besides spaCy, PyInstaller might miss them. Add them with the
--hidden-importflag, e.g.,--hidden-import "some_library". - Large executable size: Use a virtual environment with only the necessary packages, or use Option 2 to exclude the model from the package.
内容的提问来源于stack exchange,提问作者vishalk

