Skip to content

py - 安装&加速

如果项目里面没有pip

bash
$ pip
bash: pip: command not found
$ pip3
bash: pip3: command not found

$ sudo apt install -y python3-pip

加速镜像

常用镜像源:

临时用

bash
$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ 包名

长期配置

bash
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
Writing to /home/istr/.config/pip/pip.conf
$ pip config list
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple --extra-index-url https://pypi.org/simple requests

临时使用:
pip install --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple 安装的包名
或者
pip install -i https://repo.huaweicloud.com/simple/sqlalchemy

全局配置文件

ini
# Window 完整地址:C:\Users\%UserName%\pip\pip.ini
[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple
trusted-host = repo.huaweicloud.com
timeout = 120
bash
# 永久设置镜像源(Linux/Mac)
mkdir ~/.pip && \
cat <<EOF >> ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

cat <<EOF >  ~/.pip/pip.conf
[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple
trusted-host = repo.huaweicloud.com
timeout = 120
EOF

虚拟环境的使用

虚拟环境可以为每个项目创建独立的 Python 运行环境,避免不同项目之间的依赖冲突。可以使用 venv 模块创建虚拟环境。

venv

1.创建虚拟环境

bash
#使用系统默认的python
$ python3 -m venv myenv
#使用mac上指定的python版本
$ /Library/Frameworks/Python.framework/Versions/3.12/bin/python3 -m venv myenv
  • 其中 myenv 是虚拟环境的名称,可以根据需要修改。

  • 要指定用于创建虚拟环境的特定 Python 版本,我们需要在创建环境时使用完整的 Python 解释器路径

2.激活虚拟环境

bash
$ source myenv/bin/activate

激活后,终端的命令提示符会显示当前虚拟环境的名称。

3.在虚拟环境中安装依赖:

bash
$ pip install package_name
$ pip install -r requirements.txt

4.退出虚拟环境:

bash
$ deactivate

conda

bash
conda create -n name_test python=3.11
conda activate name_test
pip install -r -i

取消默认激活的base

conda config --set auto_activate_base false

pyenv https://github.com/pyenv/pyenv

https://geek-blogs.com/blog/upgrade-python-version-mac/

上次更新时间:

最近更新