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

Support Python Launcher on Windows when using poetry env use and poetry env remove (#3520) #4217

Closed
wants to merge 10 commits into from

Conversation

sitd2813
Copy link

@sitd2813 sitd2813 commented Jun 26, 2021

Pull Request Check List

Resolves: #issue-number-here

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

Fix #3520

I did not add any test because I'm not really familiar with PyTest and thus had no idea how to add Windows specific test. However, the code was built and tested on Windows 10 device and it proved to resolve #3520 without apparent errors.

EDIT

Added test to verify if EnvManager._execute_python_script works as expected.

@sitd2813 sitd2813 closed this Jun 26, 2021
@sitd2813 sitd2813 reopened this Jun 26, 2021
@sonarcloud
Copy link

sonarcloud bot commented Jun 26, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sitd2813 sitd2813 changed the title Support Python Launcher on Windows when using poetry env use (#3520) Support Python Launcher on Windows when using poetry env use and poetry env remove (#3520) Jun 26, 2021
@sonarcloud
Copy link

sonarcloud bot commented Jul 2, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sitd2813
Copy link
Author

sitd2813 commented Jul 2, 2021

Both tests currently failed due to ConnectionError. I have no idea why it occurred. It seems like there is an issue with the testing platform.

)
python_version = execute_python_script(
python,
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about creating a constant for the script "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\""?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for reviewing my code. I added the constant for the script. Also, I made the execute_python_script a member function of EnvManager as it is only used inside it. It is now EnvManager._execute_python_script.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great. Good catch.

Copy link
Author

@sitd2813 sitd2813 Jul 22, 2021

Choose a reason for hiding this comment

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

Is there anything else I need to do? It seems that running more test require approval.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it needs more tests and one more reviewer.

SitD added 6 commits July 22, 2021 01:45
I forgot to delete now unused `execute_python_script`.
Change py launcher detection to be run once when `EnvManager` is created. Also, if both python executable (pythonX.X) and py launcher exist, poetry will prefer python executable over py launcher.
@dalito
Copy link

dalito commented Sep 22, 2021

As a fan of the launcher I have tested your PR with poetry-1.2.0a2 on Win10. It mostly works. 👍

Only when removing an env an error occurs. The env is removed nevertheless. The error is probably due to a "feature" in Win10. When you don't have python on path but enter "python", Win10 suggests to install Python from the Microsoft store. So testing for "python" gives an unexpected output. Maybe it is better to test for the presence of "pip"?

λ poetry env use python3.8
Creating virtualenv mared-NfQ5kqas-py3.8 in C:\Users\zzz\AppData\Local\pypoetry\Cache\virtualenvs
Using virtualenv: C:\Users\zzz\AppData\Local\pypoetry\Cache\virtualenvs\mared-NfQ5kqas-py3.8

λ poetry env remove python3.8

  CalledProcessError

  Command 'python -W ignore -' returned non-zero exit status 9009.

  at C:\dev\Python39\lib\subprocess.py:528 in run
       524│             # We don't call process.wait() as .__exit__ does that for us.
       525│             raise
       526│         retcode = process.poll()
       527│         if check and retcode:
    →  528│             raise CalledProcessError(retcode, process.args,
       529│                                      output=stdout, stderr=stderr)
       530│     return CompletedProcess(process.args, retcode, stdout, stderr)
       531│
       532│

The following error occurred when trying to handle this error:


  EnvCommandError

  Command python -W ignore - errored with the following return code 9009, and output:
  Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640input was : import sys

  if hasattr(sys, "real_prefix"):
      print(sys.real_prefix)
  elif hasattr(sys, "base_prefix"):
      print(sys.base_prefix)
  else:
      print(sys.prefix)


  at ~\_poetry\venv\lib\site-packages\poetry\utils\env.py:1285 in _run
      1281│                 output = subprocess.check_output(
      1282│                     cmd, stderr=subprocess.STDOUT, env=env, **kwargs
      1283│                 )
      1284│         except CalledProcessError as e:
    → 1285│             raise EnvCommandError(e, input=input_)
      1286│
      1287│         return decode(output)
      1288│
      1289│     def execute(self, bin: str, *args: str, **kwargs: Any) -> Optional[int]:

@sitd2813
Copy link
Author

sitd2813 commented Sep 28, 2021

There are some issues that need discussions which are:

  1. When I type poetry env use python3.9 or any similar command, should poetry prefer py -3.9 or python3.9 or python (3.9) given that all 3 commands exist in the system?
  2. Due to how poetry detects the python executable in the system, py launcher detection will produce long unintuitive error messages.
  3. As @dalito reported, it seems that Windows' python alias also causes problems.

I think a new issue elaborating the behavior should be created.

@neersighted
Copy link
Member

The current consensus is that we should use pythonfinder instead of trying to write out this logic ourselves. Feel free to re-open this PR if you'd like to switch to that approach -- otherwise, @bmarroquin has expressed interest in PRing pythonfinder support.

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.

Natively support py launcher on Windows
4 participants