【问题标题】:How do I correctly install pip3 in a Python 3.3 installation located at a non-standard path?如何在位于非标准路径的 Python 3.3 安装中正确安装 pip3?
【发布时间】:2017-12-12 20:43:42
【问题描述】:

我有一台 Ubuntu 16.04 机器,在 /python/ 安装了多个 python 3,并且需要能够在它们之间随意切换。其中一个版本是 Python 3.3,直到 Python 3.4.x 才预安装 pip,所以我想将 pip 安装到这个 python 安装中。我不想使用发行包,因为它实际上需要集成到 /python/python33 安装中,我在管理这个时遇到了麻烦:

get-pip.py尝试卸载/usr/bin的pip版本:

$ /tmp/get-pip.py --prefix /python/python33
Collecting pip
  Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
Exception:
Traceback (most recent call last):
  File "/tmp/tmpdgdWPZ/pip.zip/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/tmp/tmpdgdWPZ/pip.zip/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/tmp/tmpdgdWPZ/pip.zip/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/tmp/tmpdgdWPZ/pip.zip/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/tmp/tmpdgdWPZ/pip.zip/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/tmp/tmpdgdWPZ/pip.zip/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/bin/pip'
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

如果我添加--ignore-installed,它不会出错,但仍在查看 /usr/bin 中的 pip 版本(它抱怨的旧 8.1.2 版本):

$ /tmp/get-pip.py --ignore-installed --prefix /python/python33
Collecting pip
  Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-9.0.1
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

easy_install 也不起作用:

$ export PYTHONPATH="$PYTHONPATH:/python/python33/"; /usr/bin/easy_install --prefix /python/python33 pip
TEST FAILED: /python/python33/lib/python2.7/site-packages does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /python/python33/lib/python2.7/site-packages

and your PYTHONPATH environment variable currently contains:

    '/pyqt/sip-4.13.1/site-packages:/pyqt/site-packages:/python/python33/'

Here are some of your options for correcting the problem:

* You can choose a different installation directory, i.e., one that is
  on PYTHONPATH or supports .pth files

* You can add the installation directory to the PYTHONPATH environment
  variable.  (It must then also be on PYTHONPATH whenever you run
  Python and want to use the package(s) you are installing.)

* You can set up the installation directory to support ".pth" files by
  using one of the approaches described here:

  https://pythonhosted.org/setuptools/easy_install.html#custom-installation-locations

Please make the appropriate changes for your system and try again.

为什么它试图安装到/python/python33/lib/python2.7/site-packages?这是python 2版本的pip还是什么?

我在这里阅读了很多关于 SO 的相关答案,但到目前为止都没有任何帮助。

【问题讨论】:

  • 如果你想使用多个版本的python,你应该使用virtualenv或类似的东西,比如conda。
  • 当然。但是让我们想象一下,无论出于何种原因,我都做不到。假设我在公司环境中......
  • 你试过/python/python33 /tmp/get-pip.py吗?
  • 成功了,@georgexsh。非常感谢!
  • 为什么你还是不能使用 virtualenv?

标签: python installation pip


【解决方案1】:

我在 16.04 的 Ubuntu Box 上安装了 Python 3.5.2 和 3.6.3。

3.5.2 是系统安装在/usr/bin 的版本 python3和python3.5的符号链接指向这个版本

3.6.3 安装在 /usr/local/bin python3.6指向这个版本

对我来说,以下作品:

wget https://bootstrap.pypa.io/get-pip.py
python3.6 get-pip.py
mkdir /home/ubuntu/myproj/venv
python3.6 -m venv /home/ubuntu/myproj/venv
#Activate the venv
source /home/ubuntu/myproj/venv/bin/activate

python3 现在在我的虚拟环境中指向 3.6。

您是如何在 16.04 上安装 Python 3.6 的。为了不破坏事物,我有一个受伤的世界。最后我从源代码构建。有关详细信息,请参阅此gist。这个question 解释了原因。而这个blog 帖子给了我解决方案。

【讨论】:

  • 你试过this ppa吗?
  • 是的,它适用于 python 3.6 问题是 16.04 需要 python 3.5 才能在系统级别运行 apt-get。
  • 从 deadsnake ppa 安装 python36 不会影响系统 pyhton,你有问题吗?
  • 现在不行。我意识到当通过 python3 调用时,我将 python 3.6 优先于 3.5。当然,学习从源代码构建始终是一种冒险。
猜你喜欢
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2019-08-23
  • 2013-12-13
  • 2013-08-24
相关资源
最近更新 更多