在 debian 10 上安装 python2.7 的 virtualenv

在 debian 10 上安装 python2.7 的 virtualenv

我想python2.7与 一起使用virtualenv。我最近从 debian 8 升级到 debian 10。我最初有python2.7pip安装在 debian 8 上,但也许安装过程中发生了一些事情,现在我没有pip.但我仍然安装了python2.7

$ python --version
Python 2.7.16

所以我只是pip像这样安装:

$ cd /tmp
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py

现在我可以看到我已经安装了 pip:

$ which pip
/home/me/.local/bin/pip
$ pip --version
pip 20.0.2 from /home/me/.local/lib/python2.7/site-packages/pip (python 2.7)

首先,这是应该安装 pip 的地方吗?在我的主目录下?我是这台电脑上的唯一用户,但我不确定是否pip应该在/usr/share/或更公开的地方才能使其正常工作?我应该使用sudo python /tmp/get-pip.py安装吗pip?我不打算以 root 身份运行 python,但apt安装总是需要 root 权限,所以也许安装pip也应该有?文档没有具体说明。

无论如何,接下来我尝试更新pip到最新版本并安装virtualenv

$ pip install -U pip
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already up-to-date: pip in ./.local/lib/python2.7/site-packages (20.0.2)

$ pip install virtualenv
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: virtualenv in ./.local/lib/python2.7/site-packages (15.1.0)

一切看起来都不错。但是当我尝试检查virtualenv我拥有的版本时,它失败了:

$ virtualenv --version
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from virtualenv.__main__ import run_with_catch
ImportError: No module named __main__

如果我尝试使用virtualenv它总是会抛出这些错误。

总的来说,我的问题是如何在 debian 10 上正确安装和 。python2.7我不介意卸载所有内容并重新开始(如果需要的话)。pipvirtualenv


按照指示斯蒂芬·基特在下面的答案中,我尝试卸载之前安装的pip和版本,但这会产生一些新错误。我将准确解释我所做的事情......virtualenvget-pip.py

首先卸载virtualenv

$ pip uninstall virtualenv
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Found existing installation: virtualenv 15.1.0
Uninstalling virtualenv-15.1.0:
  Would remove:
    /home/me/.local/bin/virtualenv
    /home/me/.local/lib/python2.7/site-packages/virtualenv-15.1.0.dist-info/*
    /home/me/.local/lib/python2.7/site-packages/virtualenv.py
    /home/me/.local/lib/python2.7/site-packages/virtualenv_support/*
Proceed (y/n)? y
  Successfully uninstalled virtualenv-15.1.0

看起来不错。然后卸载pip:

$ python -m pip uninstall pip
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Found existing installation: pip 20.0.2
Uninstalling pip-20.0.2:
  Would remove:
    /home/me/.local/bin/pip
    /home/me/.local/bin/pip2
    /home/me/.local/bin/pip2.7
    /home/me/.local/lib/python2.7/site-packages/pip-20.0.2.dist-info/*
    /home/me/.local/lib/python2.7/site-packages/pip/*
Proceed (y/n)? y
  Successfully uninstalled pip-20.0.2

$ pip --version
bash: /home/me/.local/bin/pip: No such file or directory
$ ls -a ~/.local/bin
.  ..  chardetect  easy_install  easy_install-2.7  flake8  pew  pipenv  pycodestyle  pyflakes  virtualenv-clone

这似乎也不错。我不确定 bash 如何知道pip应该是什么/home/me/.local/bin/pip,因为该文件不存在。也许 bash 有缓存?无论如何,接下来安装pipvirtualenv从 debian 10 存储库:

$ sudo apt install python-pip virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  python3-distutils python3-lib2to3 python3-virtualenv
The following NEW packages will be installed:
  python-pip python3-distutils python3-lib2to3 python3-virtualenv virtualenv

但是当我尝试检查virtualenv我现在拥有的版本时,它失败了:

$ virtualenv --version
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from virtualenv.__main__ import run_with_catch
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/__init__.py", line 3, in <module>
    from .run import cli_run
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/run/__init__.py", line 6, in <module>
    from virtualenv.run.app_data import AppDataAction
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/run/app_data.py", line 8, in <module>
    from virtualenv.util.lock import ReentrantFileLock
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/lock.py", line 11, in <module>
    from virtualenv.util.path import Path
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/path/__init__.py", line 3, in <module>
    from ._pathlib import Path
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/path/_pathlib/__init__.py", line 42, in <module>
    from pathlib2 import Path
ImportError: No module named pathlib2

答案1

为了避免把事情搞砸外部virtualenvs,我建议使用打包版本:

sudo apt install python-pip virtualenv

(如果适用的话,还有python3-pipPython 3 支持)。

您可能需要删除主目录中安装的pip和版本,以及外部的任何其他版本。virtualenvPATH/usr/bin

设置 virtualenvs 时,您可以指定 Python 2.7:

virtualenv -p /usr/bin/python2.7 ...

virtualenv会做正确的事。

答案2

经过进一步调查和讨论斯蒂芬·基特我发现我virtualenv的电脑上有 2 个不同的版本:

$ find /usr/ -iname "virtualenv" | grep bin
/usr/local/bin/virtualenv
/usr/bin/virtualenv

当我运行它时,其中的一个/usr/bin/virtualenv不会抛出错误:

$ /usr/bin/virtualenv --version
15.1.0

通过一些网络搜索,我发现其中的一个/usr/local/bin/virtualenv必须以 root 身份安装pip。所以我以piproot 身份卸载了它:

$ sudo pip uninstall virtualenv
Uninstalling virtualenv-20.0.13:
  Would remove:
    /usr/local/bin/virtualenv
    /usr/local/lib/python2.7/dist-packages/virtualenv-20.0.13.dist-info/*
    /usr/local/lib/python2.7/dist-packages/virtualenv/*
Proceed (y/n)? y
  Successfully uninstalled virtualenv-20.0.13

现在一切都很好:

$ which virtualenv
/usr/bin/virtualenv
$ find /usr/ -iname "virtualenv" | grep bin
/usr/bin/virtualenv

相关内容