Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check current python executable version #4520

Merged

Conversation

yokomotod
Copy link
Contributor

Hi.

As #3426 said, current poetry only checks the python constraint in pyproject.toml when creating a virtualenv.

This PR added a python version check for created virtualenv as well, at each command execution.

Demo

(Supporse python 3.8 and 3.9 are installed)

First, poetry install with pyproject.tom which requires python3.8:

[tool.poetry]
name = "ptry"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]

[tool.poetry.dependencies]
python = "~3.8"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

It creates virtualenv with python 3.8.x executable.

$ poetry run python -V
Python 3.8.10

Then change pyproject.tom to python = "^3.9".

With this PR, now poetry install aborts with error message:


  InvalidCurrentPythonVersionError

  Current Python version (3.8.10) is not allowed by the project (^3.9).
  Please change python executable via the "env use" command.

  at ~/.poetry/lib/poetry/utils/env.py:651 in create_venv
       647│             current_python = Version.parse(
       648│                 ".".join(str(c) for c in env.version_info[:3])
       649│             )
       650│             if not self._poetry.package.python_constraint.allows(current_python):
    →  651│                 raise InvalidCurrentPythonVersionError(
       652│                     self._poetry.package.python_versions, current_python
       653│                 )
       654│             return env
       655│ 

As above message suggests, by running poetry env use python3.9 , poetry install will work again.

I expect this PR covers all commands which should check python version, like uninstall, run, shell etc.

Pull Request Check List

Resolves: #3426

  • Added tests for changed code.
  • [ ] Updated documentation for changed code.

@edmorley
Copy link

edmorley commented Feb 2, 2022

I was about to open a new issue for this. Do you know if you'll have a chance to fix the tests on this PR? :-)

@finswimmer
Copy link
Member

Hey @yokomotod,

could you please rebase your PR to resolve the conflict?

Thanks a lot for your contribution 👍

fin swimmer

@yokomotod
Copy link
Contributor Author

@finswimmer @edmorley done!

@finswimmer finswimmer requested a review from a team February 24, 2022 10:38
Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nitpick. Otherwise looks good.

)
if not self._poetry.package.python_constraint.allows(current_python):
raise InvalidCurrentPythonVersionError(
self._poetry.package.python_versions, current_python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_python is of type Version, but the type hint in InvalidCurrentPythonVersionError says it should be a string. Thus, either the type hint has to be changed or current_python should be cast to string explicity str(current_python). I tend toward the latter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true.

Suggested change
self._poetry.package.python_versions, current_python
self._poetry.package.python_versions, str(current_python)

@yokomotod
Copy link
Contributor Author

@radoering Thank you for the review. Can we merge then?

@radoering
Copy link
Member

The tests have to be triggered and succeed first. Unfortunately, I have neither the permissions to trigger the tests nor to merge.

@finswimmer ?

Copy link
Member

@finswimmer finswimmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot 👍 🏅

Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid python version not detected
4 participants