mac/win使用pyinstaller打包app/exe文件,活着执行脚本,双击运行

🌸 踩坑记录

  • python环境最好使用虚拟环境,推荐使用conda管理,并且若本地有python环境,不要使用和 本地环境版本 相同 的虚拟环境
    • 这里踩坑较多,已经记不清楚注意点
      • 虚拟环境
      • python版本不要和本地环境一样
  • mac/win只能打包相应的程序,mac对应app,win对应exe,也可使用终端/窗口还能看见日志。
  • mac压缩后传递可能出现软件损坏/开发者未受信任情况
    • 使用终端运行 sudo spctl --master-disable
    • 系统设置、安全与隐私、允许来自任何来源,勾选
  • 若有路径读取请使用os.path.dirname(sys.argv[0]),并且最好放在同一个文件夹下
python 复制代码
# 重点这行
path = os.path.dirname(sys.argv[0])
# 拼接路径
f = os.path.join(path, 'url.txt')
# 按行读取,形成列表
websites = open(f).readlines()
  • 记录打包命令
python 复制代码
"""
常用参数 含义
-i 或 -icon 生成icon
-F 创建一个绑定的可执行文件
-w 使用窗口,无控制台
-C 使用控制台,无窗口
-D 创建一个包含可执行文件的单文件夹包(默认情况下)
-n 文件名 

mac pyinstaller -F -w -i favicon.ico trustCertificates.py
win pyinstaller -F --onefile -i favicon.ico trustCertificates.py
"""


🌸 源码

python 复制代码
import os
import sys

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

"""
常用参数 含义
-i 或 -icon 生成icon
-F 创建一个绑定的可执行文件
-w 使用窗口,无控制台
-C 使用控制台,无窗口
-D 创建一个包含可执行文件的单文件夹包(默认情况下)
-n 文件名 

mac pyinstaller -F -w -i favicon.ico trustCertificates.py
win pyinstaller -F --onefile -i favicon.ico trustCertificates.py
"""

chrome_options = Options()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

# 创建Chrome驱动程序
chrome_driver_path = 'path_to_chromedriver'
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)

# 网站列表
path = os.path.dirname(sys.argv[0])
f = os.path.join(path, 'url.txt')

websites = open(f).readlines()

# 循环访问每个网站并信任证书
for website in websites[1:]:
    try:
        driver.get(website)
        # 等待直到页面加载完成
        WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
        print(f"成功信任证书: {website}")
    except Exception as e:
        raise e
        print(f"无法信任证书: {website}, 错误信息: {str(e)}")

driver.get(websites[0])

while True:
    pass
相关推荐
AI探索者3 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者3 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh5 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅5 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽6 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时9 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿12 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python