创建虚拟环境
python3 -m venv venv
激活虚拟环境(Mac)
bash
source venv/bin/activate
系统环境变量缺失问题
MacBook-Pro:hello hl$ python3 -m venv venv
bash: sed: command not found
bash: python3: command not found
VS Code的终端类型 (如bash
、zsh
)可能会影响某些命令的执行,尤其是在macOS系统中。 VS Code的终端类型 (如bash
、zsh
)可能会影响某些命令的执行,尤其是在macOS系统中。以下是具体原因和解决方案:
为什么bash
终端会失败?
-
Python路径问题
- macOS系统默认的
bash
可能没有正确加载你的PATH
环境变量(尤其是通过Homebrew安装的Python路径未被包含)。 zsh
(macOS Catalina及更高版本的默认Shell)会自动加载~/.zshrc
或~/.zprofile
中的配置,而bash
可能不会加载~/.bash_profile
或~/.bashrc
。
- macOS系统默认的
-
虚拟环境激活脚本兼容性
venv/bin/activate
脚本可能依赖某些Shell特性(如数组操作),而旧版bash
(macOS默认的bash
是3.2版本)可能不支持。
解决方案
方法1:强制VS Code使用zsh
终端
-
修改VS Code的默认终端
-
打开VS Code,按快捷键:
- macOS :
Cmd + Shift + P
- Windows/Linux :
Ctrl + Shift + P
- macOS :
-
输入 **
Terminal: Select Default Profile
**,选择zsh
。
-
-
直接修改VS Code配置
在
settings.json
中添加:
json
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"args": ["-l"] // 强制登录Shell,加载配置
}
},
"terminal.integrated.defaultProfile.osx": "zsh"