1、安装python3.9.13
1.1、安装依赖
yum install -y zlib-devel bzip2-devel libffi-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install libffi-devel -y
1.2、下载python源码包
wget https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz
tar -zxvf Python-3.9.13.tgz
cd Python-3.9.13
./configure --prefix=/usr/local/python39
make&&make install
建立命令软链接
先查看python2和python3安装在哪
which python
which python3
ln -s /usr/local/python39/bin/python3 /usr/bin/python3
ln -s /usr/local/python39/bin/pip3 /usr/bin/pip3
查看版本号
python3 -V
2、安装pip
如果安装的python不带pip,可以通过以下命令安装
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py #下载安装脚本
python get-pip.py #运行安装脚本
2.1、更新pip
python.exe -m pip install --upgrade pip
2.2、pip可用的包安装源
如果觉得国外源慢,可以使用以下国内源
#阿里云
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/
# 设置默认源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
2.3、查看版本号
pip -V
2.4、安装模块
pip install package
# 指定版本和更新源安装
pip install -i https://mirrors.aliyun.com/pypi/simple/ numpy==1.23.2
2.5、列出已安装的包
pip list
2.6、卸载模块
pip uninstall package
2.7、显示指定安装包的信息
pip show package
2.8、检查包是否有更新
pip list --outdated
2.9、更新指定的包
pip install --upgrade package
2.10、更新所有包
pip-review --local --interactive
2.11、批量安装requirements.txt
通requirements.txt批量安装
为项目生成依赖包配置文件requirements.txt
pip freeze > requirements.txt
根据requirements.txt批量安装依赖包
pip install -r requirements.txt
2.12 依赖包导出和离线安装
1.依赖包导出
pip download -d ./download/packages -r requirements.txt
pip download 常用的参数有:
含义 | |
---|---|
-d 或 --dest | 指定依赖包的保存路径 |
-r 或 --requirement | 从文件(如:requirements .txt)中读取要下载的包信息 |
–no-deps | 不下载包的依赖项,只下载指定的包 |
–only-binary | 只下载二进制包,不下载源代码包 |
–platform | 指定目标平台(如:linux_x86_64) |
–python-version | 指定Python版本(如:36) |
–implementation | 指定Python实现(如:cp,pp,jp,ip等) |
–abi | 指定Python ABI(如:cp36m) |
2.依赖包离线安装
pip install --no-index --find-links=./download/packages -r requirements.txt
# –no-index: 表示不从网络索引安装依赖包;
# ./download/packages: 为依赖包安装的索引地址;
# requirements.txt: 为需要安装的依赖包信息。
3、安装conda
官网下载:https://www.anaconda.com/download/
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
3.1、创建环境
# 创建一个新的环境:
conda create -n python310 python=3.10
3.2、管理环境
## 激活环境:
conda activate python310
# 退出环境:
conda deactivate
# 列出所有环境:
conda env list
# 删除环境:
conda env remove --name python310
3.3、管理包
# 安装包:
conda install 包名
#安装特定版本的包:
conda install 包名=版本号
#升级包:
conda update 包名
#卸载包:
conda remove 包名
#搜索包:
conda search 包名
#搜索版本处于1.0.0及1.1之间的pandas
conda search "pandas [version='>=1.0.0,<1.1']"
3.4、管理包环境
# 导出环境到文件:
conda env export > 环境文件.yml
# 从环境文件创建环境:
conda env create -f 环境文件.yml
#克隆环境:
conda create --clone 源环境名称 --name 新环境名称
3.5、其他常用命令
#显示已安装的包列表:
conda list
# 显示包的详细信息:
conda info 包名
# 显示conda的版本信息:
conda --version
# 显示conda的帮助信息:
conda --help
3.6、conda下添加国内镜像源
# 1.配置清华镜像源
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 --set show_channel_urls yes
# 2.配置中科大镜像源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
# 3、配置上海交通大学镜像源
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge/
# 显示添加的镜像源
conda config --show channels
4、uv
官网:https://docs.astral.sh/uv/#learn-more
macOS and Linux
# Use curl to download the script and execute it with sh:
curl -LsSf https://astral.sh/uv/install.sh | sh
# If your system doesn't have curl, you can use wget:
wget -qO- https://astral.sh/uv/install.sh | sh
# Request a specific version by including it in the URL:
curl -LsSf https://astral.sh/uv/0.6.12/install.sh | sh
windows
# Use irm to download the script and execute it with iex:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# Request a specific version by including it in the URL:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/0.6.12/install.ps1 | iex"
使用 PyPI:
# With pip.
pip install uv
# Or pipx.
pipx install uv
如果通过独立安装程序安装,uv 可以自行更新到最新版本:
uv self update
4.1、创建新项目
# 使用uv init beta 创建一个新项目beta
uv init beta
uv init创建的文件外,还将创建一个虚拟环境,目前结构如下:
.
├── .venv
│ ├── bin
│ ├── lib
│ └── pyvenv.cfg
├── .python-version
├── README.md
├── main.py
├── pyproject.toml
└── uv.lock
4.2、安装、创建虚拟环境
# uv可以安装Python 版本,可同时安装多个
uv python install 3.10 3.11 3.12
# 创建虚拟环境,创建好后,系统会提示你使用下面的代码激活环境:source .venv/bin/activate
uv venv --python 3.8
4.3、uv 运行代码
安装项目所需的包,可以使用 uv add “包名”。如果pyproject.toml文件中已经有运行项目所需的相关依赖,只需要运行uv sync ,自动检索pyproject.toml中写好的依赖并进行安装。
uv run *.py
4.3、其它命令
uv init:#创建新的 Python 项目。
uv add:#向项目添加依赖项。
uv remove:#从项目中删除依赖项。
uv sync:#将项目的依赖项与环境同步。
uv lock:#为项目的依赖项创建 lockfile。
uv run:#在项目环境中执行命令。
uv tree:#查看项目的依赖关系树。
uv build:#将项目构建到分发存档中。
uv publish:#将项目发布到包索引。
uv tree: #展示安装的包的树状图。
# 查看可用的 Python 版本
uv python list
# 查找已安装的 Python 版本
uv python find