项目场景
新建了一个 Python 虚拟环境,但在虚拟环境里面使用 pip
安装包时却遇到了错误。
使用命令 pip install requests
安装包时报错信息如下:
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
原因分析
因为在虚拟环境中找不到用户目录,所以不被允许使用 --user
参数来安装包的。但我安装的时候也没指定 --user
参数呀?难道 pip
的默认配置中指定了?
果不其然,当我打开 ~/.pip/pip.conf
文件时,看到了以下内容:
[global]
#index-url = https://pypi.douban.com/simple/
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url = https://mirrors.aliyun.com/pypi/simple/
disable-pip-version-check = true
timeout = 6000
upgrade = true
[install]
user = true
看最后一行,[install]
命令中默认设置了 user=true
,所有才会报上述错误。
将最后一行的 user=true
改为 user=false
即可解决。
https://github.com/microsoft/vscode-python/issues/14327