Repository: python-poetry/poetry

Issue:
The specified Python version (3.8) is not supported by the project (^3.7).
- [x] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) Poetry version.
- [x] I have searched the [issues](https://github.com/python-poetry/poetry/issues) of this repo and believe that this is not a duplicate.
- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option).

- **OS version and name**: Ubuntu 18.04.3 LTS
- **Poetry version**: `Poetry version 1.0.0`
- **Link of a [Gist](https://gist.github.com/) with the contents of your pyproject.toml file**: https://gist.github.com/jobevers/a3e674e4f316a6c0ea535ac818bf5f61

## Issue
I wanted to start using python 3.8 in a project:

```
» poetry env use -vvv 3.8.0 

[NoCompatiblePythonVersionFound]
The specified Python version (3.8) is not supported by the project (^3.7).
Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

Traceback (most recent call last):
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/console/commands/env/use.py", line 23, in handle
    env = manager.activate(self.argument('python'), self._io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 259, in activate
    self.create_venv(io, executable=python, force=create)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 520, in create_venv
    self._poetry.package.python_versions, python_minor
```


Discussion:
You should use only the minor version with `env use`:

```bash
poetry env use 3.8
```

Basically, Poetry looks for a `python3.8.0` executable which it does not find leading to this error.
Hello @jobevers ,

I guess the error message is misleading. `poetry env use` expect the path to an python executable and not a version number.

So specify the complete path to the python 3.8 interpreter or, if you use pyenv for handling multiple python versions, activate the correct version first.

fin swimmer
```
» poetry env use -vvv 3.8

[NoCompatiblePythonVersionFound]
The specified Python version (3.8) is not supported by the project (^3.7).
Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

Traceback (most recent call last):
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/console/commands/env/use.py", line 23, in handle
    env = manager.activate(self.argument('python'), self._io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 259, in activate
    self.create_venv(io, executable=python, force=create)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 520, in create_venv
    self._poetry.package.python_versions, python_minor
```
@jobevers Do you actually have a `python3.8` executable in your `PATH`?
```
poetry env use -vvv /home/jobevers/code/pyenv/versions/3.8.0/bin/python                                             

[NoCompatiblePythonVersionFound]
The specified Python version (3.8) is not supported by the project (^3.7).
Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

Traceback (most recent call last):
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/clikit/api/command/command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/console/commands/env/use.py", line 23, in handle
    env = manager.activate(self.argument('python'), self._io)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 259, in activate
    self.create_venv(io, executable=python, force=create)
  File "/home/jobevers/.local/pipx/venvs/poetry/lib/python3.6/site-packages/poetry/utils/env.py", line 520, in create_venv
    self._poetry.package.python_versions, python_minor
```
```
» which python3.8
/home/jobevers/code/pyenv/shims/python3.8
```

Oh, I know what's going on, this is a bug in how Poetry gets the patch version of the executable. Basically, it uses the current Python for the patch version instead of the one of the passed executable.

This should be an easy fix.

Resolving patch:
diff --git a/poetry/utils/env.py b/poetry/utils/env.py
--- a/poetry/utils/env.py
+++ b/poetry/utils/env.py
@@ -494,18 +494,19 @@ def create_venv(
         python_patch = ".".join([str(v) for v in sys.version_info[:3]])
         python_minor = ".".join([str(v) for v in sys.version_info[:2]])
         if executable:
-            python_minor = decode(
+            python_patch = decode(
                 subprocess.check_output(
                     " ".join(
                         [
                             executable,
                             "-c",
-                            "\"import sys; print('.'.join([str(s) for s in sys.version_info[:2]]))\"",
+                            "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
                         ]
                     ),
                     shell=True,
                 ).strip()
             )
+            python_minor = ".".join(python_patch.split(".")[:2])
 
         supported_python = self._poetry.package.python_constraint
         if not supported_python.allows(Version.parse(python_patch)):
@@ -517,7 +518,7 @@ def create_venv(
             # Otherwise, we try to find a compatible Python version.
             if executable:
                 raise NoCompatiblePythonVersionFound(
-                    self._poetry.package.python_versions, python_minor
+                    self._poetry.package.python_versions, python_patch
                 )
 
             io.write_line(


Test patch:
diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py
--- a/tests/utils/test_env.py
+++ b/tests/utils/test_env.py
@@ -700,6 +700,45 @@ def test_create_venv_uses_patch_version_to_detect_compatibility(
     )
 
 
+def test_create_venv_uses_patch_version_to_detect_compatibility_with_executable(
+    manager, poetry, config, mocker
+):
+    if "VIRTUAL_ENV" in os.environ:
+        del os.environ["VIRTUAL_ENV"]
+
+    version = Version.parse(".".join(str(c) for c in sys.version_info[:3]))
+    poetry.package.python_versions = "~{}".format(
+        ".".join(str(c) for c in (version.major, version.minor - 1, 0))
+    )
+    venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent))
+
+    check_output = mocker.patch(
+        "poetry.utils._compat.subprocess.check_output",
+        side_effect=check_output_wrapper(
+            Version.parse("{}.{}.0".format(version.major, version.minor - 1))
+        ),
+    )
+    m = mocker.patch(
+        "poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
+    )
+
+    manager.create_venv(
+        NullIO(), executable="python{}.{}".format(version.major, version.minor - 1)
+    )
+
+    assert check_output.called
+    m.assert_called_with(
+        str(
+            Path(
+                "/foo/virtualenvs/{}-py{}.{}".format(
+                    venv_name, version.major, version.minor - 1
+                )
+            )
+        ),
+        executable="python{}.{}".format(version.major, version.minor - 1),
+    )
+
+
 def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
     manager, poetry, config, tmp_dir, mocker
 ):