Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: The 'packaging.requirements' package is required

Tags:

I'm running VPS with ubuntu:

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

I tried to upgrade Python to 2.7.16 from 2.7.6.

Just after upgrade Gunicorn library failed to start with

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.

I was told that it could be some setuptools issue, so I renewed them with

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)

(no changes)

I tried to do

pip install packaging

(no changes)

or downgrade setuptools to 19.2 (as suggested in Python 2.7 The 'packaging' package is required; normally this is bundled with this package)

and got

  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

Is there any working way to upgrade to 2.7.16 python in my case?

UPD:

I upgraded python with it with

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

pip freeze: https://www.pastiebin.com/5d592ea701503

Thanks.

like image 378
dbf Avatar asked Aug 18 '19 10:08

dbf


1 Answers

Scenario

First: When trying to execute a Python script, the following error message appears:

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.

Second: When trying to check the version of setuptools with pip show setuptools (or when using any other pip command), the next error message appears:

ImportError: cannot import name _remove_dead_weakref

Reason

This is because the current Python installation is somehow screwed up. In my case the mess was caused by an upgrade from Ubuntu 16.04 to Ubuntu 18.04.

Solution

With Virtual Environment

In the best case you were already using a virtual environment (this was my case). The solution here would be to recreate/setup your venv again (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

For example replace YOUR-PYTHON-VERSION with python2.7 or python3.7.

After recreating your venv the problem should be fixed and you should be able to use pip again.

Without Virtual Environment

I think the best way to fix the problem would be to completely uninstall all non-default Python versions (for example Ubuntu 18.04 comes with Python 3.6+) and then make a clean reinstall of all other Python versions needed.

like image 83
winklerrr Avatar answered Oct 04 '22 02:10

winklerrr