【问题标题】:Virtualenv, pip-sync and "ERROR: Cannot uninstall"Virtualenv、pip-sync 和“错误:无法卸载”
【发布时间】:2022-01-18 07:33:58
【问题描述】:

我使用的是 Ubuntu 20.04 桌面。我不记得我是否必须在这里手动安装python3-pip(必须在其他 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

...不过话说回来,不应该是这样吗,如果我使用virtualenv,那么我就不会遇到这种问题了?至少,这是我从pip cannot uninstall <package>: "It is a distutils installed project" 得到的读数:

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

那么,我能做些什么,让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]

【问题讨论】:

  • don't sudo for pip installs;如果您想确保使用正确的 Python,请使用 /path/to/python -m pip。按照设计,安装运行来自setup.py 的任意 Python 代码(对于使用它的包)。此外,您不必安装 pip;它应该随 Python 发行版一起提供。
  • 谢谢,@KarlKnechtel - 我想我只有一个 Python3 (/usr/bin/python3),据我所知,venv_mpl/bin/python3 是指向那个的符号链接。我使用 sudo 的唯一原因是因为我想将那些 pip 软件包安装为“系统”软件包(类似于 apt install python3-matplotlib 本来的样子);到目前为止,我还没有找到比使用sudo 更好的说明如何做到这一点?无论如何,我仍然不知道我应该做什么来让这个 virtualenv 填充所需的 pip 包并运行?
  • 一般建议是“不要将软件包安装为系统软件包;有一些工具可以轻松地将软件包自动安装到新的虚拟环境中”。
  • 谢谢,@KarlKnechtel - 但这仍然不能解释我应该怎么做才能让它运行; “......有工具......” - 当然,哪些工具?说,我不做sudo -H pip3 install virtualenv - 那我应该如何运行 virtualenv,所以我创建虚拟环境?
  • 虚拟环境创建由venv标准库模块since 3.3提供。您再次不需要安装任何东西。

标签: python pip virtualenv


【解决方案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,我可以使用自 3.3 以来标准的 venv 模块 - 但是,我首先需要这样做(错误消息推荐 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 问题 - 太棒了!

不过,我觉得这有点奇怪,必须删除 pipdeptree 才能成功(编辑:事实证明,pipdeptree 也必须添加到 requirements.in,然后它会全部安装其他人在那里 - 奇怪的是 pip-tools 不需要这个,然后,pip-tools 是解析需求文件的人;可能相关:请注意,有时“以下包在需求文件中被认为是不安全的: " 被打印出来,这里有一些讨论 Reasoning behind unsafe packages · Issue #522 · jazzband/pip-tools · GitHub),但是嘿 - 至少程序通过了,并且安装了正确的包,所以......太棒了!

EDIT2:如果在尝试从该虚拟环境运行 matplotlib 脚本时,Python 会以“UserWarning: Matplotlib 当前正在使用 agg,这是一个非 GUI 后端,因此无法显示该图。”,需要安装系统sudo apt install python3-tk(见"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2021-07-30
    相关资源
    最近更新 更多