2023-07-10

pkg_resources InvalidVersion: Invalid version

Supernova

Lately I've been getting lots of errors similar to pkg_resources.extern.packaging.version.InvalidVersion: Invalid version. Here's how I fixed them.

The problem

I've had other validation errors in the past because python's packaging dropped support for "legacy" version numbers. To fix this it should be possible to downgrade packaging, if it's your own code that fails installing packaging-legacy could also be an option.

However, this time I was installing some third party application. So I quickly edited the version module to remove the invalid part of the version number.

class Version(_BaseVersion):                                                                     
                                                                                                   
      _regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE)         
                                                                                                   
      def __init__(self, version: str) -> None:                                                    
                                                                                                   
          # Validate the version and parse it into pieces                                          
          match = self._regex.search(version)                                                      
          if not match:                                                                            
              # raise InvalidVersion(f"Invalid version: '{version}'")                              
              version = version.replace("bad_part", "")                                        
          match = self._regex.search(version)                                                      
          if not match:                                                                            
              raise InvalidVersion(f"Invalid version: '{version}'")

Updating the Version class like this could help you too, the version.py file should be in the traceback. It's not a good or permanent solution though, you should probably report bugs upstream, but projects affected by this might not be well maintained.

0 comments

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo