Repository: pypa/pip

Issue:
Issue uninstalling egg with slightly different name
I've an old venv created via virtualenv 1.4.9:

```
$ virtualenv --setuptools --no-site-packages test_setuptools
$ ls test_setuptools/lib/python2.6/site-packages/
easy-install.pth  pip-6.0.3-py2.6.egg  setuptools-0.6c11-py2.6.egg  setuptools.pth
```

Note the setuptools-0.6c11-py2.6.egg.

```
(test_setuptools) $ pip install -U setuptools
DEPRECATION: --download-cache has been deprecated and will be removed in the future. Pip now automatically uses and configures its cache.
Processing ./.cache/pip/wheelhouse/setuptools-10.0.1-py2.py3-none-any.whl
Installing collected packages: setuptools
  Found existing installation: setuptools 0.6rc11
    Uninstalling setuptools-0.6rc11:
      Successfully uninstalled setuptools-0.6rc11

Successfully installed setuptools-0.6rc11

(test_setuptools) $ pip list | grep setup
setuptools (0.6rc11)

(test_setuptools) $ ls test_setuptools/lib/python2.6/site-packages/
easy-install.pth  easy_install.py  easy_install.pyc  _markerlib  pip-6.0.3-py2.6.egg  pkg_resources  setuptools  setuptools-0.6c11-py2.6.egg  setuptools-10.0.1.dist-info  setuptools.pth
```

Setuptools 10.0.1 is indeed installed but 0.6rc11 is not uninstalled because the egg is named setuptools-0.6c11-py2.6.egg and not setuptools-0.6rc11-py2.6.egg



Discussion:
I've this issue on Debian 6.0 (squeeze)


Resolving patch:
diff --git a/pip/req/req_install.py b/pip/req/req_install.py
--- a/pip/req/req_install.py
+++ b/pip/req/req_install.py
@@ -566,7 +566,6 @@ def uninstall(self, auto_confirm=False):
         # Workaround - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=618367
         debian_egg_info_path = pip_egg_info_path.replace(
             '-py%s' % pkg_resources.PY_MAJOR, '')
-        easy_install_egg = dist.egg_name() + '.egg'
         develop_egg_link = egg_link_path(dist)
 
         pip_egg_info_exists = os.path.exists(pip_egg_info_path)
@@ -603,9 +602,12 @@ def uninstall(self, auto_confirm=False):
                     paths_to_remove.add(path + '.py')
                     paths_to_remove.add(path + '.pyc')
 
-        elif dist.location.endswith(easy_install_egg):
+        elif dist.location.endswith('.egg'):
             # package installed by easy_install
+            # We cannot match on dist.egg_name because it can slightly vary
+            # i.e. setuptools-0.6c11-py2.6.egg vs setuptools-0.6rc11-py2.6.egg
             paths_to_remove.add(dist.location)
+            easy_install_egg = os.path.split(dist.location)[1]
             easy_install_pth = os.path.join(os.path.dirname(dist.location),
                                             'easy-install.pth')
             paths_to_remove.add_pth(easy_install_pth, './' + easy_install_egg)
@@ -625,6 +627,10 @@ def uninstall(self, auto_confirm=False):
         elif dist_info_exists:
             for path in pip.wheel.uninstallation_paths(dist):
                 paths_to_remove.add(path)
+        else:
+            logger.debug(
+                'Not sure how to uninstall: %s - Check: %s',
+                dist, dist.location)
 
         # find distutils scripts= scripts
         if dist.has_metadata('scripts') and dist.metadata_isdir('scripts'):


Test patch:
diff --git a/tests/functional/test_install_upgrade.py b/tests/functional/test_install_upgrade.py
--- a/tests/functional/test_install_upgrade.py
+++ b/tests/functional/test_install_upgrade.py
@@ -329,7 +329,7 @@ def test_py2_from_setuptools_6_to_setuptools_7(
             "Found existing installation: setuptools 0.6rc11" in result.stdout
         )
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.8)" in result.stdout
+        assert "setuptools (0.9.8)" in result.stdout
 
     def test_py2_py3_from_distribute_6_to_setuptools_7(
             self, script, data, virtualenv):
@@ -344,8 +344,8 @@ def test_py2_py3_from_distribute_6_to_setuptools_7(
             "Found existing installation: distribute 0.6.34" in result.stdout
         )
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.8)" in result.stdout
-        "distribute (0.7.3)" in result.stdout
+        assert "setuptools (0.9.8)" in result.stdout
+        assert "distribute (0.7.3)" not in result.stdout
 
     def test_from_setuptools_7_to_setuptools_7(self, script, data, virtualenv):
         self.prep_ve(script, '1.10', virtualenv.pip_source_dir)
@@ -355,7 +355,7 @@ def test_from_setuptools_7_to_setuptools_7(self, script, data, virtualenv):
         )
         assert "Found existing installation: setuptools 0.9.7" in result.stdout
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.8)" in result.stdout
+        assert "setuptools (0.9.8)" in result.stdout
 
     def test_from_setuptools_7_to_setuptools_7_using_wheel(
             self, script, data, virtualenv):
@@ -368,7 +368,7 @@ def test_from_setuptools_7_to_setuptools_7_using_wheel(
         # only wheels use dist-info
         assert 'setuptools-0.9.8.dist-info' in str(result.files_created)
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.8)" in result.stdout
+        assert "setuptools (0.9.8)" in result.stdout
 
     # disabling intermittent travis failure:
     #   https://github.com/pypa/pip/issues/1379
@@ -387,13 +387,12 @@ def test_from_setuptools_7_to_setuptools_7_with_distribute_7_installed(
             '--find-links=%s' % data.find_links, 'setuptools==0.9.6'
         )
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.6)" in result.stdout
-        "distribute (0.7.3)" in result.stdout
+        assert "setuptools (0.9.6)" in result.stdout
+        assert "distribute (0.7.3)" not in result.stdout
         result = self.script.run(
             self.ve_bin / 'pip', 'install', '--no-index',
             '--find-links=%s' % data.find_links, '-U', 'setuptools'
         )
         assert "Found existing installation: setuptools 0.9.6" in result.stdout
         result = self.script.run(self.ve_bin / 'pip', 'list')
-        "setuptools (0.9.8)" in result.stdout
-        "distribute (0.7.3)" in result.stdout
+        assert "setuptools (0.9.8)" in result.stdout