程序员老王 发布的文章

import sys
from pathlib import Path

# 设置一个跨平台的统一路径
def get_playwright_browsers_path():
    # 获取用户主目录

    home_dir = Path.home()

    # 在主目录下创建一个固定的 Playwright 浏览器路径
    playwright_path = home_dir / ".playwright_browsers"

    # 确保路径存在
    playwright_path.mkdir(parents=True, exist_ok=True)

    return str(playwright_path)

# 动态设置浏览器路径
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = get_playwright_browsers_path()

def ensure_browser_installed():
    try:
        from playwright.__main__ import main as playwright_main
        playwright_main()  # 自动安装 Chromium
    except Exception as e:
        print(f"Error during Playwright installation: {e}")
if __name__ == "__main__":
    # check args
    args = sys.argv
    if len(args)>1 and args[1] == 'install':
        ensure_browser_installed()
    app = QApplication(sys.argv)
    main_window = URLFetcherApp()
    main_window.show()
    sys.exit(app.exec_())

打包之后运行

main.exe install

pre {
      white-space: pre-wrap; /* 保留格式并自动换行 */
      word-wrap: break-word; /* IE 浏览器兼容 */
      overflow-wrap: break-word; /* 现代浏览器兼容 */
      border: 1px solid #ccc; /* 添加边框便于观察 */
      padding: 10px;
    }

import sys
import datetime
import os

def print_with_lineno(message):
    frame = sys._getframe(1)  # 获取调用者的帧
    lineno = frame.f_lineno  # 行号
    filename = os.path.basename(frame.f_code.co_filename)  # 提取文件名
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 格式化时间
    print(f"{current_time} - {filename} - Line {lineno}: {message}")

# 示例
print_with_lineno("This is a test message")
print_with_lineno("Another message")