【问题标题】:Pip permission denied on Ubuntu 20.04Ubuntu 20.04 上的 Pip 权限被拒绝
【发布时间】:2022-01-13 08:26:16
【问题描述】:

由于某些原因,我在我的 Ubuntu 18.04 中使用 sudo pip for python3.6 安装 python 包。我最近将 ubuntu 版本升级到了 python3.8 附带的 20.04,我想继续使用它。因此,现在,我的系统有 3 个 python 版本——2.7、3.6、3.8。

$ pip -V
pip 20.2.3 from /usr/local/lib/python2.7/dist-packages/pip-21.0.1-py2.7.egg/pip (python 2.7)

$ pip3 -V
pip 20.3.4 from /home/dell/.local/lib/python3.8/site-packages/pip (python 3.8)

$ python -V
2.7.18

$ python3 -V 
Python 3.8.10

但是每当我使用 pip3 安装 python 包时,它都会显示以下错误 -

$ pip3 install numpy
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.4.1 requires numpy~=1.19.2, but you have numpy 1.22.0 which is incompatible.
Successfully installed numpy-1.22.0
ERROR: Exception:
Traceback (most recent call last):
  File "/usr/lib/python3.8/shutil.py", line 791, in move
    os.rename(src, real_dst)
PermissionError: [Errno 13] Permission denied: '/tmp/pip-target-x7bedgtq/lib/python/numpy-1.22.0.dist-info' -> '/usr/local/lib/python3.6/dist-packages/numpy-1.22.0.dist-info'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 223, in _main
    status = self.run(options, args)
  File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 180, in wrapper
    return func(self, options, args)
  File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 453, in run
    self._handle_target_dir(
  File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 510, in _handle_target_dir
    shutil.move(
  File "/usr/lib/python3.8/shutil.py", line 807, in move
    copytree(src, real_dst, copy_function=copy_function,
  File "/usr/lib/python3.8/shutil.py", line 557, in copytree
    return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
  File "/usr/lib/python3.8/shutil.py", line 458, in _copytree
    os.makedirs(dst, exist_ok=dirs_exist_ok)
  File "/usr/lib/python3.8/os.py", line 223, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/numpy-1.22.0.dist-info'

它以某种方式指向我使用 sudo pip 安装的 python3.6 numpy 包。我该如何解决这个问题?无论如何,python3.6 包是否可以从 sudo 访问中解除阻塞?

还是把'/usr/local/lib/python3.6/dist-packages/'里面的包都卸载掉就好了?

编辑@psarka 评论,

$ sudo python3.6 -m pip uninstall numpy
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3/dist-packages/pip/__main__.py", line 16, in <module>
    from pip._internal.cli.main import main as _main  # isort:skip # noqa
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'

【问题讨论】:

    标签: python linux ubuntu pip


    【解决方案1】:

    这不是对您问题的直接回答,因为您以错误的方式做事,并面临以错误方式做事的直接后果。所以停下来,使用venv! :)

    cd my_project
    python3.8 -m venv venv
    source venv/bin/activate
    pip install whatever
    

    如果你没有python3.8-venv,安装它:

    sudo apt install python3.8-venv
    

    【讨论】:

    • 我肯定之前没有使用过 venv 把 python 的东西搞砸了。但是我也按照您提到的方式尝试了 venv,这是相同的错误。还有其他解决方案吗?
    • 嗯,尝试删除有问题的包。首先不使用 sudo(您应该得到相同的访问被拒绝错误),然后使用 sudo:sudo python3.6 -m pip uninstall numpy.
    • 请看我的编辑...它现在抛出另一个错误。我试过sudo apt-get install python3.8-distutils...还是同样的错误。
    • 没有用的尝试通过这个,只需干净地重新安装你的操作系统。
    • 我建议尝试 pipenv,在我看来,它可以让处理虚拟环境变得非常容易并且不易出错pipenv.pypa.io/en/latest
    猜你喜欢
    • 2021-08-29
    • 2018-07-15
    • 2016-03-12
    • 2017-02-02
    • 2019-06-05
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多