py - 安装&加速
如果项目里面没有pip
bash
$ pip
bash: pip: command not found
$ pip3
bash: pip3: command not found
$ sudo apt install -y python3-pip加速镜像
常用镜像源:
- 清华:
https://pypi.tuna.tsinghua.edu.cn/simple/ - 阿里云:
https://mirrors.aliyun.com/pypi/simple/ - 华为云:
https://repo.huaweicloud.com/repository/pypi/simple/ - 豆瓣:
http://pypi.douban.com/simple/ - 中科大:
https://pypi.mirrors.ustc.edu.cn/simple/ - https://mirrors.huaweicloud.com/mirrorDetail/5ea148ce302e67c59c8fe161
临时用
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 listpip 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 = 120bash
# 永久设置镜像源(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.txt4.退出虚拟环境:
bash
$ deactivateconda
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