Python常用操作

Conda 安装教程

https://www.jianshu.com/p/edaa744ea47d

查看当前的源

1
conda config --show channels

修改为清华源

1
2
3
4
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/

阿里源:

Conda 配置阿里源 - 码农教程

conda 常用命令

conda 下多版本 Python 切换和配置 - 简书

创建虚拟环境

Python 中的虚拟环境指的是一个独立的 Python 运行环境,它与系统中全局安装的 Python 环境相互隔离,并且可以在其中安装和使用特定版本的 Python 解释器、库和工具。虚拟环境可以被认为是一种“沙箱”,在其中进行开发和测试时,不会对全局 Python 环境造成影响,而且可以根据需要创建多个不同的虚拟环境。

这种沙箱方式可以帮助用户避免不同项目之间依赖冲突的问题,并且能够更好地管理项目依赖,例如可以随意添加、更新或删除所需的库和工具,不受全局 Python 环境中其他项目的干扰。另外,虚拟环境也方便了代码分享和迁移,因为项目的依赖可以被包含到虚拟环境中,从而保证了其他人能够快速搭建和运行项目。

以安装 Jupyter

1
2
3
4
5
6
7
8
9
10
11
12
# 查看
conda info --env
# conda新建一个环境(如无需指定python版本,则不加python这个参数)
conda create -n jupyter python=3.11 -y
# 激活jupyter环境
conda activate jupyter
# 安装jupyter
conda install jupyter -y
# 退出环境
conda deactivate
# 删除虚拟环境
conda remove -n jupyter --all

删除虚拟环境

包相关的信息

包仓库

https://pypi.org/

查看源

修改源

注意: 新版 ubuntu 要求使用 https 源。
下面是一些常用的 pip 源:
清华: https://pypi.tuna.tsinghua.edu.cn/simple
中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学: http://pypi.hustunique.com/
豆瓣: http://pypi.douban.com/simple/
阿里云: http://mirrors.aliyun.com/pypi/simple/

1
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/

临时设置:

在使用 pip 安装包时,加上 -i 参数指定安装源即可,例如:

pip install -i https://pypi.python.org/simple numpy #这条命令将使用阿里云 PyPI 镜像安装 numpy 包

安装单个包

1
pip install opencv-python

批量安装包

1
pip install -r requirements.txt

查看包是否已安装

1
pip show --files 安装包名

查看过期的包

1
pip list --outdated

升级包

1
pip install --upgrade 要升级的包名

路径

python 中的模块、库、包有什么区别?

https://www.zhihu.com/question/30082392/answer/2030353759

Python 里面存在这样的关系:
数据可以封装在容器(列表、元组、字符串、字典)里面;

代码可以封装在 function 里面;

function 和数据可以封装在 class 里面(或者说方法和属性可以封装在类里面);

上述三类内容可以打包在 module(模块)里面;

多个 module 可以打包在 package(包)里面;

多个 package 可以打包在 library(库)里面。

在子目录执行脚本时的 import 方式

Relative imports in Python 3 - Stack Overflow

(重要)代理

诸如 V2RayN 的系统代理,默认是不会在 Python 代码环境生效的。

有如下几种方法可以给你的 Python 代码设置运行时的代理。

设置系统代理

这种方式最简单粗暴。

1
2
3
4
5
6
7
import os

# 设置HTTP代理
os.environ['http_proxy'] = 'http://127.0.0.1:10809'

# 设置HTTPS代理
os.environ['https_proxy'] = 'http://127.0.0.1:10809'

设置 Http 代理

注意,http 和 https 的协议要确认好,和你请求的接口相匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import urllib.parse
import urllib.request

# 设置代理信息(如果需要)
PROXY_IP = '127.0.0.1'
PROXY_PORT = 10809
# 这里的http和https的协议要确认好,和你请求的接口相匹配
proxy = urllib.request.ProxyHandler({'https': f'http://{PROXY_IP}:{PROXY_PORT}'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)

params = {
'part': 'snippet,contentDetails,statistics',
'id': video_id,
}
query_string = urllib.parse.urlencode(params)
url = f'{base_url}?{query_string}'
req = urllib.request.Request(url)
with urllib.request.urlopen(req, timeout=10) as response:
data = response.read()
print(data.decode('utf-8'))

这种方式,有时候遇到你用的第三方 http 包有问题的话,会比较蛋疼。

常用代码片段

随机取数组中的一个元素

1
2
3
from random import choice
foo = ['a', 'b', 'c', 'd', 'e']
print choice(foo)

打印模型参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
parameters = list(model.parameters())
print("parameters",parameters)

# 参数数量
model.num_parameters()

# 配置信息,其中包括每一层的神经元数量、注意力头数、隐藏层大小等。通过遍历每一层的配置信息,可以计算出整个模型的神经元数量。
model.config

from transformers import AutoModel

model = AutoModel.from_pretrained("模型名称")

# 模型的总层数(包括 encoder 和 decoder 层)。
num_layers = model.config.num_hidden_layers
print("模型层数:", num_layers)

编码规范

PEP8

PEP 8 – Style Guide for Python Code | peps.python.org

Google Python Style Guide

styleguide | Style guides for Google-originated open-source projects

在 PEP8 之上做了更多的说明,可以作为 PEP8 的补充。

VSCode 插件

Pylint

autopep8

常见问题

conda 创建的虚拟环境中,无法 pip 安装包

1
Could not fetch URL https://pypi.mirrors.ustc.edu.cn/simple/gym/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.mirrors.ustc.edu.cn', port=443): Max retries exceeded with url: /simple/gym/ (Caused by SSLError(SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF) (_ssl.c:1131)'))) - skipping

Can't connect to HTTPS URL because the SSL module is not available · Issue #8273 · conda/conda · GitHub

代码里面导入已安装的包,出现包找不到的红色提示

为什么我已经安装了第三方包:

1
pip install youtube-transcript-api

但是代码里面导入的时候:

1
from youtube_transcript_api import YouTubeTranscriptApi

还是会出现红线,并提升包找不到:

1
2
Unable to import 'youtube_transcript_api'PylintE0401:import-error
Import "youtube_transcript_api" could not be resolvedPylancereportMissingImports

python json.dump()中文变成 Unicode 字符串了

json.dump()  默认会将中文字符串编码成 Unicode 的转义序列,也就是将中文字符转换为  \uXXXX  的形式,其中  XXXX  是该字符的 Unicode 码点的十六进制表示。这是为了确保 JSON 字符串可以被正确地解析和处理,因为 JSON 规范要求所有非 ASCII 字符必须经过转义才能在字符串中表示。

如果你想要将中文字符保留为原始的 UTF-8 编码,可以在调用  json.dump()  函数时指定  ensure_ascii=False  参数,例如:

1
2
3
4
5
import json

data = {"name": "张三", "age": 18}
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)

这样就可以将中文字符以原始的 UTF-8 编码保存到 JSON 文件中了。

参考资料

项目模板:

https://github.com/gaogaotiantian/viztracer