Skip to content

Commit afaafca

Browse files
committed
Migrate from miniconda to miniforge
1 parent caac7f8 commit afaafca

File tree

1 file changed

+70
-63
lines changed

1 file changed

+70
-63
lines changed

source/programming/python.md

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,111 @@
11
# Python 语言
22

33
- 本节贡献者: {{田冬冬}}(作者)、{{姚家园}}(审稿)
4-
- 最近更新日期: 2023-09-23
4+
- 最近更新日期: 2025-01-03
55
- 预计花费时间: 60 分钟
66

77
---
88

99
## 简介
1010

11-
[Python](https://www.python.org/) 是一种广泛使用的通用编程语言,其具有语法简单、
12-
功能强大等优点,是目前地震学科研最常用的编程语言之一。
11+
[Python](https://www.python.org/) 是一种广泛使用的通用编程语言,其具有语法简单、功能强大等优点,
12+
是目前地震学科研最常用的编程语言之一。
1313

14-
这一节中,我们不介绍具体的 Python 语法,而是着重介绍如何安装和管理 Python、
15-
如何编写并执行 Python 脚本等。这一节的最后列出了一些免费的 Python 学习资源,
16-
读者可根据需要自行学习。
14+
这一节中,我们不介绍具体的 Python 语法,而是着重介绍如何安装和管理 Python、如何编写并执行 Python 脚本
15+
等。这一节的最后列出了一些免费的 Python 学习资源,读者可根据需要自行学习。
1716

18-
## 安装 Miniconda
17+
## 安装 Miniforge
1918

20-
Python 是一种解释型语言,需要专门的解释器去执行 Python 代码。
21-
尽管 Linux/macOS 系统内置了 Python 解释器,但是建议用户不要使用它,
22-
以免误操作破坏系统内置 Python, 造成系统出现问题。
23-
**建议用户安装 Miniconda, 使用其提供的 `conda` 命令管理和安装 Python 及其模块。**
19+
Python 是一种解释型语言,需要专门的解释器去执行 Python 代码。尽管 Linux/macOS 系统内置了 Python
20+
解释器,但是建议用户不要使用它,以免误操作破坏系统内置 Python, 造成系统出现问题。
21+
**建议用户安装 Miniforge,使用其提供的 `conda` 命令管理和安装 Python 及其模块。**
2422

25-
:::{dropdown} Python、AnacondaMiniconda 的区别与联系
23+
:::{dropdown} Python、AnacondaMiniconda 与 Miniforge
2624
:color: info
2725
:icon: info
2826

29-
在安装和使用 Miniconda 前,读者有必要了解 Python、Anaconda 和 Miniconda 三者
30-
之间的区别与联系
27+
有一些 Python 使用经验的用户,可能接触或使用过 Anaconda、MinicondaMiniforge。这其中涉及到很多
28+
概念,下面做简要解释
3129

3230
Python 解释器
33-
: 从 [Python 官方网站](https://www.python.org/downloads/) 下载的 Python 安装包
34-
只提供了一个 Python 解释器,仅包含 Python 的核心模块和库,是运行 Python
35-
脚本所必需的。使用官方的 Python 安装包相当于安装了 Python 解释器 + 核心模块/库。
31+
: 从 [Python 官方网站](https://www.python.org/downloads/) 下载的 Python 安装包只提供了一个
32+
Python 解释器,仅包含 Python 的核心模块和库,是运行 Python脚本所必需的。使用官方的 Python 安装包
33+
相当于安装了 Python 解释器 + 核心模块/库。
3634

3735
Anaconda
38-
: [Anaconda](https://www.anaconda.com/) 是一个 Python **发行版**,不仅提供了
39-
Python 解释器,还内置了很多 Python 开发工具与众多科学计算相关的库,形成了
40-
一个可以开箱即用的 Python 科学计算环境,省去了自行配置科学计算环境的麻烦。
41-
Anaconda 还提供了强大的软件包管理工具 `conda`,可以方便地安装模块和管理环境。
42-
安装 Anaconda 相当于安装了 Python 解释器 + 核心模块/库 + 数百个科学计算相关模块 + 包管理器 `conda`
36+
: [Anaconda](https://www.anaconda.com/download)[Anaconda 公司](https://www.anaconda.com/)
37+
提供的一个 Python **发行版**,其不仅提供了 Python 解释器,还内置了很多 Python 开发工具与众多科学
38+
计算相关的库,形成了一个可以开箱即用的 Python 科学计算环境,省去了自行配置科学计算环境的麻烦。Anaconda
39+
还提供了强大的软件包管理工具 `conda`,可以方便地安装模块和管理环境。安装 Anaconda 相当于安装了
40+
Python 解释器 + 核心模块/库 + 数百个科学计算相关模块 + 包管理器 `conda`
4341

4442
尽管 Anaconda 有很多优点,其也有明显的缺点:
43+
4544
- 安装包非常大(超过 500 MB)
4645
- 安装过程耗时长(一般超过 5 分钟)
4746
- 安装后占用大量硬盘空间(一般超过 3 GB)
4847
- 安装了很多平时用不到的模块,进而导致安装新模块时会可能出现版本冲突
4948

5049
Miniconda
51-
: [Miniconda](https://docs.conda.io/en/latest/miniconda.html) 是 Anaconda 的精简版。
52-
它继承了 Anaconda 的优点,同时避免了 Anaconda 的臃肿。其安装包只有约 50 MB,安装
53-
通常也只需要数十秒。安装 Miniconda 相当于安装了 Python 解释器 + 核心模块/库 + 包管理器 `conda`
50+
: [Miniconda](https://docs.anaconda.com/miniconda/) 是 Anaconda 公司提供的 Anaconda 的精简版。
51+
安装 Miniconda 相当于安装了 Python 解释器 + 核心模块/库 + 包管理器 `conda`。因而,其避免了
52+
Anaconda 的臃肿,安装包只有约 50 MB,安装通常也只需要数十秒。由于内置了包管理器 `conda`,可以根据
53+
自己的需求安装软件包,极大地避免了内置大量软件包导致的软件版本冲突的问题。
54+
55+
conda-forge
56+
: Anaconda 公司在制作 Anaconda 发行版的同时,还维护了一个小型软件仓库,称之为 main channel。该软件
57+
仓库中仅包含了 Anaconda 公司感兴趣的几百个软件包,因而无法满足所有用户的需求。因而,一些有志之士成立了
58+
[conda-forge](https://conda-forge.org/) 社区,维护了另一个软件仓库,称之为 conda-forge channel。
59+
该软件仓库包含了近 3 万个软件包,以满足不同用户的需求。因而,大部分人通常都使用 conda-forge channel。
60+
但需要注意的是,某些软件包在 main 和 conda-forge channel 中同时存在,混用不同的 channel 可能会
61+
导致软件版本冲突。
62+
63+
Miniforge
64+
: 为了避免混用 main 和 conda-forge channel 可能导致的软件冲突,conda-forge 社区制作了 Miniforge
65+
发行版。Miniforge 与 Miniconda 基本相同,因而安装 Miniforge 相当于安装了 Python 解释器 +
66+
核心模块/库 + 包管理器 `conda`。不同的点在于,Miniforge 默认使用 conda-forge channel,即默认只
67+
从 conda-forge channel 中获取软件包,因而极大避免了混用 main 和 conda-forge channel 可能导致的
68+
冲突。因而,Miniforge 是目前我们推荐的 Python 发行版。
5469
:::
5570

56-
下面展示了如何在 Linux 系统下安装 Miniconda。其它操作系统下的安装说明以及具体使用方法可以参考
57-
[地震“学”软件中 Anaconda 相关内容](inv:software:anaconda/index)
71+
下面展示了如何在 Linux 系统下安装 Miniforge。其它操作系统下的安装说明以及具体使用方法可以参考
72+
[地震“学”软件中 conda 相关内容](inv:software:conda/index)
5873

59-
1. 下载 Miniconda
74+
1. 下载 Miniforge
6075

61-
下载地址:[Miniconda3-latest-Linux-x86_64.sh](https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh)
76+
下载地址:[Miniforge3-Linux-x86_64.sh](https://mirrors.tuna.tsinghua.edu.cn/github-release/conda-forge/miniforge/LatestRelease/Miniforge3-Linux-x86_64.sh)
6277

63-
2. 安装 Miniconda
78+
2. 安装 Miniforge
6479

6580
```
66-
$ bash Miniconda3-latest-Linux-x86_64.sh
81+
$ bash Miniforge3-Linux-x86_64.sh
6782
```
6883

69-
Miniconda 默认会安装到 {file}`${HOME}/miniconda3` 下,在安装过程中可以
70-
设置为其他路径。
84+
Miniforge 默认会安装到 {file}`${HOME}/miniforge3` 下,在安装过程中可以设置为其他路径。
7185

7286
安装通常只需要十几秒,在安装的最后会出现:
7387

74-
Do you wish the installer to initialize Miniconda3
75-
by running conda init? [yes|no]
76-
[no] >>>
88+
Do you wish to update your shell profile to automatically initialize conda?
89+
This will activate conda on startup and change the command prompt when activated.
90+
If you'd prefer that conda's base environment not be activated on startup,
91+
run the following command when conda is activated:
92+
93+
conda config --set auto_activate_base false
94+
95+
You can undo this by running `conda init --reverse $SHELL`? [yes|no]
96+
[no] >>>
7797

7898
输入 `yes` 则安装包会向当前 SHELL 的配置文件写入 `conda` 初始化语句。
7999

80100
3. 测试安装
81101

82-
打开一个新的终端,在终端中输入 `python`,输出中看到 **Anaconda, Inc.**
83-
字样即代表成功安装 Miniconda 并启动了 Python 解释器:
102+
打开一个新的终端,在终端中输入 `python`,输出中看到 `packaged by conda-forge` 字样即代表成功
103+
安装 Miniforge 并启动了 Python 解释器:
84104

85-
$ python
86-
Python 3.9.12 (main, Apr 5 2022, 01:53:17)
87-
[GCC 7.5.0] :: Anaconda, Inc. on linux
88-
Type "help", "copyright", "credits" or "license" for more information.
89-
>>>
105+
$ python
106+
Python 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:25:12) [Clang 18.1.8 ] on darwin
107+
Type "help", "copyright", "credits" or "license" for more information.
108+
>>>
90109
91110
在提示符 `>>>` 后输入 `quit()` 后按下 {kbd}`Enter` 键退出 Python 解释器。
92111

@@ -96,19 +115,16 @@ Miniconda
96115

97116
```
98117
$ python
99-
Python 3.9.12 (main, Apr 5 2022, 01:53:17)
100-
[GCC 7.5.0] :: Anaconda, Inc. on linux
118+
Python 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:25:12) [Clang 18.1.8 ] on darwin
101119
Type "help", "copyright", "credits" or "license" for more information.
102120
>>>
103121
```
104122

105-
进入 Python 解释器后,首先会显示 Python 版本信息、版权声明以及帮助信息,
106-
然后会显示符号 `>>>``>>>` 是 Python 解释器在交互模式下的主提示符,提示用户
107-
可以在 `>>>` 后输入 Python 指令。
123+
进入 Python 解释器后,首先会显示 Python 版本信息、版权声明以及帮助信息,然后会显示符号 `>>>``>>>`
124+
是 Python 解释器在交互模式下的主提示符,提示用户可以在 `>>>` 后输入 Python 指令。
108125

109-
`>>>` 提示符后输入 `1 + 2` 并按下 {kbd}`Enter` 键将指令 `1 + 2` 传给 Python
110-
解释器。解释器接收指令后会执行指令,输出结果 `3`,并再次显示主提示符
111-
`>>>` 等待用户的下一次输入。
126+
`>>>` 提示符后输入 `1 + 2` 并按下 {kbd}`Enter` 键将指令 `1 + 2` 传给 Python 解释器。解释器
127+
接收指令后会执行指令,输出结果 `3`,并再次显示主提示符 `>>>` 等待用户的下一次输入。
112128
```python
113129
>>> 1 + 2
114130
3
@@ -118,8 +134,8 @@ Type "help", "copyright", "credits" or "license" for more information.
118134
Python 解释器交互模式可以当做一个快捷的计算器来使用!
119135
:::
120136

121-
下面继续看一个稍复杂点的例子。给变量 `x` 赋值,然后紧跟着一个判断语句
122-
“如果 x 的值大于 0,则打印字符串 Hello world!”。
137+
下面继续看一个稍复杂点的例子。给变量 `x` 赋值,然后紧跟着一个判断语句“如果 x 的值大于 0,则打印字符串
138+
Hello world!”。
123139
```python
124140
>>> x = 5
125141
>>> if x > 0:
@@ -184,7 +200,7 @@ Python 解释器内置了所有标准库,安装解释器后就可以直接使
184200
Python 包,也可用于从源码安装 Python 包。
185201

186202
[`conda`](https://docs.conda.io/)
187-
: `conda`Anaconda/Miniconda 提供的包管理器,不仅可以安装 Python 包,
203+
: `conda`Miniforge 中内置的包管理器,不仅可以安装 Python 包,
188204
还可以安装其他语言写的包(理论上可以安装任何软件)。它的另一个重要功能是管理 Python 环境,
189205
可用于在一个系统内安装多个不同版本的 Python 解释器或包。
190206
:::
@@ -193,16 +209,7 @@ Python 解释器内置了所有标准库,安装解释器后就可以直接使
193209

194210
在使用 `conda` 前,还需要对 `conda` 做简单配置:
195211
```
196-
# 增加 conda-forge 通道,可以安装更多的软件包
197-
$ conda config --add channels conda-forge
198-
# 设置通道优先级为 strict,以避免混用 conda-forge 和 main
199-
$ conda config --set channel_priority true
200-
# 显示通道的 URL
201-
$ conda config --set show_channel_urls true
202-
# 设置 conda 使用更快的 libmamab solver
203-
$ conda config --set solver libmamba
204212
# 配置使用国内清华源以加快软件下载速度
205-
$ conda config --add default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
206213
$ conda config --set 'custom_channels.conda-forge' https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
207214
```
208215

0 commit comments

Comments
 (0)