Repository: pypa/virtualenv

Issue:
PyPy 2.2 and virtualenv 1.10 on Windows7
I have been unable to use virtualenv.exe -p <path>\pypy.exe. It seems virtualenv is not copying any of the lib_pypy directories, and non-recursively copying lib-python\2.7. I am running Windows 7, virtualenv 1.10.1 and PyPy 2.2 alpha. I can copy the missing files by hand into the virtualenv, and then rerun the command and all works fine.

Here is some more info:

I have unzipped pypy from the latest nightlies, 
http://buildbot.pypy.org/nightly/trunk/pypy-c-jit-67639-205ceffda4fa-win32.zip
Here is it's version:

```
>pypy-c-jit-67639-205ceffda4fa-win32\pypy.exe
Python 2.7.3 (205ceffda4fa, Oct 26 2013, 21:59:48)
[PyPy 2.2.0-alpha0 with MSC v.1500 32 bit] on win32
```

I have a stock python 2.7.5 in c:\python27, with virtualenv from PyPi installed:

```
>c:\Python27\Scripts\virtualenv.exe --version
1.10.1
```

Then I try to use virtualenv to install pypy

```
>c:\Python27\Scripts\virtualenv.exe -p pypy-c-jit-67639-205ceffda4fa-win32\pypy.exe dummy
Running virtualenv with interpreter pypy-c-jit-67639-205ceffda4fa-win32\pypy.exe
New pypy executable in dummy\bin\pypy.exe
debug: WARNING: Library path not found, using compiled-in sys.path.
debug: WARNING: 'sys.prefix' will not be set.
debug: WARNING: Make sure the pypy binary is kept inside its tree of files.
debug: WARNING: It is ok to create a symlink to it from somewhere else.
debug: OperationError:
debug:  operror-type: ImportError
debug:  operror-value: No module named os
ERROR: The executable dummy\bin\pypy.exe is not functioning
ERROR: It thinks sys.prefix is u'c:\\users\\matti\\pypy_stuff' (should be u'c:\\users\\matti\\pypy_stuff\\dummy')
ERROR: virtualenv is not compatible with this system or executable
Note: some Windows users have reported this error when they installed Python for "Only this user" or have multiple versions of Python installed. Copying the appropriate PythonXX.dll to the virtualenv Scripts/ directory may fix this problem.
```

By hand (actually using total commander) I copy lib_pypy and lib-python and then rerun the command. Then it all works fine:

```
>c:\Python27\Scripts\virtualenv.exe -p pypy-c-jit-67639-205ceffda4fa-win32\pypy.exe dummy
Running virtualenv with interpreter pypy-c-jit-67639-205ceffda4fa-win32\pypy.exe
New pypy executable in dummy\bin\pypy.exe
Overwriting dummy\lib-python\2.7\distutils\__init__.py with new content
Installing Setuptools.........................................................................done.
Installing Pip..........................................................................................done.

Thanks,
Matti Picus

```



Discussion:
Problem still present in virtualenv 1.11.2.  I was testing with pypy-2.2.1-win32.zip, extracted into c:\pypy-2.2.1-win32.

Problem still present in virtualenv 1.11.4. I was also trying to run pypy-2.2.1-win32.zip, extracted into c:\pypy-2.2.1-win32.

Progress. The first problem is that install_python() is not copying any of the lib_pypy directory, which is needed for proper functioning of the virtualenv. I do not see how to do this cleanly, could some virtualenv guru give me a hint how to copy recursively the entire lib_pypy tree? The directory structure of pypy is

```
home
|- lib-python/2.7/<stdlib>
|- lib_pypy/<files and directories critical for pypy with suffixes py and pyd>
|- tcl/<cpython compatible version of tcl and tk runtime>
|- pypy.exe
|- *.dll
```

The second failure occurs when installing pip, but let's cross that bridge when this part is clear...


Resolving patch:
diff --git a/virtualenv.py b/virtualenv.py
--- a/virtualenv.py
+++ b/virtualenv.py
@@ -1316,11 +1316,22 @@ def install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages, clear, sy
             copyfile(py_executable, python_executable, symlink)
 
             if is_win:
-                for name in 'libexpat.dll', 'libpypy.dll', 'libpypy-c.dll', 'libeay32.dll', 'ssleay32.dll', 'sqlite.dll':
+                for name in ['libexpat.dll', 'libpypy.dll', 'libpypy-c.dll',
+                            'libeay32.dll', 'ssleay32.dll', 'sqlite3.dll',
+                            'tcl85.dll', 'tk85.dll']:
                     src = join(prefix, name)
                     if os.path.exists(src):
                         copyfile(src, join(bin_dir, name), symlink)
 
+                for d in sys.path:
+                    if d.endswith('lib_pypy'):
+                        break
+                else:
+                    logger.fatal('Could not find lib_pypy in sys.path')
+                    raise SystemExit(3)
+                logger.info('Copying lib_pypy')
+                copyfile(d, os.path.join(home_dir, 'lib_pypy'), symlink)
+
     if os.path.splitext(os.path.basename(py_executable))[0] != expected_exe:
         secondary_exe = os.path.join(os.path.dirname(py_executable),
                                      expected_exe)


Test patch: