title: How do I compare version numbers in Python?
body: <p>I am walking a directory that contains eggs to add those eggs to the <code>sys.path</code>.  If there are two versions of the same .egg in the directory, I want to add only the latest one.</p>
  
  <p>I have a regular expression <code>r"^(?P&lt;eggName&gt;\w+)-(?P&lt;eggVersion&gt;[\d\.]+)-.+\.egg$</code> to extract the name and version from the filename.  The problem is comparing the version number, which is a string like <code>2.3.1</code>.</p>
  
  <p>Since I'm comparing strings, 2 sorts above 10, but that's not correct for versions.</p>
  
  <pre><code>&gt;&gt;&gt; "2.3.1" &gt; "10.1.1"
  True
  </code></pre>
  
  <p>I could do some splitting, parsing, casting to int, etc., and I would eventually get a workaround.  But this is Python, <a href="https://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java">not Java</a>.  Is there an elegant way to compare version strings?</p>