Skip to content

Instantly share code, notes, and snippets.

@floer32
Last active August 7, 2024 17:36
Show Gist options
  • Save floer32/b9820fe204eebb0cc5b9aba49f3c8b22 to your computer and use it in GitHub Desktop.
Save floer32/b9820fe204eebb0cc5b9aba49f3c8b22 to your computer and use it in GitHub Desktop.
fix that pesky "ImportError: No module named packaging.version" problem. Or more generally, if you need to fix pip using pip, just make sure you do "python -m pip" instead of straight-up "pip".

It can come up while installing anything really. I'm not sure exactly which versions of pip are affected, I know I had 1.5.6 and it was affected.

These examples are grabbed from my system level Python, but of course you should use a virtualenv.

  Running setup.py install for simplegeneric
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Library/Python/2.7/site-packages/setuptools/__init__.py", line 12, in <module>
        import setuptools.version
      File "/Library/Python/2.7/site-packages/setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 70, in <module>
        import packaging.version
    ImportError: No module named packaging.version
    Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/tmp/pip_build_root/simplegeneric/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-jLBBW0-record/install-record.txt --single-version-externally-managed --compile:
    Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Python/2.7/site-packages/setuptools/__init__.py", line 12, in <module>

    import setuptools.version

  File "/Library/Python/2.7/site-packages/setuptools/version.py", line 1, in <module>

    import pkg_resources

  File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 70, in <module>

    import packaging.version

ImportError: No module named packaging.version

OK, right, so you try upgrading setuptools or pip. But chicken and egg...

$ sudo pip install -U pip
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 70, in <module>
    import packaging.version
ImportError: No module named packaging.version

Here you go. python -m pip not just pip. Heh.

$ sudo python -m pip install -U pip
Downloading/unpacking pip from https://pypi.python.org/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.0.1-py2.py3-none-any.whl#md5=297dbd16ef53bcef0447d245815f5144
  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB): 1.3MB downloaded
Installing collected packages: pip
  Found existing installation: pip 1.5.6
    Uninstalling pip:
      Successfully uninstalled pip
Successfully installed pip
Cleaning up...

You likely need to upgrade setuptools too, and you can now that your pip is working.

# virtualenv
python -m pip install --upgrade pip
pip install --upgrade setuptools
# system level
sudo python -m pip install --upgrade pip
sudo pip install --upgrade setuptools
@snehaIndapure410
Copy link

snehaIndapure410 commented May 26, 2022

"pip install packaging" worked for me

run the above command in the folder path '/Library/Frameworks/Python.framework/Versions/3.7'

@nicholascgilpin
Copy link

In 2022, packaging worked where other methods failed.
pip install packaging

@averyfreeman
Copy link

OmniOS is very specific because it has like 3 package managers :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment