Skip to content

Commit d56007c

Browse files
refactor(install): 重构安装脚本并更新文档
将安装脚本 `install.py` 移动到 `install` 目录下,并增加 `rich` 库用于美化输出。同时,更新 `README.md` 以反映新的安装流程,并删除旧的手动安装说明。新增 `install.sh` 脚本以支持跨平台安装 Python 和 pip。
1 parent cd60f05 commit d56007c

File tree

5 files changed

+122
-39
lines changed

5 files changed

+122
-39
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818

1919
## 安装部署
2020

21-
### 1. 安装 Python 依赖
22-
```bash
23-
# 创建虚拟环境(推荐)
24-
python3 -m venv venv
25-
source venv/bin/activate
26-
27-
# 安装依赖包
28-
pip install fastapi uvicorn DrissionPage
21+
### 环境要求
22+
- Python 3.9+
23+
- Python-Pip
24+
- Windows/Linux/MacOS
25+
26+
### 安装依赖
27+
#### 自动安装
28+
```bash
29+
python install.py #自动安装配置
2930
```
30-
31-
### 2. 安装浏览器内核
31+
#### 手动安装
3232
```bash
33+
pip install -r requirements.txt
34+
35+
playwright install
3336
playwright install chromium
3437
```
3538

install.py

-28
This file was deleted.

install/install.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 自动安装依赖文件中的依赖并配置
2+
import subprocess
3+
import sys
4+
from rich.console import Console
5+
from rich.progress import Progress
6+
7+
def install_dependencies():
8+
with Progress() as progress:
9+
task = progress.add_task("[cyan]安装依赖...", total=1)
10+
console = Console()
11+
try:
12+
result = subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
13+
capture_output=True, text=True)
14+
console.print(f'[bold green]\n{result.stdout}')
15+
print("依赖已安装。")
16+
except subprocess.CalledProcessError:
17+
console.print("[bold red]安装依赖时发生错误。")
18+
19+
def configure():
20+
console = Console()
21+
try:
22+
console.print("正在安装浏览器驱动(PlayWright环境[Chromium, Microsoft Edge, WebKit等])...")
23+
subprocess.check_call(["playwright", "install"])
24+
subprocess.check_call(["playwright", "install", "chromium"])
25+
except subprocess.CalledProcessError:
26+
console.print("[bold red]安装浏览器驱动时发生错误。")
27+
finally:
28+
console.print("[bold green]✓ 配置完成")
29+
30+
def run():
31+
install_dependencies()
32+
configure()
33+
34+
if __name__ == "__main__":
35+
run()

install/install.sh

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
# 系统检测函数
4+
detect_system() {
5+
if [[ -f /etc/os-release ]]; then
6+
. /etc/os-release
7+
case $ID in
8+
debian|ubuntu) echo "debian";;
9+
centos|fedora|rhel) echo "rhel";;
10+
arch) echo "arch";;
11+
*) echo "unknown";;
12+
esac
13+
elif [[ $(uname) == "Darwin" ]]; then
14+
echo "macos"
15+
else
16+
echo "unknown"
17+
fi
18+
}
19+
20+
# 安装函数
21+
install_packages() {
22+
case $1 in
23+
debian)
24+
sudo apt update && sudo apt install -y python3 python3-pip
25+
;;
26+
rhel)
27+
if command -v dnf &> /dev/null; then
28+
sudo dnf install -y python3 python3-pip
29+
else
30+
sudo yum install -y python3 python3-pip
31+
fi
32+
;;
33+
arch)
34+
sudo pacman -Sy --noconfirm python python-pip
35+
;;
36+
macos)
37+
if ! command -v brew &> /dev/null; then
38+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
39+
fi
40+
brew install python3
41+
;;
42+
*)
43+
echo "不支持的系统"
44+
exit 1
45+
;;
46+
esac
47+
}
48+
49+
# 主执行流程
50+
set -euo pipefail
51+
52+
system_type=$(detect_system)
53+
if [[ $system_type == "unknown" ]]; then
54+
echo "错误:无法识别的操作系统"
55+
exit 1
56+
fi
57+
58+
echo "检测到系统类型:$system_type"
59+
echo "开始安装Python和pip..."
60+
61+
if ! install_packages $system_type; then
62+
echo "安装失败,请检查错误信息"
63+
exit 1
64+
fi
65+
66+
# 验证安装
67+
if ! command -v python3 --version &> /dev/null || ! command -v pip3 --version &> /dev/null; then
68+
echo "安装验证失败"
69+
exit 1
70+
fi
71+
72+
echo "安装成功!"

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fastapi
22
uvicorn
33
DrissionPage
4-
playwright
4+
playwright
5+
rich

0 commit comments

Comments
 (0)