超详细Claude Code安装使用指南(Mac版)_claude code mac
一、Claude code是什么
Claude Code 是Anthropic 推出的一款Agent编码工具,支持在终端运行,理解代码库,并通过自然语言命令帮助用户更快的编写代码,功能和Cursor类似,但claude code是一个命令行工具,它没有UI界面。
二、Claude code强在哪
- 自动收集理解项目上下文,无须手动将文件加入上下文
- 自动提交代码
- 生成文档 / 创建 PR
- 执行测试 / 修复 bug
- 编写脚本 / 管理成本
- 配合其他 AI 模型,实现混合编程
如果说 Cursor 是 AK47,那么 Claude Code 就是自动巡航导弹。
- 从\"
和AI一起写代码
\"到\"让AI为你写代码
\",让你可以更加地专注聚焦。
三、Claude code安装配置
1、安装node.js
# 安装命令brew install node
由于我的网络问题,所以我是用以下方式安装的node.js
ch@chdeMacBook-Air ~ % curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 16555 100 16555 0 0 38 0 0:07:15 0:07:14 0:00:01 2371=> Downloading nvm from git to \'/Users/ch/.nvm\'=> 正克隆到 \'/Users/ch/.nvm\'...remote: Enumerating objects: 383, done.remote: Counting objects: 100% (383/383), done.remote: Compressing objects: 100% (326/326), done.remote: Total 383 (delta 43), reused 179 (delta 29), pack-reused 0 (from 0)接收对象中: 100% (383/383), 392.12 KiB | 99.00 KiB/s, 完成.处理 delta 中: 100% (43/43), 完成.* (头指针在 FETCH_HEAD 分离) master=> Compressing and cleaning up git repository=> Appending nvm source string to /Users/ch/.zshrc=> Appending bash_completion source string to /Users/ch/.zshrc=> Close and reopen your terminal to start using nvm or run the following to use it now:export NVM_DIR=\"$HOME/.nvm\"[ -s \"$NVM_DIR/nvm.sh\" ] && \\. \"$NVM_DIR/nvm.sh\" # This loads nvm[ -s \"$NVM_DIR/bash_completion\" ] && \\. \"$NVM_DIR/bash_completion\" # This loads nvm bash_completionch@chdeMacBook-Air ~ % nvm --versionzsh: command not found: nvmch@chdeMacBook-Air ~ % ch@chdeMacBook-Air ~ % nvm --version0.39.7ch@chdeMacBook-Air ~ % nvm install --ltsInstalling latest LTS version.Downloading and installing node v22.17.1...Downloading https://nodejs.org/dist/v22.17.1/node-v22.17.1-darwin-arm64.tar.xz...######################################################################### 100.0%Computing checksum with sha256sumChecksums matched!Now using node v22.17.1 (npm v10.9.2)Creating default alias: default -> lts/* (-> v22.17.1)ch@chdeMacBook-Air ~ % nvm alias default \'lts/*\'default -> lts/* (-> v22.17.1)ch@chdeMacBook-Air ~ % node --versionv22.17.1ch@chdeMacBook-Air ~ % npm --version10.9.2ch@chdeMacBook-Air ~ %
2、安装claude code
# 安装claude code命令npm install -g @anthropic-ai/claude-code# 验证安装claude --version
-g 标志表示 Claude Code 全局安装,使其可从任何目录使用
ch@chdeMacBook-Air ~ % npm install -g @anthropic-ai/claude-codeadded 3 packages in 22s2 packages are looking for funding run `npm fund` for detailsnpm noticenpm notice New major version of npm available! 10.9.2 -> 11.5.1npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.5.1npm notice To update run: npm install -g npm@11.5.1npm noticech@chdeMacBook-Air ~ % claude --version1.0.61 (Claude Code)
3、配置环境变量
前提:需要先拥有claude code的key
3.1 可将环境变量写入 ./bash_profile或 /.bashrc或 /.zshrc
# 写入环境变量命令echo -e \'\\n export ANTHROPIC_AUTH_TOKEN=sk-...\' >> ~/.bash_profileecho -e \'\\n export ANTHROPIC_BASE_URL=https://anyrouter.top\' >> ~/.bash_profileecho -e \'\\n export ANTHROPIC_API_KEY=sk-ant-oat01\' >> ~/.bash_profileecho -e \'\\n export ANTHROPIC_AUTH_TOKEN=sk-...\' >> ~/.bashrcecho -e \'\\n export ANTHROPIC_BASE_URL=https://anyrouter.top\' >> ~/.bashrcecho -e \'\\n export ANTHROPIC_API_KEY=sk-ant-oat01\' >> ~/.bashrcecho -e \'\\n export ANTHROPIC_AUTH_TOKEN=sk-...\' >> ~/.zshrcecho -e \'\\n export ANTHROPIC_BASE_URL=https://anyrouter.top\' >> ~/.zshrcecho -e \'\\n export ANTHROPIC_API_KEY=sk-ant-oat01\' >> ~/.zshrc
3.2 如何选择?
选择哪个文件取决于你默认使用的 shell,只写入一个文件即可!
- 如果输出 **
/bin/bash
:你应该将环境变量添加到 ~/.bash_profile
或 ~/.bashrc
**。 - 如果输出 **
/bin/zsh
:你应该将环境变量添加到 ~/.zshrc
**。
你的系统默认 shell:
- 较老的 macOS (Catalina 之前): 默认是
bash
。 - macOS Catalina (10.15) 及更新版本: 默认 shell 已更改为
zsh
。如果你没有手动修改过,很可能就是zsh
。
# 验证shellch@chdeMacBook-Air ~ % echo $SHELL/bin/zsh# 写入环境变量ch@chdeMacBook-Air ~ % echo -e \'\\n export ANTHROPIC_AUTH_TOKEN=sk-ant-oat01\' >> ~/.zshrcecho -e \'\\n export ANTHROPIC_BASE_URL=https://gecode\' >> ~/.zshrcecho -e \'\\n export ANTHROPIC_API_KEY=sk-ant-oat01\' >> ~/.zshrc
3.3 让配置生效
最简单的方法是:关闭当前终端窗口,再重新打开一个新的终端窗口。新窗口会自动加载修改后的配置文件。
# 验证是否生效echo $ANTHROPIC_AUTH_TOKENecho $ANTHROPIC_BASE_URLecho $ANTHROPIC_API_KEY
ch@chdeMacBook-Air ~ % echo $ANTHROPIC_AUTH_TOKENecho $ANTHROPIC_BASE_URLsk-ant-oat01https://gaccodech@chdeMacBook-Air ~ %
4、使用claude code
4.1 安装完成后,你可以在任何项目目录中开始使用 Claude Code
## 进入项目目录:cd your-project-folder## 在项目目录下运行:claude
4.2 运行后
- 选择你喜欢的主题 + Enter
- 确认安全须知 + Enter
- 使用默认 Terminal 配置 + Enter
- 信任工作目录 + Enter
5、claude code项目实战演示
5.1 创建claude.md,让AI通过生成md先熟悉你的项目
- 在命令行输入/init
- AI生成完成后,会询问是否需要往项目里创建文件
- 通过上下箭头进行选择,选择yes后点击回车即可
- 由于生成是英文的,可以继续让它翻译成中文写入
5.2 新需求示例,AI思考和修改如下:
它可以帮你干完一整套:从分析、修改到测试验证
生成测试用例时,传参包含了测试用例数量字段,由于不好评估每个需求需要生成多少测试用例,所以想做下兼容,把这个字段改成可选字段,用户在 页面未勾选该字段时则不传测试用例数量,若勾选后则需要传✻ Thinking… 用户想要修改测试用例生成功能,将测试用例数量字段改为可选字段。具体需求是: 1. 在页面上添加一个勾选框来控制是否指定测试用例数量 2. 如果用户不勾选,则不传测试用例数量参数 3. 如果勾选了,则传递用户指定的数量 我需要: 1. 修改 app.py 中的UI部分,添加勾选框控制 2. 修改传参逻辑,根据勾选状态决定是否传递数量参数 3. 修改 utils.py 中的函数,让测试用例数量参数变为可选 4. 当不传数量时,需要有一个默认的处理逻辑 让我先看看当前的实现,然后进行修改。⏺ 我来帮你修改测试用例数量字段为可选字段。需要修改UI界面和后端逻辑来支持这个功能。⏺ Update Todos ⎿ ☐ 修改UI界面,添加测试用例数量可选控制 ☐ 修改后端函数,支持可选的测试用例数量参数 ☐ 测试修改后的功能✻ Thinking… 让我先修改 app.py 中的UI部分,添加一个勾选框来控制是否指定测试用例数量。 现在的代码中,测试用例数量是这样处理的: with col_count: test_case_count = st.number_input(\"测试用例数量\", min_value=1, max_value=200, value=3) 我需要添加一个勾选框,当勾选时才显示数量输入框。⏺ Read(app.py) ⎿ Read 25 lines (ctrl+r to expand)✻ Thinking… 现在我需要修改 app.py 中的UI部分。我会: 1. 在第三列(col_count)中添加一个勾选框来控制是否指定测试用例数量 2. 根据勾选状态显示或隐藏数量输入框 3. 修改调用测试用例生成函数的逻辑,根据勾选状态决定是否传递数量参数 让我先修改UI部分:⏺ Update(app.py) ⎿ Updated app.py with 5 additions and 1 removal 63 ) 64 65 with col_count: 66 - test_case_count = st.number_input(\"测试用例数量\", min_value=1, max_value=200, value=3) 66 + specify_count = st.checkbox(\"指定测试用例数量\", value=False) 67 + if specify_count: 68 + test_case_count = st.number_input(\"测试用例数量\", min_value=1, max_value=200, value=3) 69 + else: 70 + test_case_count = None 71 72 # 生成按钮 73 generate_button = st.button(\"生成测试用例\", type=\"primary\")✻ Thinking… 现在我需要修改调用 generate_test_cases 函数的逻辑,让它支持可选的测试用例数量参数。我需要先查看当前的调用代码:⏺ Read(app.py) ⎿ Read 15 lines (ctrl+r to expand)✻ Thinking… 我需要修改这个调用逻辑,根据 test_case_count 是否为 None 来决定是否传递这个参数。我可以使用条件传参的方式:⏺ Update(app.py) ⎿ Updated app.py with 10 additions and 7 removals 96 with st.spinner(\"正在生成测试用例...\"): 97 try: 98 # 调用测试用例生成函数 99 -test_cases = generate_test_cases( 100 - requirement_text=requirement_text, 101 - test_level=test_level, 102 - test_priority=test_priority, 103 - test_case_count=test_case_count, 104 - prompt=prompt 105 -) 99 +kwargs = { 100 + \'requirement_text\': requirement_text, 101 + \'test_level\': test_level, 102 + \'test_priority\': test_priority, 103 + \'prompt\': prompt 104 +} 105 +if test_case_count is not None: 106 + kwargs[\'test_case_count\'] = test_case_count 107 + 108 +test_cases = generate_test_cases(**kwargs) 109 st.session_state.test_cases = test_cases 110 st.success(f\"成功生成 {len(test_cases[\'test_cases\'])} 个测试用例\") 111 except Exception as e:✻ Thinking… 现在我需要修改 utils.py 中的 generate_test_cases 函数,让 test_case_count 参数变为可选的。我需要先查看当前的函数定义:⏺ Update Todos ⎿ ☒ 修改UI界面,添加测试用例数量可选控制 ☐ 修改后端函数,支持可选的测试用例数量参数 ☐ 测试修改后的功能✻ Thinking… 让我查看 utils.py 中 generate_test_cases 函数的定义:⏺ Read(utils.py) ⎿ Read 10 lines (ctrl+r to expand)✻ Thinking… 我需要修改 generate_test_cases 函数的参数定义,让 test_case_count 变为可选参数,并设置一个默认值。我还需要查看异步版本的函数定义:⏺ Read(utils.py) ⎿ Read 10 lines (ctrl+r to expand)✻ Thinking… 我需要修改两个函数: 1. generate_test_cases_async (异步版本) 2. generate_test_cases (同步版本) 首先修改异步版本的函数,将 test_case_count 设为可选参数,并在函数内部处理默认值。我需要看一下函数内部如何使用 test_case_count 的:⏺ Read(utils.py) ⎿ Read 15 lines (ctrl+r to expand)✻ Thinking… 我看到函数内部在多个地方使用了 test_case_count。当用户不指定数量时,我需要设置一个合理的默认值。基于代码逻辑,我认为可以设置一个默认 值,比如根据需求文本的长度或复杂度来智能推荐数量,或者简单设置为一个固定值如5个。 让我修改异步版本的函数:⏺ Update(utils.py) ⎿ Updated utils.py with 15 additions and 1 removal 158 requirement_text: str, 159 test_level: str, 160 test_priority: str, 161 - test_case_count: int, 161 + test_case_count: int = None, 162 prompt: str = \"\" 163 ) -> Dict[str, List[Dict[str, str]]]: 164 \"\"\" 165 增强的分批生成测试用例异步版本,特别优化JSON解析 166 \"\"\" 167 + # 如果未指定测试用例数量,根据需求文本长度智能推荐 168 + if test_case_count is None: 169 + # 根据需求文本长度智能推荐测试用例数量 170 + text_length = len(requirement_text) 171 + if text_length < 500: 172 + test_case_count = 3 173 + elif text_length < 1500: 174 + test_case_count = 5 175 + elif text_length < 3000: 176 + test_case_count = 8 177 + else: 178 + test_case_count = 10 179 + logging.info(f\"未指定测试用例数量,根据需求文本长度({text_length}字符)智能推荐生成{test_case_count}个测试用例\") 180 + 181 # 验证输入 182 if not (1 <= test_case_count Dict[str, List[Dict[str, str]]]: 400 \"\"\"✻ Thinking… 现在我还需要修改同步版本函数中调用异步版本的部分,确保参数传递正确:⏺ Read(utils.py) ⎿ Read 10 lines (ctrl+r to expand)✻ Thinking… 这里的参数传递已经是正确的了,因为我们只是修改了参数的默认值,不需要改变这里的传递方式。现在让我测试一下修改是否正确。首先让我启动应 用看看:⏺ Update Todos ⎿ ☒ 修改UI界面,添加测试用例数量可选控制 ☒ 修改后端函数,支持可选的测试用例数量参数 ☐ 测试修改后的功能✻ Thinking… 让我检查一下修改是否完整。我还需要确认一下当未勾选测试用例数量时,提示词模板中是否也需要相应调整,因为提示词中可能包含了测试用例数量 的引用:⏺ Search(pattern: \"test_case_count\", path: \"utils.py\", output_mode: \"content\") ⎿ Found 30 lines (ctrl+r to expand)✻ Thinking… 我看到代码中有多处使用了 test_case_count,但看起来我的修改应该是正确的,因为在异步函数开始时就会对 None 值进行处理并设置默认值。 让我检查一下提示词模板中是否有对测试用例数量的引用:⏺ Search(pattern: \"{test_case_count}\", path: \"utils.py\", output_mode: \"content\") ⎿ Found 0 lines ✻ Thinking… 好的,看起来提示词模板中没有直接引用 test_case_count 变量。让我检查一下基础提示词模板和批次提示词模板中是否有相关的数量引用:⏺ Read(utils.py) ⎿ Read 20 lines (ctrl+r to expand)✻ Thinking… 看起来提示词中使用的是 current_count 而不是直接的 test_case_count,这是在批次处理逻辑中计算得出的,所以不会有问题。 现在让我验证一下修改是否能正常工作。我可以创建一个简单的测试脚本或者直接运行应用来检查:⏺ Bash(source .venv/bin/activate && python -c \" import sys…) ⎿ 测试不传递test_case_count参数... 成功!智能推荐生成了 3 个测试用例 请求数量: 3 … +3 lines (ctrl+r to expand)⏺ Bash(source .venv/bin/activate && python -c \" import sys…) ⎿ 测试传递test_case_count=5参数... 成功!指定生成了 5 个测试用例 请求数量: 5 … +2 lines (ctrl+r to expand) ⎿ API Error (Request timed out.) · Retrying in 1 seconds… (attempt 1/10) ⎿ API Error (Request timed out.) · Retrying in 1 seconds… (attempt 2/10) ⎿ API Error (Request timed out.) · Retrying in 2 seconds… (attempt 3/10)⏺ Bash(source .venv/bin/activate && streamlit run app.py --server.headless=true --server.port=8502 & sleep 3…) ⎿ Error: Command timed out after 2m 0.0s /Users/ch/Desktop/Case/.venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the \'ssl\' module is compiled with \'LibreSSL 2.8.3\'. See: https://github.com/ur/ub/issues/3020 warnings.warn( 2025-07-28 22:49:27,810 - INFO - 未指定测试用例数量,根据需求文本长度(20字符)智能推荐生成3个测试用例 2025-07-28 22:49:27,811 - INFO - 生成3条测试用例,分为1批,每批约3条 2025-07-28 22:49:54,233 - INFO - 批处理1 JSON解析成功 Streamlit应用已启动,可以在浏览器中访问 http://localhost:8502 进行测试 gp 19167 0.1 0.8 412080464 135984 s003 S+ 10:48下午 0:00.72 /Users/ch/Desktop/Case/.venv/bin/python /Users/ch/Desktop/Case/.venv/bin/streamlit run app.py gp 19221 0.0 0.0 410595232 1200 ?? R 10:48下午 0:00.00 grep streamlit gp 19222 0.0 0.0 410904944 1136 ?? S 10:48下午 0:00.00 /bin/zsh -c -l source /Users/ch/.claude/shell-snapshots/snapshot-zsh-1753712359115-jph3wp.sh && eval \'source .venv/bin/activate && streamlit run app.py --server.headless=true --server.port=8502 &\\012sleep 3\\012echo \"Streamlit应用已启动,可以在浏览器中访问 http://localhost:8502 进行测试\"\\012ps aux | grep streamlit\' \\| /var/folders/8f/qmgts4s128j794g000gn/T/claude-6f35-cwd … +13 lines (ctrl+r to see all)⏺ Update Todos ⎿ ☒ 修改UI界面,添加测试用例数量可选控制 ☒ 修改后端函数,支持可选的测试用例数量参数 ☒ 测试修改后的功能⏺ ✅ 功能修改和测试完成! 已完成的修改: 1. UI界面修改 (app.py) - 添加了\"指定测试用例数量\"复选框 - 只有勾选后才显示数量输入框 - 未勾选时 test_case_count = None 2. 后端函数修改 (utils.py) - generate_test_cases() 和 generate_test_cases_async() 函数的 test_case_count 参数改为可选,默认值为 None - 添加智能推荐逻辑:根据需求文本长度自动推荐测试用例数量 - < 500字符:推荐3个 - 500-1500字符:推荐5个 - 1500-3000字符:推荐8个 3000字符:推荐10个 3. 测试验证 - ✅ 不传递 test_case_count 参数:成功生成3个测试用例(智能推荐) - ✅ 传递 test_case_count=5 参数:成功生成5个测试用例(用户指定) - ✅ Streamlit应用已启动,可在 http://localhost:8502 测试UI功能 现在用户可以: 1. 不勾选\"指定测试用例数量\"→ 系统根据需求文本长度智能推荐 2. 勾选后输入具体数量 → 按用户指定数量生成