Starting from Anaconda, step by step guide to package your script 🚀
In daily development, we often write efficient scripts in Python, such as data processing tools and automation programs. However, if we want to share them with non-programmer friends, they may not have a Python environment installed. In this case, packaging the Python program into an EXE executable file is super convenient! EXE files can be run directly with a double-click, without relying on the Python interpreter, making cross-platform distribution easier.
Today, I will share a detailed packaging process based on a practical case. This case uses Anaconda to manage the environment, combined with the PyInstaller tool to package a Python script that processes Excel data (the script is named <span>test.py</span>, which you can replace with your own script). The whole process is simple and easy to understand, even beginners can get started. Let’s go!
Prerequisites
- Install Anaconda: If you haven’t installed it yet, hurry to download Anaconda from the official website (which includes Python and the conda package manager). It helps you easily create virtual environments and avoid environment conflicts.
- Target Directory: Assume your Python script is located in the
<span>E:\ProgramPackaging</span>folder. If yours is in a different path, remember to replace it. - Notes: The entire operation needs to be performed under the Windows system (because Anaconda Prompt is a command line tool for Windows). Run Anaconda Prompt as an administrator to avoid permission issues.
Step 1: Open Anaconda Prompt (Administrator Mode)
First, search for and open Anaconda Prompt as Administrator. This is because some operations (like installing packages) may require administrator privileges.
- Type “Anaconda Prompt” in the Windows search bar, right-click and select “Run as administrator”.
This will take you to the Anaconda command line interface, ready to start our packaging journey!
Step 2: Switch to the Target Directory
In Anaconda Prompt, enter the following command to switch to the folder where your script is located:
cd /d E:\ProgramPackaging
<span>/d</span>parameter is used to switch drives (from C drive to E drive).- Why do this step? Because subsequent operations (like running the script and packaging) need to be performed in the script directory for easier file path management.
After confirming the directory is correct, press Enter to execute.
Step 3: Create and Activate Virtual Environment, Install Dependencies
The virtual environment is key to packaging! It isolates your project dependencies, ensuring that the EXE file includes all necessary libraries and does not conflict with the system environment.
-
Create Virtual Environment (only execute this the first time):
conda create -n py36_env python=3.6The system will prompt you to confirm, press
<span>y</span>to continue. After successful creation, the environment path will be displayed. <span>-n py36_env</span>: Specifies the environment name (you can change it to your own).<span>python=3.6</span>: Specifies the Python version (using 3.6 here; if you need other versions like 3.8 or 3.9, you can replace it).- After creation, you don’t need to create it again for future program modifications, just activate it directly.
-
Activate Virtual Environment:
conda activate py36_env - After activation, the command line prompt will change to
<span>(py36_env)</span>at the beginning, indicating that you have entered the virtual environment. -
Install Dependencies: In the activated environment, install the packages required by the script. Here, we use Tsinghua mirror source to speed up the download (
<span>-i https://pypi.tuna.tsinghua.edu.cn/simple</span>).pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlsxwriter pandas numpy xlrd==1.2.0 pyinstaller openpyxl - These libraries are for Excel data processing (e.g., pandas for data processing, xlsxwriter for writing Excel, etc.).
<span>xlrd==1.2.0</span>specifies the version because the new version may not support some old formats. <span>pyinstaller</span>is the packaging tool and must be installed.- If your script requires other libraries, remember to add them here.
After installation, check: use <span>pip list</span> to view installed packages and ensure everything is ready.
Step 4: Test Run Your Python Script
Before packaging, first confirm that the script runs normally! In the virtual environment, enter:
python E:\ProgramPackaging\test.py
- If the script has parameters or inputs, follow the prompts.
- Run successfully? This indicates that the environment configuration is OK. If there are errors, check the library versions or script code.
This step is super important to avoid discovering issues after packaging and having to start over.
Step 5: Use PyInstaller to Package into EXE
Finally, we reach the packaging stage! Enter the following command:
pyinstaller --distpath E:\ProgramPackaging\output --onefile E:\ProgramPackaging\test.py
<span>--distpath E:\ProgramPackaging\output</span>: Specifies the output directory (here it is the<span>output</span>folder, which you can customize).<span>--onefile</span>: Packages into a single EXE file (for easy distribution). If omitted, a folder will be generated containing the EXE and dependency files.- The packaging process will generate a
<span>.spec</span>file and build log. After completion, find your EXE file in the<span>output</span>folder (the file name is usually the script name without the .py extension).
Double-click the EXE to test run! If everything is normal, it should work like an independent program.
Troubleshooting Common Issues
- Packaging Failed: Check if you are running the command in the virtual environment. Or there may be a PyInstaller version issue; try upgrading
<span>pip install --upgrade pyinstaller</span>. - EXE Run Error: This may be due to missing DLL files or environment variables. Add the
<span>--add-data</span>parameter to package additional files (see PyInstaller documentation for details). - Mirror Source Invalid: If the Tsinghua source is slow, switch to Alibaba Cloud:
<span>-i https://mirrors.aliyun.com/pypi/simple/</span>. - Switching Virtual Environment: If you want to exit the environment, use
<span>conda deactivate</span>.
Summary and Tips
Through this process, you can easily package your Python script into an EXE and distribute it to friends or colleagues! The combination of Anaconda + PyInstaller is particularly suitable for data science and automation projects. Remember to test the package on machines without Python to ensure compatibility.
If your script involves GUI (like Tkinter), PyInstaller also supports it; for complex projects, you can explore the <span>--hidden-import</span> option to import implicit modules.
Like this tutorial? Feel free to like, bookmark, and share! If you have questions, leave a message in the comments, and I will do my best to answer. See you next time, continuing to share more Python tips~ 🚀