You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用auto-py-to-exe打包Python脚本遇手动输入阻塞问题求助

打包Python脚本为可执行文件时卡在动态库搜索阶段并要求手动输入的解决办法

问题描述

尝试将包含requests、BeautifulSoup等库的Python脚本转为可执行文件,先后使用auto-py-to-exe和py2exe工具,均遇到同一问题:打包过程中需要手动输入。

已通过sys.frozen判断运行环境,在打包模式下设置默认参数避免运行时输入,但使用auto-py-to-exe选择「单文件」和「控制台模式」打包时,进程卡在145067 INFO: Looking for dynamic libraries步骤并提示手动输入。

脚本代码

import requests
from bs4 import BeautifulSoup
import os
import csv
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
import sys

# Check if the script is being executed as an executable or in the development environment
if getattr(sys, 'frozen', False):
    # In executable mode (assign default values to avoid input during conversion)
    start_date = "240901"
    end_date = "240915"
    year = "2024"
else:
    # In development mode (ask for input in the console)
    start_date = input("Enter the start date (format: YYMMDD, e.g., 240901): ")
    end_date = input("Enter the end date (format: YYMMDD, e.g., 240915): ")
    year = input("Enter the year (format: YYYY, e.g., 2024): ")

# Generate the list of dates in the provided range
dates = []
for i in range(int(start_date[-2:]), int(end_date[-2:]) + 1):
    date = start_date[:-2] + str(i).zfill(2)  # Concatenate the year and day, ensuring it has two digits
    dates.append(date)

# Specify the directory and output file name
directory = os.path.join(os.getenv('USERPROFILE'), "Documents", "00_PJBC", "Data")
file_name = f"Data_{start_date}_{end_date}.csv"
file_path = os.path.join(directory, file_name)

# Create the directory if it doesn't exist
os.makedirs(directory, exist_ok=True)

# Function to process each URL
def process_url(date):
    url = f"https://www.pjbc.gob.mx/boletinj/{year}/my_html/bc{date}.htm"
    data = []
    
    try:
        # Request the webpage
        response = requests.get(url)
        response.encoding = response.apparent_encoding  # Use automatically detected encoding
        
        # Parse the HTML content
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # Find paragraphs and other elements that may contain the text
        paragraphs = soup.find_all(['p', 'div', 'span'], class_=['MsoNormal', None])
        
        # Extract relevant data
        for para in paragraphs:
            text = para.get_text(separator=" ").strip()  # Extract the text and clean spaces
            if text:
                data.append([text, date])  # Add the data to the list

    except Exception as e:
        print(f"Error processing {url}: {e}")
    
    return data

# Number of threads (adjust based on available resources)
num_threads = 5

# Open the CSV file in write mode
with open(file_path, mode='w', newline='', encoding='utf-8') as csv_file:
    csv_writer = csv.writer(csv_file)
    
    # Write headers to the CSV file
    csv_writer.writerow(["Data", "Date"])  # Headers "Data" and "Date"

    # Create a progress bar
    with tqdm(total=len(dates), desc="Downloading pages", unit="page") as progress_bar:
        # Use ThreadPoolExecutor to handle downloads concurrently
        with ThreadPoolExecutor(max_workers=num_threads) as executor:
            # Submit all URLs to the thread pool
            futures = {executor.submit(process_url, date): date for date in dates}
            
            # As tasks are completed, update the file and progress bar
            for future in as_completed(futures):
                result = future.result()
                if result:  # If valid data is returned
                    csv_writer.writerows(result)  # Write all downloaded data
                progress_bar.update(1)

print("Download complete, data saved to", file_path)

解决指导

  • 补全隐藏依赖导入:部分库在打包时不会被自动检测到,导致打包过程中触发交互请求。在auto-py-to-exe的「Advanced」选项中添加hidden-import,填入requests、bs4、tqdm、concurrent.futures;或者直接用PyInstaller命令行:
    pyinstaller --onefile --console your_script.py --hidden-import requests --hidden-import bs4 --hidden-import tqdm
    
  • 清理打包缓存:删除项目目录下的build、dist文件夹和生成的.spec文件,避免旧配置残留导致的异常,之后重新启动打包流程。
  • 指定动态库搜索路径:卡在动态库搜索阶段时,可手动指定Python依赖库的路径,比如:
    pyinstaller --onefile --console your_script.py --paths "C:\PythonXX\Lib\site-packages"
    
  • 更换打包工具测试:如果auto-py-to-exe和py2exe问题持续,尝试用nuitka或cx_Freeze。比如nuitka的打包命令:
    nuitka --standalone --onefile your_script.py
    
  • 排查依赖库的交互请求:部分加密或系统相关的依赖库在初始化时可能会触发控制台输入,可临时注释脚本中的非核心代码(比如网络请求、多线程部分),逐步排查是哪个环节导致的输入请求。

内容的提问来源于stack exchange,提问作者Luis Pacheco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 17:32:02