提问者:小点点

Virtualenv、pip-sync和“错误:无法卸载”


我在Ubuntu 20.04桌面上。我不记得我是否必须在这里手动安装python3pip(必须在其他Ubuntu机器上安装),但我有一个系统:

$ apt-show-versions -r python3-pip
python3-pip:all/focal-updates 20.0.2-5ubuntu1.6 uptodate

...然后:

sudo -H pip3 install --upgrade pip
sudo -H pip3 install pip-tools pipdeptree
sudo -H pip3 install virtualenv

然后,尝试从我的主目录设置virtualenv:

virtualenv venv_mpl

然后,我将以下内容放在venv_mpl/requirements.in中:

plotly
seaborn
scikit-learn

然后,我启用 virtualenv,并尝试安装软件包:

$ source venv_mpl/bin/activate
(venv_mpl) $ pip-compile venv_mpl/requirements.in
...
(venv_mpl) $ pip-sync venv_mpl/requirements.txt
Found existing installation: apturl 0.5.2
ERROR: Cannot uninstall 'apturl'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
Traceback (most recent call last):
...

嗯,是的,我以前确实安装了apturl:

$ apt-show-versions -r apt.*url
apturl:amd64/focal 0.5.2ubuntu19 uptodate
apturl-common:amd64/focal 0.5.2ubuntu19 uptodate

…但话说回来,不应该是这样的吗,如果我使用虚拟环境,那么我就不会有这种问题了?至少,这是我从pip获得的阅读无法卸载

或者,根据您的需要,不使用您的系统Python和/或其全局环境而是创建私有Python安装和/或环境可能会更有效率。这里有许多选项,包括虚拟环境

那么,我能做些什么,将 pip-compile 从上述 requirements.in 收集的 pip 包安装在如上所述创建的 virtualenv 中?

编辑:如果我尝试删除系统apturl,它还会尝试删除一个关键的桌面组件,在本例中为ubuntu-mate-core

$ sudo apt remove --dry-run apturl
[sudo] password for administrator: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  apturl ubuntu-mate-core
0 upgraded, 0 newly installed, 2 to remove and 21 not upgraded.
Remv ubuntu-mate-core [1.263]
Remv apturl [0.5.2ubuntu19]

共1个答案

匿名用户

正如@KarlKnechtel所指出的,sudo不应与pip一起使用,所以我尝试这样做-首先,恢复以前的状态(根据OP中所做的更改):

# answer Y to all questions at next command
sudo -H pip3 uninstall virtualenv pip-tools pipdeptree
rm -rf venv_mpl

现在,由于这目前是Python 3.8.10,我可以使用venv模块,这是3.3以来的标准-但是,我首先需要这样做(错误消息推荐python3.8-venv,但python3-venv可能更好):

sudo apt install python3-venv

...然后,我可以创建virtualenv:

python3 -m venv venv_mpl

现在,让我们在venv中继续操作,以普通用户的身份运行命令:

$ source venv_mpl/bin/activate

(venv_mpl) $ pip3 install --upgrade pip
...
Successfully installed pip-21.3.1

(venv_mpl) $ pip3 install pip-tools pipdeptree
Collecting pip-tools
...
Successfully installed click-8.0.3 pep517-0.12.0 pip-tools-6.4.0 pipdeptree-2.2.0 tomli-2.0.0 wheel-0.37.0

(venv_mpl) $ cat > venv_mpl/requirements.in <<'EOF'
plotly
seaborn
scikit-learn
EOF

(venv_mpl) $ pip-compile venv_mpl/requirements.in
...

(venv_mpl) $ pip-sync venv_mpl/requirements.txt
Found existing installation: pipdeptree 2.2.0
Uninstalling pipdeptree-2.2.0:
  Successfully uninstalled pipdeptree-2.2.0
Collecting cycler==0.11.0
  Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools==4.28.3
  Using cached fonttools-4.28.3-py3-none-any.whl (884 kB)
Collecting joblib==1.1.0
  Using cached joblib-1.1.0-py2.py3-none-any.whl (306 kB)
Collecting kiwisolver==1.3.2
  Using cached kiwisolver-1.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.2 MB)
Collecting matplotlib==3.5.1
  Using cached matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB)
Collecting numpy==1.21.4
  Using cached numpy-1.21.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)
Collecting packaging==21.3
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pandas==1.3.5
  Using cached pandas-1.3.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.5 MB)
Collecting pillow==8.4.0
  Using cached Pillow-8.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)
Collecting plotly==5.4.0
  Using cached plotly-5.4.0-py2.py3-none-any.whl (25.3 MB)
Collecting pyparsing==3.0.6
  Using cached pyparsing-3.0.6-py3-none-any.whl (97 kB)
Collecting python-dateutil==2.8.2
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting pytz==2021.3
  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting scikit-learn==1.0.1
  Using cached scikit_learn-1.0.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (25.9 MB)
Collecting scipy==1.7.3
  Using cached scipy-1.7.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (39.3 MB)
Collecting seaborn==0.11.2
  Using cached seaborn-0.11.2-py3-none-any.whl (292 kB)
Collecting six==1.16.0
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting tenacity==8.0.1
  Using cached tenacity-8.0.1-py3-none-any.whl (24 kB)
Collecting threadpoolctl==3.0.0
  Using cached threadpoolctl-3.0.0-py3-none-any.whl (14 kB)
Installing collected packages: six, pyparsing, pytz, python-dateutil, pillow, packaging, numpy, kiwisolver, fonttools, cycler, threadpoolctl, tenacity, scipy, pandas, matplotlib, joblib, seaborn, scikit-learn, plotly
Successfully installed cycler-0.11.0 fonttools-4.28.3 joblib-1.1.0 kiwisolver-1.3.2 matplotlib-3.5.1 numpy-1.21.4 packaging-21.3 pandas-1.3.5 pillow-8.4.0 plotly-5.4.0 pyparsing-3.0.6 python-dateutil-2.8.2 pytz-2021.3 scikit-learn-1.0.1 scipy-1.7.3 seaborn-0.11.2 six-1.16.0 tenacity-8.0.1 threadpoolctl-3.0.0

好吧,它在这里确实成功了,没有遇到apturl问题——太棒了!

不过,我觉得有点奇怪的是,< code>pipdeptree必须被删除才能成功(编辑:结果是,< code>pipdeptree也必须被添加到< code>requirements.in中,然后它就像所有其他的一样被安装在那里——奇怪的是< code>pip-tools不需要它,然后,< code>pip-tools是解析需求文件的那个;可能相关:请注意,有时“在需求文件中,下列软件包被认为是不安全的:”被打印出来,这里有一些关于不安全软件包背后的原因的讨论...太好了!

EDIT2:如果尝试从这个虚拟环境运行 matplotlib 脚本时,Python 响应“UserWarning:Matplotlib 当前正在使用 agg,这是一个非 GUI 后端,因此无法显示图形”,则需要安装系统 sudo apt install python3-tk(请参阅“用户警告:Matplotlib 当前正在使用 agg,这是一个非 GUI 后端,因此无法显示图形。