使用pip升级最初通过apt安装的软件包

时间:2019-08-06 16:05:09

标签: python linux pip debian apt

我想通过docutils安装pip的最新版本,但是它不知道如何升级通过apt安装的系统版本。

$ sudo --set-home python2 -m pip install --upgrade docutils
Collecting docutils
  Using cached https://files.pythonhosted.org/packages/3a/dc/bf2b15d1fa15a6f7a9e77a61b74ecbbae7258558fcda8ffc9a6638a6b327/docutils-0.15.2-py2-none-any.whl
Installing collected packages: docutils
  Found existing installation: docutils 0.14
ERROR: Cannot uninstall 'docutils'. 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.

$ apt-cache show python-docutils | head -n 3
Package: python-docutils
Architecture: all
Version: 0.14+dfsg-3

我在网络上没有想到或找到的解决方案:

  1. 使用apt删除rm -rf /usr/lib/python2.7/dist-packages/docutils*版本。这会使pip保持沉默,但意味着:

    • 系统上已安装的文件不再符合Debian打包系统的想法
    • 我可能会破坏对docutils 0.14的系统软件依赖性
    • 对Debian软件包的任何更新都会导致apt重新安装
    • this answer中讨论的其他问题
  2. pip install --force-reinstall。 (同样的问题。)

  3. pip install --ignore-install。 (同样的问题。)

是否有办法从pip中获得适用于我的最新版本的默认环境,但没有机会破坏任何系统软件? The same answer above建议使用virtualenvvenvpyenvpipenv中的一个。我尝试了pipenv,但它不想使用--system安装命令行中列出的单个软件包,而且我不知道创建Pipfile是否会真正解决此问题。

我宁愿不必以某种方式手动切换环境,以使用apt安装的软件包而不是pip安装的软件包。有没有办法只让安装了apt的软件来使用一个环境,而不能使该环境与安装了pip的东西一起使用?

2 个答案:

答案 0 :(得分:2)

  

我宁愿不必以某种方式手动切换环境以使用apt安装的软件包而不是pip安装的软件包。有没有办法让仅apt安装的软件使用一个环境,否则将环境与pip安装的东西一起使用?

理想情况下,应该使用系统版本点子版本。

根据Debian Python Policy

  

只要您未在路径中安装其他版本的Python,Debian的Python版本就不会受到新版本的影响。

     

如果您安装了已安装的Python版本的其他微型版本,则还需要小心安装用于该版本Python的所有模块。

答案 1 :(得分:0)

到目前为止,在~/.bashrc中添加以下内容似乎效果很好:

if [ ! -d ~/venv/python3 ]; then
    python3 -m venv --system-site-packages ~/venv/python3
fi

if [ -d ~/venv/python3 ]; then
    VIRTUAL_ENV_DISABLE_PROMPT=1 . ~/venv/python3/bin/activate
fi

大多数系统安装的脚本都使用/usr/bin硬编码的Python之一,而不是使用/usr/bin/env python,因此不受此影响。