Commits

antocuni  committed 23980e1

fix virtualenv bug, which is present also in the original version: if
PYTHONPATH is set (even if it is empty), CPython will always try to import a
site.py from the current directory before looking in the stdlib. This is a
problem when we use --distribute, because the code chdir() in
virtualenv_support, and the the python subprocess which is started tries to
import virtualenv_support/site.py, which in turns fails as it cannot find
orig-prefix.txt.

If all of this sounds complicated, try to just run:
$ PYTHONPATH ./virtualenv.py --distribute xxx

to see that it fails (tested with cpython 2.6.5 on ubuntu).

  • Participants
  • Parent commits 234f32d

Comments (0)

Files changed (1)

File virtualenv.py

     if unzip:
         cmd.append('--always-unzip')
     env = {}
+    remove_from_env = []
     if logger.stdout_level_matches(logger.DEBUG):
         cmd.append('-v')
 
         # the source is found, let's chdir
         if source is not None and os.path.exists(source):
             os.chdir(os.path.dirname(source))
+            # in this case, we want to be sure that PYTHONPATH is unset (not
+            # just empty, really unset), else CPython tries to import the
+            # site.py that it's in virtualenv_support
+            remove_from_env.append('PYTHONPATH')
         else:
             logger.info('No %s egg found; downloading' % project_name)
         cmd.extend(['--always-copy', '-U', project_name])
         call_subprocess(cmd, show_stdout=False,
                         filter_stdout=_filter_ez_setup,
                         extra_env=env,
+                        remove_from_env=remove_from_env,
                         cwd=cwd)
     finally:
         logger.indent -= 2
 
 def call_subprocess(cmd, show_stdout=True,
                     filter_stdout=None, cwd=None,
-                    raise_on_returncode=True, extra_env=None):
+                    raise_on_returncode=True, extra_env=None,
+                    remove_from_env=None):
     cmd_parts = []
     for part in cmd:
         if len(part) > 40:
     else:
         stdout = subprocess.PIPE
     logger.debug("Running command %s" % cmd_desc)
-    if extra_env:
+    if extra_env or remove_from_env:
         env = os.environ.copy()
-        env.update(extra_env)
+        if extra_env:
+            env.update(extra_env)
+        if remove_from_env:
+            for varname in remove_from_env:
+                env.pop(varname, None)
     else:
         env = None
     try: