首页 > 解决方案 > 为什么virtualenv上的`pip install`会引发“无法执行'--user'安装”?

问题描述

e.g. airflow@my-worker-0:~$ which python

我在我的 Debian GNU/Linux 10 机器(Airflow 服务器)上(全局)安装了 Python3.8:

$ which python
>> /usr/local/bin/python

$ python --version
>> Python 3.8.10

$ which pip
>> /usr/local/bin/pip

以下是关于 pip 的配置:

$ pip config list
:env:.user='true'

$ pip config list -v
Skipping file '/etc/xdg/pip/pip.conf' (variant: global)
Skipping file '/etc/pip.conf' (variant: global)
For variant 'user', will try loading '/home/airflow/.pip/pip.conf'
For variant 'user', will try loading '/home/airflow/.config/pip/pip.conf'
Skipping file '/usr/local/pip.conf' (variant: site)
:env:.user='true'

一件奇怪的事情是conf上面的所有文件都不存在:

$ cat /home/airflow/.config/pip/pip.conf
>> bash: /home/airflow/.config/pip/pip.conf: No such file or directory
$ cat /etc/xdg/pip/pip.conf
>> cat: /etc/xdg/pip/pip.conf: No such file or directory
$ cat /etc/pip.conf
>> cat: /etc/pip.conf: No such file or directory
$ cat /home/airflow/.pip/pip.conf
>> cat: /home/airflow/.pip/pip.conf: No such file or directory
$ cat /home/airflow/.config/pip/pip.conf
>> cat: /home/airflow/.config/pip/pip.conf: No such file or directory
$ cat /usr/local/pip.conf
>> cat: /usr/local/pip.conf: No such file or directory

我创建virtualenv并尝试安装 python 包,但出现错误:

$ virtualenv /tmp/apache-beam-venv-wow --python=python3.8
>> created virtual environment CPython3.8.10.final.0-64 in 243ms
  creator CPython3Posix(dest=/tmp/apache-beam-venv-wow, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/airflow/.local/share/virtualenv)
    added seed packages: pip==21.1.3, setuptools==57.4.0, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

$ ls -alh /tmp/apache-beam-venv-wow/
>> total 12K
drwxr-xr-x 4 airflow airflow   64 Aug 16 01:49 .
drwxrwxrwt 1 root    root    4.0K Aug 16 02:24 ..
-rw-r--r-- 1 airflow airflow   40 Aug 16 01:03 .gitignore
drwxr-xr-x 2 airflow airflow  285 Aug 16 01:03 bin
drwxr-xr-x 3 airflow airflow   23 Aug 16 01:03 lib
-rw-r--r-- 1 airflow airflow  225 Aug 16 01:03 pyvenv.cfg

$ ls -alh /tmp/apache-beam-venv-wow/bin
>> total 56K
drwxr-xr-x 2 airflow airflow  285 Aug 16 01:03 .
drwxr-xr-x 4 airflow airflow   64 Aug 16 01:49 ..
-rw-r--r-- 1 airflow airflow 2.1K Aug 16 01:03 activate
-rw-r--r-- 1 airflow airflow 1.5K Aug 16 01:03 activate.csh
-rw-r--r-- 1 airflow airflow 3.0K Aug 16 01:03 activate.fish
-rw-r--r-- 1 airflow airflow 1.8K Aug 16 01:03 activate.ps1
-rw-r--r-- 1 airflow airflow 1.2K Aug 16 01:03 activate.xsh
-rw-r--r-- 1 airflow airflow 1.2K Aug 16 01:03 activate_this.py
-rwxr-xr-x 1 airflow airflow  245 Aug 16 01:03 pip
-rwxr-xr-x 1 airflow airflow  245 Aug 16 01:03 pip-3.8
-rwxr-xr-x 1 airflow airflow  245 Aug 16 01:03 pip3
-rwxr-xr-x 1 airflow airflow  245 Aug 16 01:03 pip3.8
lrwxrwxrwx 1 airflow airflow   21 Aug 16 01:03 python -> /usr/local/bin/python
lrwxrwxrwx 1 airflow airflow    6 Aug 16 01:03 python3 -> python
lrwxrwxrwx 1 airflow airflow    6 Aug 16 01:03 python3.8 -> python
-rwxr-xr-x 1 airflow airflow  232 Aug 16 01:03 wheel
-rwxr-xr-x 1 airflow airflow  232 Aug 16 01:03 wheel-3.8
-rwxr-xr-x 1 airflow airflow  232 Aug 16 01:03 wheel3
-rwxr-xr-x 1 airflow airflow  232 Aug 16 01:03 wheel3.8

$ /tmp/apache-beam-venv-wow/bin/pip install requests
>> ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the '/tmp/apache-beam-venv-wow/bin/python -m pip install --upgrade pip' command.

pip config也在这个 virtualenv 的 pip 上运行命令:

$ /tmp/apache-beam-venv-wow/bin/pip config list
>> :env:.user='true'

$ /tmp/apache-beam-venv-wow/bin/pip config list -v
>> Skipping file '/etc/xdg/pip/pip.conf' (variant: global)
Skipping file '/etc/pip.conf' (variant: global)
For variant 'user', will try loading '/home/airflow/.pip/pip.conf'
For variant 'user', will try loading '/home/airflow/.config/pip/pip.conf'
Skipping file '/tmp/apache-beam-venv-wow/pip.conf' (variant: site)
:env:.user='true'

我想知道为什么 virtualenv 的 pip 安装包使用--user选项以及如何防止这种情况。

我试过的:

$ /tmp/apache-beam-venv-wow/bin/pip config get :env:.user
true

$ /tmp/apache-beam-venv-wow/bin/pip config set ':env:.user' false
Writing to /home/airflow/.config/pip/pip.conf

$ cat /home/airflow/.config/pip/pip.conf
[:env:]
user = false

$ ./pip install requests
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the '/tmp/apache-beam-venv-wow/bin/python -m pip install --upgrade pip' command.

标签: pythonpipvirtualenv

解决方案


this:

$ /tmp/apache-beam-venv-wow/bin/pip config list
>> :env:.user='true'

is indicating you have PIP_USER=1 set in your environment (perhaps due to a .bashrc or /etc/environment file?)

unset that and it should stop forcing --user on your install commands (environment variables override the configuration files for pip)

unset PIP_USER

推荐阅读