Repository: pypa/virtualenv

Issue:
Can't create a python 2.7 environment
**Issue**

In an Ubuntu 18.04 docker environment, tox is trying to do `virtualenv --no-download --python /usr/bin/python2.7 py27`.  With virtualenv 20.0.10 this worked.  As of 20.0.11, this fails with an error:
```
TypeError: unsupported operand type(s) for /: 'tuple' and 'str'
```
It should still work.

**Environment**

Provide at least:
- OS: Ubuntu 18.04
- ``pip list`` of the host python where ``virtualenv`` is installed:

```console
Package                       Version              Location                     
----------------------------- -------------------- ----------------------------------
amqp                          2.5.2
appdirs                       1.4.3
attrs                         19.3.0
bcrypt                        3.1.7
billiard                      3.6.3.0
bleach                        3.1.3
boto3                         1.12.24
botocore                      1.15.24
cachetools                    4.0.0
celery                        4.4.2
certifi                       2019.11.28
cffi                          1.14.0
chardet                       3.0.4
cheroot                       8.3.0
CherryPy                      18.5.0
click                         7.1.1
click-plugins                 1.1.1
cryptography                  2.8
ctk-cli                       1.5
decorator                     4.4.2
diskcache                     4.1.0
distlib                       0.3.0
docker                        4.2.0
docutils                      0.15.2
dogpile.cache                 0.9.0
filelock                      3.0.12
fusepy                        3.0.1
GDAL                          3.1.0
girder                        3.0.11               /opt/girder                  
girder-archive-access         0.2.0
girder-client                 3.0.11               /opt/girder/clients/python   
girder-dicom-viewer           3.0.11
girder-homepage               3.0.11
girder-jobs                   3.0.11
girder-large-image            1.0.5.dev13+gc67ca6f /opt/large_image/girder      
girder-large-image-annotation 1.0.5.dev13+gc67ca6f /opt/large_image/girder_annotation
girder-ldap                   3.0.11
girder-resource-path-tools    1.0.1.dev2
girder-slicer-cli-web         1.0.2.dev28+g9db4a50 /opt/slicer_cli_web          
girder-virtual-folders        3.0.11
girder-worker                 0.6.1.dev23+g5810d1d /opt/girder_worker           
girder-worker-utils           0.8.7.dev2+g856849e  /opt/girder_worker_utils     
girder-xtk-demo               0.4.0
Glymur                        0.9.1
histomicsui                   1.0.0.dev39+ga7d0e3f /opt/HistomicsUI             
idna                          2.9
importlib-metadata            1.5.0
importlib-resources           1.4.0
jaraco.classes                3.1.0
jaraco.collections            3.0.0
jaraco.functools              3.0.0
jaraco.text                   3.2.0
jeepney                       0.4.3
Jinja2                        2.11.1
jmespath                      0.9.5
jsonpickle                    1.3
jsonschema                    3.2.0
keyring                       21.2.0
kombu                         4.6.8
large-image                   1.0.5.dev13+gc67ca6f /opt/large_image             
large-image-source-dummy      1.0.5.dev13+gc67ca6f /opt/large_image/sources/dummy
large-image-source-gdal       1.0.5.dev13+gc67ca6f /opt/large_image/sources/gdal
large-image-source-mapnik     1.0.5.dev13+gc67ca6f /opt/large_image/sources/mapnik
large-image-source-nd2        1.0.5.dev13+gc67ca6f /opt/large_image/sources/nd2 
large-image-source-ometiff    1.0.5.dev13+gc67ca6f /opt/large_image/sources/ometiff
large-image-source-openjpeg   1.0.5.dev13+gc67ca6f /opt/large_image/sources/openjpeg
large-image-source-openslide  1.0.5.dev13+gc67ca6f /opt/large_image/sources/openslide
large-image-source-pil        1.0.5.dev13+gc67ca6f /opt/large_image/sources/pil 
large-image-source-test       1.0.5.dev13+gc67ca6f /opt/large_image/sources/test
large-image-source-tiff       1.0.5.dev13+gc67ca6f /opt/large_image/sources/tiff
large-image-tasks             1.0.5.dev13+gc67ca6f /opt/large_image/tasks       
libtiff                       0.5.0
lxml                          4.5.0
Mako                          1.1.2
mapnik                        4.0.0
MarkupSafe                    1.1.1
more-itertools                8.2.0
nd2reader                     3.2.3
numpy                         1.18.2
openslide-python              1.1.1
packaging                     20.3
palettable                    3.3.0
passlib                       1.7.2
pbr                           5.4.4
Pillow                        7.0.0
PIMS                          0.4.1
pip                           20.0.2
pkg-resources                 0.0.0
pkgconfig                     1.5.1
pkginfo                       1.5.0.1
pluggy                        0.13.1
portend                       2.6
psutil                        5.7.0
py                            1.8.1
pyasn1                        0.4.8
pyasn1-modules                0.2.8
pycparser                     2.20
pydicom                       1.4.2
Pygments                      2.6.1
pyldap                        3.0.0.post1
pylibmc                       1.6.1
pymongo                       3.10.1
pyOpenSSL                     19.1.0
pyparsing                     2.4.6
pyproj                        2.6.0
pyrsistent                    0.15.7
python-dateutil               2.8.1
python-ldap                   3.2.0
pytz                          2019.3
pyvips                        2.1.12
PyYAML                        5.3.1
readme-renderer               25.0
requests                      2.23.0
requests-toolbelt             0.9.1
s3transfer                    0.3.3
SecretStorage                 3.1.2
setuptools                    46.0.0
setuptools-scm                3.5.0
six                           1.14.0
slicerator                    1.0.0
stevedore                     1.32.0
tempora                       2.1.0
toml                          0.10.0
tox                           3.14.5
tqdm                          4.43.0
twine                         3.1.1
ujson                         2.0.2
urllib3                       1.25.8
vine                          1.3.0
virtualenv                    20.0.12
webencodings                  0.5.1
websocket-client              0.57.0
xmltodict                     0.12.0
zc.lockfile                   2.0
zipp                          3.1.0
```

**Output of the virtual environment creation**

Make sure to run the creation with `-vvv --with-traceback`:

```console
306 setup logging to NOTSET [DEBUG report:43]
375 find interpreter for spec PythonSpec(path=/usr/bin/python2.7) [INFO builtin:44]
377 Attempting to acquire lock 139669810685880 on /home/ubuntu/.local/share/virtualenv/py_info/20.0.12/beb4f305451b4835b36a63cb1c75e625a7251cf8d76b65be2f5856eee68cb551.lock [DEBUG filelock:270]
378 Lock 139669810685880 acquired on /home/ubuntu/.local/share/virtualenv/py_info/20.0.12/beb4f305451b4835b36a63cb1c75e625a7251cf8d76b65be2f5856eee68cb551.lock [INFO filelock:274]
378 get PythonInfo from /home/ubuntu/.local/share/virtualenv/py_info/20.0.12/beb4f305451b4835b36a63cb1c75e625a7251cf8d76b65be2f5856eee68cb551.json for /usr/bin/python2.7 [DEBUG cached_py_info:79]
379 Attempting to release lock 139669810685880 on /home/ubuntu/.local/share/virtualenv/py_info/20.0.12/beb4f305451b4835b36a63cb1c75e625a7251cf8d76b65be2f5856eee68cb551.lock [DEBUG filelock:315]
380 Lock 139669810685880 released on /home/ubuntu/.local/share/virtualenv/py_info/20.0.12/beb4f305451b4835b36a63cb1c75e625a7251cf8d76b65be2f5856eee68cb551.lock [INFO filelock:318]
380 proposed PythonInfo(spec=CPython2.7.17.final.0-64, exe=/usr/bin/python2.7, platform=linux2, version='2.7.17 (default, Nov  7 2019, 10:07:09) \n[GCC 7.4.0]', encoding_fs_io=UTF-8-None) [INFO builtin:50]
380 accepted PythonInfo(spec=CPython2.7.17.final.0-64, exe=/usr/bin/python2.7, platform=linux2, version='2.7.17 (default, Nov  7 2019, 10:07:09) \n[GCC 7.4.0]', encoding_fs_io=UTF-8-None) [DEBUG builtin:52]
383 filesystem is case-sensitive [DEBUG info:28]
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/__main__.py", line 62, in <module>
    run_with_catch()
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/__main__.py", line 51, in run_with_catch
    run(args, options)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/__main__.py", line 20, in run
    session = cli_run(args, options)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/run/__init__.py", line 27, in cli_run
    session = session_via_cli(args, options)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/run/__init__.py", line 35, in session_via_cli
    parser = build_parser(args, options)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/run/__init__.py", line 75, in build_parser
    CreatorSelector(interpreter, parser),
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/run/plugin/creators.py", line 15, in __init__
    creators, self.key_to_meta, self.describe, self.builtin_key = self.for_interpreter(interpreter)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/run/plugin/creators.py", line 25, in for_interpreter
    meta = creator_class.can_create(interpreter)
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/create/via_global_ref/builtin/via_global_self_do.py", line 33, in can_create
    for src in cls.sources(interpreter):
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/create/via_global_ref/builtin/cpython/cpython2.py", line 66, in sources
    for src in super(CPython2Posix, cls).sources(interpreter):
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/create/via_global_ref/builtin/cpython/cpython2.py", line 21, in sources
    for src in super(CPython2, cls).sources(interpreter):
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/create/via_global_ref/builtin/python2/python2.py", line 76, in sources
    compiled_file, to_compiled, compiled_exists = cls.from_stdlib(mappings, "{}.pyc".format(req))
  File "/home/ubuntu/.virtualenvs/girder/lib/python3.6/site-packages/virtualenv/create/via_global_ref/builtin/python2/python2.py", line 88, in from_stdlib
    result = (mappings[0] / name, mappings[1], False)
TypeError: unsupported operand type(s) for /: 'tuple' and 'str'
```

The specific error looks like it was introduced in #1728 at https://github.com/pypa/virtualenv/blob/master/src/virtualenv/create/via_global_ref/builtin/python2/python2.py#L79-L85.  Specifically, this line (https://github.com/pypa/virtualenv/blob/master/src/virtualenv/create/via_global_ref/builtin/python2/python2.py#L85)
```
        return mappings[0] / name, mappings[1], False
```
should probably be 
```
        return mappings[0][0] / name, mappings[0][1], False
```
since mappings are a list of tuples.


Discussion:
@manthey thanks for the excellent debugging, you're right 👍 
FWIW, I'm seeing this with `unicode` instead of `str`:
```
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in <module>
    sys.exit(run_with_catch())
  File "/usr/local/lib/python2.7/site-packages/virtualenv/__main__.py", line 51, in run_with_catch
    run(args, options)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/__main__.py", line 20, in run
    session = cli_run(args, options)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/run/__init__.py", line 27, in cli_run
    session = session_via_cli(args, options)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/run/__init__.py", line 35, in session_via_cli
    parser = build_parser(args, options)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/run/__init__.py", line 75, in build_parser
    CreatorSelector(interpreter, parser),
  File "/usr/local/lib/python2.7/site-packages/virtualenv/run/plugin/creators.py", line 15, in __init__
    creators, self.key_to_meta, self.describe, self.builtin_key = self.for_interpreter(interpreter)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/run/plugin/creators.py", line 25, in for_interpreter
    meta = creator_class.can_create(interpreter)
  File "/usr/local/lib/python2.7/site-packages/virtualenv/create/via_global_ref/builtin/via_global_self_do.py", line 33, in can_create
    for src in cls.sources(interpreter):
  File "/usr/local/lib/python2.7/site-packages/virtualenv/create/via_global_ref/builtin/cpython/cpython2.py", line 66, in sources
    for src in super(CPython2Posix, cls).sources(interpreter):
  File "/usr/local/lib/python2.7/site-packages/virtualenv/create/via_global_ref/builtin/cpython/cpython2.py", line 21, in sources
    for src in super(CPython2, cls).sources(interpreter):
  File "/usr/local/lib/python2.7/site-packages/virtualenv/create/via_global_ref/builtin/python2/python2.py", line 72, in sources
    module_file, to_module, module_exists = cls.from_stdlib(mappings, "{}.py".format(req))
  File "/usr/local/lib/python2.7/site-packages/virtualenv/create/via_global_ref/builtin/python2/python2.py", line 85, in from_stdlib
    return mappings[0] / name, mappings[1], False
TypeError: unsupported operand type(s) for /: 'tuple' and 'unicode'
```
Working on the fix 👍 

Resolving patch:
diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py
--- a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py
+++ b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py
@@ -82,7 +82,8 @@ def from_stdlib(mappings, name):
             src = from_std / name
             if src.exists():
                 return src, to_std, True
-        return mappings[0] / name, mappings[1], False
+        # if not exists, fallback to first in list
+        return mappings[0][0] / name, mappings[0][1], False
 
     @classmethod
     def mappings(cls, interpreter):


Test patch:
diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py
--- a/tests/unit/create/test_creator.py
+++ b/tests/unit/create/test_creator.py
@@ -493,27 +493,36 @@ def _get_sys_path(flag=None):
     not (CURRENT.implementation == "CPython" and PY2),
     reason="stdlib components without py files only possible on CPython2",
 )
-def test_pyc_only(tmp_path, mocker, session_app_data):
+@pytest.mark.parametrize(
+    "py, pyc",
+    list(
+        product(
+            [True, False] if Python2.from_stdlib(Python2.mappings(CURRENT), "os.py")[2] else [False],
+            [True, False] if Python2.from_stdlib(Python2.mappings(CURRENT), "os.pyc")[2] else [False],
+        )
+    ),
+)
+def test_py_pyc_missing(tmp_path, mocker, session_app_data, py, pyc):
     """Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)"""
-    interpreter = PythonInfo.from_exe(sys.executable, session_app_data)
-    host_pyc, _, host_pyc_exists = Python2.from_stdlib(Python2.mappings(interpreter), "os.pyc")
-    if not host_pyc_exists:
-        pytest.skip("missing system os.pyc at {}".format(host_pyc))
     previous = Python2.from_stdlib
 
     def from_stdlib(mappings, name):
         path, to, exists = previous(mappings, name)
-        if name.endswith(".py"):
-            exists = False
+        if name.endswith("py"):
+            exists = py
+        elif name.endswith("pyc"):
+            exists = pyc
         return path, to, exists
 
     mocker.patch.object(Python2, "from_stdlib", side_effect=from_stdlib)
 
-    result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", ""])
+    result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", "", "-vv"])
+    py_at = Python2.from_stdlib(Python2.mappings(CURRENT), "os.py")[1](result.creator, Path("os.py"))
+    py = pyc is False or py  # if pyc is False we fallback to serve the py, which will exist (as we only mock the check)
+    assert py_at.exists() is py
 
-    assert not (result.creator.stdlib / "os.py").exists()
-    assert (result.creator.stdlib / "os.pyc").exists()
-    assert "os.pyc" in result.creator.debug["os"]
+    pyc_at = Python2.from_stdlib(Python2.mappings(CURRENT), "osc.py")[1](result.creator, Path("os.pyc"))
+    assert pyc_at.exists() is pyc
 
 
 def test_zip_importer_can_import_setuptools(tmp_path):