Python 经验汇编

1. 虚拟环境安装

参考 Python教程 - 廖雪峰。

注意找到正确的 Python 执行码版本,常见目录包括:

  • Conda安装:/usr/local/anaconda3/
  • 操作系统内置Python2:/System/Library/Frameworks/Python.framework/Versions/
  • 官方Python3:/Library/Frameworks/Python.framework/Versions/

在当前的.venv子目录,安装虚拟环境

1
python3 -m venv .venv

安装结果如下:

1
2
3
4
5
6
7
sj@JiandeiMac signal-test % ls -l .venv
total 16
drwxr-xr-x 33 sj staff 1056 9 21 23:56 bin
drwxr-xr-x 3 sj staff 96 9 21 22:50 include
drwxr-xr-x 3 sj staff 96 9 21 22:50 lib
-rw-r--r-- 1 sj staff 224 9 21 22:50 pyvenv.cfg
drwxr-xr-x 3 sj staff 96 9 21 22:53 share

激活虚拟环境

1
source .venv/bin/activate

显示当前已安装的软件包,默认只有pip,很干净!!!

1
2
3
4
(.venv) sj@JiandeiMac aaa % pip3 list
Package Version
------- -------
pip 24.0

2. 设置国内安装源

一次性的替换源

1
pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

永久的替换源

查看pip的配置信息, pip3 config list,但是更好的是pip3 config debug

1
2
3
4
5
6
7
8
9
10
11
12
(.venv) sj@JiandeiMac aaa % pip3 config debug
env_var:
env:
global:
/Library/Application Support/pip/pip.conf, exists: False
site:
/Users/sj/MyProject/aaa/.venv/pip.conf, exists: False
user:
/Users/sj/.pip/pip.conf, exists: False
/Users/sj/.config/pip/pip.conf, exists: True
global.index-url: http://mirrors.aliyun.com/pypi/simple/
install.trusted-host: mirrors.aliyun.com

配置文件名为pip.conf,路径依次为:系统安装目录—当前虚拟环境—当前用户归属目录

以/Users/sj/.config/pip/pip.conf为例,也可以用于.venv/pip.conf,其内容为:

1
2
3
4
5
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host = mirrors.aliyun.com

也可以使用命令行方式,但要注意实际修改了哪个配置文件:

1
2
pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple/
pip3 config set install.trusted-host mirrors.aliyun.com

多个源的设置方法

注意,index-url字段只能有一个地址,其他在extra-index-url字段

1
2
3
4
5
6
7
8
9
10
11
12
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url=
https://pypi.mirrors.ustc.edu.cn/simple/
https://pypi.douban.com/simple/
https://pypi.python.org/simple/

[install]
trusted-host = tuna.tsinghua.edu.cn
pypi.douban.com
mirrors.ustc.edu.cn
python.org

参考文献

库函数