Python 安装与使用

Python

下载(Windows x86-64 embeddable zip file)安装:https://www.python.org/downloads/windows & https://www.python.org/ftp/python & https://registry.npmmirror.com/binary.html?path=python & https://pythonlang.cn/downloads

 

包管理

一、pip

环境变量

# python.exe
python-x.x.x
# pip.exe
python-x.x.x\Scripts
# pip install xxx 后的 xxx.exe
python-x.x.x\Lib\site-packages\bin

下载 get-pip.py:https://bootstrap.pypa.io/get-pip.py & https://pip.pypa.io/en/stable/installation

curl -LOJ https://bootstrap.pypa.io/get-pip.py
curl -LOJ https://raw.githubusercontent.com/pypa/get-pip/main/public/get-pip.py
curl -LOJ https://fastly.jsdelivr.net/gh/pypa/get-pip@main/public/get-pip.py
curl -LOJ https://ghproxy.net/https://raw.githubusercontent.com/pypa/get-pip/main/public/get-pip.py

安装 pip

# 修改 pythonXX._pth 文件,去掉 #import site 前的 # 号。或直接删除该文件
# 安装 pip,先设置 pip 源安装会快一点
python get-pip.py
# 后续升级 pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U --trusted-host pypi.tuna.tsinghua.edu.cn
# 验证是否安装成功
python -m pip install --help

文件方式修改 pip 源:Windows 在用户目录下新建 pip\pip.ini 文件(也可以在 python.exe 同级目录创建 pip.ini),如:C:\Users\Administrator\pip\pip.ini。Linux 在用户目录下新建 ~/.pip/pip.conf 文件

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install] 
trusted-host = pypi.tuna.tsinghua.edu.cn

命令方式修改 pip 源

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 如果提示源不受信任,就设置这个
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

其它 pip 镜像源:

https://mirrors.cernet.edu.cn/list/pypi
阿里云:https://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban):https://pypi.douban.com/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple/

包查询:https://pypi.org & https://pypi.ac.cn https://pypi.com.cn

包依赖查询:https://pypi.org/pypi/requests/2.31.0/json,查看 info 字段下的 requires_dist 列表

查看 pip 安装位置(默认从下往上的路径搜索)

python -m site
python -m site -help

USER_BASE python.exe 启动程序路径
USER_SITE 依赖安装包基础路径

python -m pip -v config list

For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\Administrator\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\Administrator\AppData\Roaming\pip\pip.ini'
For variant 'site', will try loading 'D:\PcAPP\Python\pip.ini'
global.index-url='https://mirrors.aliyun.com/pypi/simple/'
global.target='D:\\PcAPP\\Python\\Lib\\site-packages'
install.trusted-host='mirrors.aliyun.com'

更改 pip 安装位置

[global]
target = C:\python-x.x.x\Lib\site-packages

更改 pip 缓存位置

pip config set global.cache-dir "D:\MyFile\pip"

相当于
[global]
cache-dir = D:\MyFile\pip

常用 pip 命令

pip -v config list
pip -v config debug
pip cache purge
pip freeze > requirements.txt
pip install -e .
pip install numpy
pip uninstall numpy
pip install numpy==1.16.0
pip install -U --force-reinstall --pre numpy
pip install --upgrade pip

二、pipx

https://pipx.pypa.io/latest/installation/

 

环境隔离

一、venv

https://docs.python.org/3/tutorial/venv.html & https://docs.python.org/3/library/venv.html & https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/

二、conda

https://www.anaconda.com/docs/getting-started/miniconda/installhttps://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ & https://mirrors.ustc.edu.cn/help/anaconda.html

# 查看
conda info --env
conda env list
# 创建
conda create -n labelme python=3.12 -y
# 切换虚拟环境
conda activate labelme
# 切换 python 版本
conda install python=3.13
# 删除
conda activate base
conda remove -n labelme --all -y
# 更新
conda update --all

Anaconda 环境变量

Anaconda3
Anaconda3\Scripts
Anaconda3\Library\bin

启动(Cmd 快捷方式)

# Anaconda Prompt
%WINDIR%\System32\cmd.exe "/K" C:\ProgramData\miniconda3\Scripts\activate.bat C:\ProgramData\miniconda3
# ico
%ALLUSERSPROFILE%\miniconda3\Menu\anaconda_prompt.ico

# Anaconda PowerShell Prompt
%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\ProgramData\miniconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\ProgramData\miniconda3' "
# ico
%ALLUSERSPROFILE%\miniconda3\Menu\anaconda_powershell_prompt.ico

# 起始位置
C:\Users\Administrator
%HOMEPATH%

三、uv

https://docs.astral.sh/uv/getting-started/installation/ & https://blog.csdn.net/AlienProgrammer/article/details/149743804 & https://astral.ac.cn

环境变量:https://docs.astral.sh/uv/reference/environment/

:: setx UV_CACHE_DIR "D:\Development\uv\cache"
setx UV_INSTALL_DIR "D:\Development\uv\bin"
setx UV_PYTHON_BIN_DIR "D:\Development\uv\bin"
setx UV_PYTHON_CACHE_DIR "D:\Development\uv\cache"
setx UV_PYTHON_INSTALL_DIR "D:\Development\uv\python"
setx UV_TOOL_BIN_DIR "D:\Development\uv\bin"
setx UV_TOOL_DIR "D:\Development\uv\tool"
setx UV_CONFIG_FILE "D:\Development\uv\uv.toml"

:: setx PATH "D:\Development\uv\bin;%PATH%;D:\Development\uv"

uv cache dir
uv python dir
uv python dir
uv tool dir
uv tool dir --bin

cmd

默认全局配置路径:%SYSTEMDRIVE%\ProgramData\uv\uv.toml,设置:https://docs.astral.sh/uv/reference/settings/

cache-dir = "D:/Development/uv/cache"
#index-url = "https://mirrors.aliyun.com/pypi/simple"
index-url = "https://pypi.tuna.tsinghua.edu.cn/simple"
# https://mirrors.cernet.edu.cn/list/python-build-standalone
python-install-mirror = "https://registry.npmmirror.com/-/binary/python-build-standalone"
#python-install-mirror = "https://mirror.nju.edu.cn/github-release/indygreg/python-build-standalone"
#python-install-mirror = "https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone"
#python-install-mirror = "https://mirrors.lzu.edu.cn/github-release/astral-sh/python-build-standalone/"
#python-install-mirror = "https://ghfast.top/https://github.com/astral-sh/python-build-standalone/releases/download"
#python-install-mirror = "https://gh-proxy.com/https://github.com/astral-sh/python-build-standalone/releases/download"
#python-install-mirror = "https://gitproxy.click/https://github.com/indygreg/python-build-standalone/releases/download"
[pip]
index-url = "https://pypi.tuna.tsinghua.edu.cn/simple"

使用

uv venv
.\.venv\Scripts\activate.bat
uv pip install -e .
deactivate.bat

uv add -r requirements.txt
uv run label-studio-ml create my_ml_backend
uv run python -m label-studio-ml create my_ml_backend
uv run -- label-studio-ml create my_ml_backend

uv sync --verbose
uv run --verbose label-studio start -b --port 80 --data-dir data --username l@l.ll --password l
uvx --verbose --python 3.12 label-studio start -b --port 80 --data-dir D:\label-studio --username l@l.ll --password l

uv python pin 3.12

:: Clear the cache, removing all entries or those linked to specific packages
uv cache clean
:: Prune all unreachable objects from the cache
uv cache prune

 


https://mirrors.tuna.tsinghua.edu.cn/help/pypi

https://repo.anaconda.com/miniconda & https://mirror.tuna.tsinghua.edu.cn/help/anaconda

posted @ 2019-09-12 11:54  江湖小小白  阅读(696)  评论(0)    收藏  举报