没啥用软件 - Python库的批量安装
本帖最后由 快乐的小驹 于 2025-5-10 08:55 编辑前些天电脑出现了问题,我从新安装了Python环境。等到安装库的时候,如果不安装会影响我以前写的一些代码。
幸好我以前安装什么库都记录了下来。就写了一个能批量安装Python库的Python代码。(有兴趣的可以自己编译成exe,以后留用。)
功能:
1,他首先会检查你是否安装pip,如果没有安装会自动安装。
2,他会检查你pip的版本,如果不是最新版会自动更新。
3,批量安装指定的Python包,优先使用国内镜像源,如果失败会自动重试。
如果你感兴趣,免费的评分给一个吧。
import subprocess
import time
import sys
import io
# 设置标准输出和错误输出的编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# 需要安装的包列表
PACKAGES = [
"Flask",
"pyperclip",
"pycryptodome",
"wmi",
"pillow",
"ntplib",
"pyDes",
"requests",
"pystray",
"baidu-aip",
"pandas",
"docxtpl",
"openpyxl",
"lxml",
"retrying",
"bs4",
"mysql-connector-python",
"xlwt",
"faker",
"pysimplegui",
"PyQt5",
"qrcode",
"opencv-python",
"PyPDF2 Pillow",
"humanize",
"edge-tts",
"tqdm",
"PyPDF2",
"ttkbootstrap",
"pyecharts",
"screeninfo",
"mss",
"fitz",
"PySide6",
"print",
"waitress",
"PyQt6",
]
def install_package(package, max_retries=3, retry_delay=10):
"""
安装指定的Python包,优先使用国内镜像源,如果失败会自动重试
:param package: 要安装的包名
:param max_retries: 最大重试次数
:param retry_delay: 重试间隔(秒)
"""
retry_count = 0
# 国内镜像源列表
MIRRORS = [
"https://pypi.tuna.tsinghua.edu.cn/simple",
"https://mirrors.aliyun.com/pypi/simple/",
"https://pypi.mirrors.ustc.edu.cn/simple/"
]
while retry_count < max_retries:
try:
print(f"正在安装 {package}... (尝试 {retry_count + 1}/{max_retries})")
# 前3次尝试使用国内镜像源
if retry_count < 3:
mirror = MIRRORS
print(f"使用镜像源: {mirror}")
cmd = ]
else:
# 3次失败后使用默认源
print("切换至默认源")
cmd =
subprocess.check_call(cmd, shell=True, encoding='utf-8')
print(f"成功安装 {package}")
return True
except subprocess.CalledProcessError as e:
print(f"安装 {package} 失败: {e}")
retry_count += 1
if retry_count < max_retries:
print(f"等待 {retry_delay} 秒后重试...")
time.sleep(retry_delay)
print(f"安装 {package} 失败,已达到最大重试次数 {max_retries}")
return False
def check_and_install_pip(max_retries=3, retry_delay=10):
"""
检查并安装pip,如果失败会自动重试
:param max_retries: 最大重试次数
:param retry_delay: 重试间隔(秒)
:return: 是否安装成功
"""
retry_count = 0
while retry_count < max_retries:
try:
print(f"检查pip安装状态... (尝试 {retry_count + 1}/{max_retries})")
subprocess.check_call(, shell=True, encoding='utf-8')
print("pip已安装")
# 检查并更新pip到最新版本
try:
print("正在检查pip更新...")
subprocess.check_call(,
shell=True, encoding='utf-8')
print("pip已更新到最新版本")
except subprocess.CalledProcessError as e:
print(f"pip更新失败: {e}")
return True
except subprocess.CalledProcessError:
print("pip未安装,正在尝试安装...")
try:
subprocess.check_call(,
shell=True, encoding='utf-8')
print("pip安装成功")
return True
except subprocess.CalledProcessError as e:
print(f"pip安装失败: {e}")
retry_count += 1
if retry_count < max_retries:
print(f"等待 {retry_delay} 秒后重试...")
time.sleep(retry_delay)
print(f"pip安装失败,已达到最大重试次数 {max_retries}")
return False
def main():
"""主函数,依次安装所有包"""
success_count = 0
failure_count = 0
# 先检查并安装pip
if not check_and_install_pip():
print("pip安装失败,无法继续安装其他包")
return
print(f"开始安装 {len(PACKAGES)} 个Python包...")
for package in PACKAGES:
if install_package(package):
success_count += 1
else:
failure_count += 1
print(f"\n安装完成: 成功 {success_count} 个, 失败 {failure_count} 个")
if __name__ == "__main__":
main()
《Excel文件比较器v1.3》
https://www.52pojie.cn/thread-2020893-1-1.html
《个性计算器》
https://www.52pojie.cn/thread-2028540-1-1.html
没啥用软件 - 《本地测试服务器》
https://www.52pojie.cn/thread-2030222-1-1.html 有经验的话还是 pip freese + pip install -r requirements.txt 更方便 有一点小小的疑问, 这和使用 requirements.txt 区别大吗{:1_904:} 基本上是常用库
学习一下。 Lisenly 发表于 2025-5-9 15:39
有一点小小的疑问, 这和使用 requirements.txt 区别大吗
区别不大~给我这样的小白用的! 发现运行时,清华那个镜像有问题,限制我了,我删掉了,
我同时删之前打包成exe和删掉之后生成的,,运行时同样出错,,提示
Unhandled exception in script
Failed to execute script 'python依赖库' due to unhandledexception: 'NoneType' object has no attribute 'buffer
Traceback (most recent call last):File "python依赖库,py", line 7,in <module>AttributeError: 'NoneType' object has no attribute 'buffer
Close
纯小白,,知道要怎么解决么,,没生成时,,在python运行时,,是正常的,没报错 收藏!感谢大神 收藏,感谢分享 再也不怕重装系统了{:1_918:} 很实用,是我需要的,感谢提供分享!