Happy_language/setup_python_env.sh
2025-09-15 00:15:37 +08:00

71 lines
2.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "🔧 开始配置Python 3.10.16环境..."
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${YELLOW}📋 步骤1: 检查当前配置${NC}"
echo "当前.zshrc中的pyenv配置"
tail -5 ~/.zshrc | grep -E "(PYENV|pyenv)" || echo "未找到pyenv配置"
echo -e "\n${YELLOW}📋 步骤2: 设置本地Python版本${NC}"
pyenv local 3.10.16
echo "已设置本地Python版本为3.10.16"
echo -e "\n${YELLOW}📋 步骤3: 验证Python版本${NC}"
echo "当前Python版本: $(python --version)"
echo "Python路径: $(which python)"
echo -e "\n${YELLOW}📋 步骤4: 重新创建虚拟环境${NC}"
# 删除现有虚拟环境
if [ -d "venv" ]; then
echo "删除现有虚拟环境..."
rm -rf venv
fi
# 创建新的虚拟环境
echo "创建新的虚拟环境..."
python -m venv venv
# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate
echo "虚拟环境中的Python版本: $(python --version)"
echo -e "\n${YELLOW}📋 步骤5: 更新.gitignore${NC}"
# 检查.gitignore中是否已有venv/
if ! grep -q "venv/" .gitignore 2>/dev/null; then
echo "venv/" >> .gitignore
echo "已添加venv/到.gitignore"
else
echo ".gitignore中已存在venv/"
fi
echo -e "\n${YELLOW}📋 步骤6: 验证配置${NC}"
echo "检查.python-version文件"
if [ -f ".python-version" ]; then
echo "✅ .python-version存在内容: $(cat .python-version)"
else
echo "❌ .python-version文件不存在"
fi
echo -e "\n${GREEN}✅ 环境配置完成!${NC}"
echo -e "${GREEN}📝 重要提示:${NC}"
echo "1. 虚拟环境已激活Python版本: $(python --version)"
echo "2. 每次进入项目目录时,使用: source venv/bin/activate"
echo "3. 退出虚拟环境使用: deactivate"
echo "4. .python-version文件确保项目目录始终使用Python 3.10.16"
echo -e "\n${YELLOW}🧪 测试建议:${NC}"
echo "重新打开终端,进入项目目录,运行以下命令测试:"
echo "cd $(pwd)"
echo "python --version # 应该显示Python 3.10.16"
echo "source venv/bin/activate"
echo "python --version # 应该显示Python 3.10.16"