前置要求 #
- Python 3.7+ - Codex CLI 通常需要 Python 环境
- OpenAI API Key - 如果使用 OpenAI 的服务
安装步骤 #
通过 pip 安装 #
# 1. 检查 Python 版本
python --version
# 2. 升级 pip
python -m pip install --upgrade pip
# 3. 安装 openai-codex(如果是 OpenAI 官方的)
pip install openai
# 或者安装 codex-cli
pip install codex-cli
配置文件方式 #
第一步:创建配置目录 #
打开 PowerShell,执行:
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.codex"
解释:这会在您的用户目录(通常是 C:\Users\您的用户名\)下创建一个名为 .codex 的隐藏文件夹。
第二步:创建配置文件 #
用 PowerShell 直接创建 #
@"
{
"api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
"model": "code-davinci-002",
"temperature": 0.5,
"max_tokens": 2000
}
"@ | Out-File -FilePath "$env:USERPROFILE\.codex\config.json" -Encoding UTF8
重要:把 "sk-xxxxxxxxxxxxxxxxxxxxxxxx" 替换成您真实的 OpenAI API Key
第三步:配置项说明 #
api_key:您的 OpenAI API 密钥(必填)
model:使用的模型(可选)
code-davinci-002:代码生成模型gpt-3.5-turbo:通用模型gpt-4:更强大的模型
temperature:创造性程度(可选,0-1之间)
0:更确定、可预测1:更有创造性、随机0.5:平衡选择
max_tokens:最大输出长度(可选)
- 数字越大,生成的代码越长
第四步:验证配置 #
# 检查文件是否存在
Test-Path "$env:USERPROFILE\.codex\config.json"
# 查看配置文件内容
Get-Content "$env:USERPROFILE\.codex\config.json"
如果显示 True 和您的配置内容,说明创建成功。
第五步:测试 Codex #
codex "write a hello world function"
如果能正常返回代码,说明配置成功!

问题: #
行 codex “write a hello world function"报codex : 无法加载文件 C:\nvm4w\nodejs\codex.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.co m/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1
- codex “write a hello world function”
+ CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
这是 PowerShell 执行策略的权限问题。有两种解决方法:
方法一:修改执行策略(推荐) #
以管理员身份运行 PowerShell,然后执行:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
执行后会提示确认,输入 Y 回车即可。
解释:
RemoteSigned:允许运行本地脚本,从网络下载的脚本需要签名CurrentUser:只影响当前用户,不影响系统其他用户