ImportError:“ packaging.requirements”包是必需的

时间:2019-08-18 10:46:55

标签: python python-2.7 gunicorn setuptools

我正在使用ubuntu运行VPS:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty

我试图将Python从2.7.6升级到2.7.16。

只是在升级后Gunicorn库无法启动

Traceback (most recent call last):
  File "/home/user/bin/python-2.7/bin/gunicorn", line 7, in <module>
    from gunicorn.app.wsgiapp import run
  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
    from gunicorn.app.base import Application
  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 12, in <module>
    from gunicorn import util
  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/util.py", line 12, in <module>
    import pkg_resources
  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/__init__.py", line 82, in <module>
    __import__('pkg_resources.extern.packaging.requirements')
  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/extern/__init__.py", line 61, in load_module
    "distribution.".format(**locals())
ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.

有人告诉我这可能是一些setuptools问题,所以我用它们更新了

python -m ensurepip
Looking in links: /tmp/tmp98U8zf
Requirement already satisfied: setuptools in ./bin/python-2.7/lib/python2.7/site-packages (41.1.0)
Requirement already satisfied: pip in ./bin/python-2.7/lib/python2.7/site-packages (19.2.2)

(无更改)

我试图做

pip install packaging

(无更改)

或将setuptools降级到19.2(如Python 2.7 The 'packaging' package is required; normally this is bundled with this package中的建议)

得到

  File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/config.py", line 8, in <module>
    import copy
  File "/home/user/bin/python-2.7/lib/python2.7/copy.py", line 52, in <module>
    import weakref
  File "/home/user/bin/python-2.7/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref

在我的情况下,有什么可行的方法可以升级到2.7.16 python?

UPD

我用升级了python

wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
make clean && ./configure --enable-unicode=ucs4 --enable-shared --prefix=/home/user/bin/python-2.7/ --with-ensurepip=install && make && make install
pip install -r ~/django/django_projects/requirements.txt

点冻结:https://www.pastiebin.com/5d592ea701503

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试通过键入以下内容来升级点子:

pip install --upgrade pip

答案 1 :(得分:0)

场景

首先:尝试执行Python脚本时,出现以下错误消息:

ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.

第二::尝试使用setuptools检查pip show setuptools的版本时(或使用任何其他pip命令时),出现下一条错误消息:

ImportError: cannot import name _remove_dead_weakref

原因

这是因为当前的Python安装被搞砸了。就我而言,混乱是由于从Ubuntu 16.04升级到Ubuntu 18.04引起的。

解决方案

使用虚拟环境

在最佳情况下,您已经在使用虚拟环境(这是我的情况)。解决方案是重新创建/设置您的venv(step-by-step):

$ cd /path/to/your/venv

# remove your old venv
$ rm -rf ./*

# create a new one
$ /usr/bin/virtualenv . --python=YOUR-PYTHON-VERSION

# activate venv
$ source bin/activate

# verify the correct python version is installed
$ python --version

例如,将YOUR-PYTHON-VERSION替换为python2.7python3.7

重新创建venv后,问题应已解决,您应该可以再次使用pip。

没有虚拟环境

我认为解决此问题的最佳方法是完全卸载所有非默认的Python版本(例如Ubuntu 18.04随附的Python 3.6+),然后重新安装所有其他Python版本需要。