最近需要用 Webots 做些仿真,Python 脚本少不了,还得调 OpenCV 和 MATLAB。Windows 和 macOS 两台机器都配了一下,把过程记下来,以后自己翻也方便。
版本选择
经过一番踩坑,我固定了这套版本组合:
- Webots R2023b:对 Python 3.11 支持最好,也能联动 ROS2
- Python 3.11:不建议用 3.12,容易碰到兼容问题
- OpenCV 4.8.0:用 4.9 以上的版本可能在 Webots 里报错
- Micromamba 最新版:包管理器选轻量的
平台:Windows 10/11 和 macOS(Intel 或 Apple Silicon)都行。
安装 Python 3.11
去 python.org 下对应系统的 3.11.x 安装包。Win 用户选 64-bit 安装器,Mac 根据芯片选 universal2 或 Intel 版。
Windows 下安装时,一定要勾选 'Add Python to PATH',否则后面一堆麻烦。macOS 双击 pkg 一路下一步就行。
装完在命令行试试:
python --version # Windows
python3 --version # macOS
看到 3.11.x 就没问题。Mac 用户记得用 python3,系统自带的 Python 2.7 早该歇着了。
用 Micromamba 管理 Python 环境
直接用系统 Python 容易污染,我选了 Micromamba,安装快,不用像 anaconda 那样拖家带口。
安装 Micromamba
Windows(管理员权限的 cmd 或 PowerShell):
curl -Ls https://micro.mamba.pm/api/micromamba/win-64/latest | tar -xvj bin\micromamba.exe -O > micromamba.exe
micromamba shell hook -s cmd.exe | Out-String | Invoke-Expression
macOS(终端):
curl -Ls https://micro.mamba.pm/api/micromamba/osx-64/latest | tar -xvj bin/micromamba
./bin/micromamba shell hook -s zsh | source
如果用的是 bash,把 zsh 换成 bash。
完成之后试试:
micromamba --version
能输出版本号就装好了。如果找不到命令,要么是没重启终端,要么是初始化命令没生效,回去重新跑一遍。
如果嫌下载慢,可以配清华镜像:
mkdir -p ~/.mamba
echo "channels:
- defaults
- conda-forge
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
show_channel_urls: true" > ~/.mamba/mambarc

