# Python
# Flag
由于
Python
是动态编译的语言,和C
/C++
、Java
或者Kotlin
等静态语言不同,它是在运行时一句一句代码地边编译边执行的, 而Java
是提前将高级语言编译成了JVM
字节码,运行时直接通过JVM和机器打交道,所以进行密集计算时运行速度远高于动态编译语言。 但是根据二八定律
(帕累托法则),有80%
的计算资源只被20%
的程序所使用,因此在大部分情况下, 原生的CPython
解释器已经足够满足日常编程,加上很多科学计算库底层都是C
/C++
写的,一般不会用Python
做密集计算。
- https://github.com/topics/python (opens new window)
- https://github.com/python (opens new window)
- https://github.com/psf (opens new window)
- https://github.com/Python-World (opens new window)
- https://github.com/taichi-dev/taichi (opens new window)
- https://github.com/kennethreitz/pep8.org (opens new window)
- https://github.com/hhatto/autopep8 (opens new window)
- https://github.com/psf/black (opens new window)
- 代码格式化 https://github.com/astral-sh/ruff (opens new window)
- GO-Node-Python的简单性能比较 (opens new window)
- 内置函数 https://docs.python.org/zh-cn/3/library/functions.html (opens new window)
- 内置异常 https://docs.python.org/zh-cn/3/library/exceptions.html (opens new window)
- List Comprehensions (opens new window)
- 列表显示/列表解析 (opens new window)
- https://github.com/jackfrued/Python-100-Days (opens new window)
- 内置异常和函数-LeaCoder https://lichangke.github.io/category/#python (opens new window)
- Python语言小册 (opens new window)
- https://github.com/revolunet/PythonBooks (opens new window)
- https://github.com/robertparley/wtfpython-cn (opens new window)
- https://github.com/gto76/python-cheatsheet (opens new window)
- 教程 https://github.com/TwoWater/Python (opens new window)
- https://github.com/xxg1413/python (opens new window)
- https://github.com/yidao620c/python3-cookbook (opens new window)
- https://github.com/eastlakeside/interpy-zh (opens new window)
- https://github.com/cloga/scipy-lecture-notes_cn (opens new window)
- https://www.junmajinlong.com/python/index (opens new window)
- https://zmister.com (opens new window)
- https://github.com/iam-veeramalla/python-for-devops (opens new window)
- 基础教程 https://morvanzhou.github.io/tutorials/python-basic/basic (opens new window)
- https://github.com/luckrnx09/python-guide-for-javascript-engineers (opens new window)
- https://github.com/ProgrammingHero1 (opens new window)
- 设计模式和集合 https://github.com/faif/python-patterns (opens new window)
- https://hellopython.readthedocs.io (opens new window)
- https://github.com/Pierian-Data/Complete-Python-3-Bootcamp (opens new window)
- 流式数据处理 https://github.com/sandabuliu/python-stream (opens new window)
- https://github.com/Asabeneh/30-Days-Of-Python (opens new window)
- https://github.com/cycleuser/ThinkPython-CN (opens new window)
- FantixKing 的个人主页-掘金 (opens new window)
- Python中的GIL机制详解 (opens new window)
- 深挖python上下文管理——类的__init__、enter、exit、__del__在with下的表现 (opens new window)
- 深入理解 Python 中的上下文管理器 (opens new window)
test = Test() # 触发__del__
with Test() as t: # 触发__exit__
pass
工具
- 纯真IP数据库 https://github.com/animalize/qqwry-python3 (opens new window)
- 反编译 https://github.com/countercept/python-exe-unpacker (opens new window)
- 解密MS Office https://github.com/nolze/msoffcrypto-tool (opens new window)
- 网站脱机下载 https://github.com/xroche/httrack (opens new window)
- web文件扫描 https://github.com/topics/dirbuster (opens new window)
- https://github.com/H4ckForJob/dirmap (opens new window)
- https://github.com/DIGITALCRIMINAL/OnlyFans (opens new window)
- WiFi扫描破解 https://github.com/TheSadError/NIVOS (opens new window)
- CLI工具 https://github.com/kellyjonbrazil/jc (opens new window)
- https://github.com/astral-sh/ruff (opens new window)
- 数据提取 https://github.com/opendatalab/MinerU (opens new window)
# 三方编译器
PyPy
使用了JIT
(即时编译)技术,混合了动态编译和静态编译的特性,仍然是一句一句编译源代码, 但是会将翻译过的代码缓存起来以降低性能损耗。相对于静态编译代码,即时编译的代码可以处理延迟绑定并增强安全性。
- https://github.com/python/cpython (opens new window)
- https://www.pypy.org (opens new window)
- https://github.com/pyston/pyston (opens new window)
- https://github.com/stackless-dev/stackless (opens new window)
- https://github.com/jython/jython (opens new window)
- https://github.com/IronLanguages (opens new window)
- https://github.com/numpy (opens new window)
- https://github.com/faster-cpython (opens new window)
- https://github.com/cython/cython (opens new window)
- https://github.com/numba/numba (opens new window)
- https://github.com/Nuitka/Nuitka (opens new window)
- https://github.com/Immediate-Mode-UI/Nuklear (opens new window)
- https://github.com/pybind (opens new window)
- https://github.com/wlav/cppyy (opens new window)
- https://github.com/ethan-funny (opens new window)
# 安装新版
全部操作都在
root
用户下执行
# 安装编译相关工具
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel \
ncurses-devel sqlite-devel readline-devel tk-devel \
gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
# 下载安装包解压
- 到官网复制最新版下载地址 https://www.python.org/downloads/release
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
# 解压
tar -xvJf Python-3.7.3.tar.xz
# 切换到解压目录
cd Python-3.7.3
# 编译安装
# 创建编译安装目录,并配置指定安装路径
mkdir /usr/local/python3 && ./configure --prefix=/usr/local/python3
# 编译安装并把安装日志保存下来
make && make install > install.log
# 创建软连接
ln -s /usr/local/python3/bin/python3 /bin/python3
ln -s /usr/local/python3/bin/pip3 /bin/pip3
# 验证是否成功
python3 -V && pip3 -V
# 安装后yum不能正常使用
- 把
#!/usr/bin/python
修改为#!/usr/bin/python2
vi /usr/bin/yum
- 把
#!/usr/bin/python
修改为#!/usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
# pip
- https://github.com/pypa (opens new window)
- https://packaging.python.org/tutorials/installing-packages (opens new window)
- Python包管理工作流 (opens new window)
sudo apt install python3-pip
- Anaconda https://github.com/ContinuumIO (opens new window) 科学包及其依赖项的发行版本
- https://github.com/conda/conda (opens new window) 包及其依赖项和环境的管理工具
- https://github.com/jazzband/pip-tools (opens new window)
- https://github.com/python-poetry/poetry (opens new window)
- https://github.com/pyenv/pyenv (opens new window)
- https://github.com/danhper/asdf-python (opens new window)
- https://github.com/pdm-project/pdm (opens new window)
- https://bitbucket.org/virtualenvwrapper/virtualenvwrapper (opens new window)
- https://github.com/davidmarble/virtualenvwrapper-win (opens new window)
- https://github.com/python-poetry/poetry (opens new window)
- https://github.com/astral-sh/uv (opens new window)
# 镜像源
- 阿里云 https://mirrors.aliyun.com/pypi/simple (opens new window)
- 百度 https://mirror.baidu.com/pypi/simple (opens new window)
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple (opens new window)
- 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple (opens new window)
- 豆瓣(douban) http://pypi.douban.com/simple (opens new window)
- 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple (opens new window)
- 华中理工大学 http://pypi.hustunique.com (opens new window)
- 山东理工大学 http://pypi.sdutlinux.org (opens new window)
- 上海交通大学 https://mirrors.sjtug.sjtu.edu.cn/pypi (opens new window)
- 安装单个库
# 使用pip参数`-i 网址`,如果不是`https`协议网址需要加--trusted-host参数
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com requests
- 使用命令全局配置
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
- 编辑文件全局配置
# Linux环境
vi ~/.pip/pip.conf
# windows环境
%APPDATA%\Romaing\pip\pip.ini
# 添加或者修改
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
# 生成依赖管理文件
# 对于没有使用虚拟环境,会把系统所有的包都导出
pip freeze > requirements.txt
# pipreqs则只导出当前项目依赖库,如果文件存在可以使用--force覆盖,默认不会覆盖
pipreqs --force --encoding utf-8 ./
# 根据管理文件安装依赖
pip install -r requirements.txt
pip install --requirement requirements.txt
# 更新依赖库
更新pip
python -m pip install --upgrade pip setuptools
- 解决pip安装模块提示已经安装更高版本问题
pip3 install --ignore-installed 模块名
查看可更新的库
# --outdated
pip list -o
# format有两个选项:columns(有表头), freeze(无表头), json
pip list --outdated --format=columns
更新单个库
pip install --upgrade 要升级的包名
批量更新库
# linux 下升级所有 pip 过期包
pip install `pip list --outdate | sed -n '3,$p' | awk '{print $1}'` --upgrade
pip freeze --local | grep -v '^-e' | cut -d = -f 1 | xargs -n1 pip install -U
pip list -o --format=freeze | cut -d = -f 1 | xargs -n1 pip install -U
# 安装 pip-review
pip install pip-review
# 查看可更新的包
pip-review
# 自动更新所有包
pip-review --auto
# 更新包,提供操作可选项:[Y]es, [N]o, [A]ll, [Q]uit
pip-review --local --interactive
from subprocess import call
from pip._internal.utils.misc import get_installed_distributions
packages = [dist.project_name for dist in get_installed_distributions()]
call("pip install --upgrade" + ' '.join(packages), shell=True)
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade" + ' '.join(packages), shell=True)
# 卸载依赖库
pip uninstall 要卸载的包名