使用 conda 管理电脑多个 python 版本
背景
之前一直使用 python 自带的虚拟环境管理工具(virtualen
包),虽然很舒服,可以有不同的软件包环境,但是所有环境都只能基于一个 python 版本。由于历史原因,系统(Ubuntu)升级时给我新增了一个 python3.11,我索性就只保留了这一个版本。
这两天要使用 open3d,结果发现不支持最新版本的 python3.11…… 有两个解决方法:
- 使用 conda 管理 python 环境
- 再安装一个低版本的 python,
virtualen
创建虚拟环境时指定低版本 python
纠结了一番后最终我还是投入了 conda 的怀抱!虽然安装有点麻烦,但是使用起来一样简单。
安装过程(Linux)
安装过程中有一些选项和设置,这里记录我最终决定的安装方法,大体依照官方教程:
依照教程下载安装脚本,bash 执行
安装程序大致执行以下步骤
-
license 确认:空格继续阅读,q 退出,然后确认
Do you accept the license terms? [yes|no]
-
安装位置确认:这里存储所有的环境和软件包(大概)
Miniconda3 will now be installed into this location:
/home/thor/miniconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
-
conda init 确认:这里确认,会对 bash 的
~/.bashrc
进行配置(保证进入终端可以使用 conda 命令)Do you wish the installer to initialize Miniconda3 by running conda init?
如果经常使用 zsh,可以运行
conda init zsh
,会对应修改~/.zshrc
如果想要再谨慎一点,可以查看 conda init 的文档,提供了 verbose 选项和撤销修改的 reverse 选项
conda init — conda 23.7.3 documentation
- modifies the shell's configuration files to add the necessary environment variables for Conda to work properly. These environment variables include paths to Conda's executables and libraries.
- sets up the necessary shell-level commands, such as
conda activate
andconda deactivate
, which affect the state of the shell and allow you to activate and deactivate Conda environments.
-
安装完毕后每次打开终端,都会默认进入 base 环境,这个环境里的 python 是 conda 内部的,之前系统安装的 python 会被屏蔽掉。我希望使用到 base 时再手动 activate base,故再执行一下:
conda config --set auto_activate_base false
新环境的创建示例
$ conda create -n 3d python=3.10 # 创建新环境 $ conda info --envs # 查看目前的环境 下载 # conda environments: # base /home/thor/miniconda3 3d /home/thor/miniconda3/envs/3d $ conda activate 3d 下载 (3d) $ pip install open3d (3d) $ conda install jupyter notebook
注意📢
不要使用 pip 安装 jupyter 环境,使用有点问题…… [1],原因大概是 jupyter 安装的时候,会在诸如 ~/.local/bin
下面添加可执行文件,文件开头是 #!/bin/python3
但是这就导致默认使用的不是 conda 环境中的 jupyter, 必须使用 python3 -m jupyter notebook
才能正常打开。
此外,根据 jupyter 官方网站[2],conda 推荐用 the conda-forge channel 安装。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!