posit-dev / publisher

MIT License
3 stars 0 forks source link

Use configured python #1727

Closed mmarchetti closed 2 months ago

mmarchetti commented 2 months ago

Intent

This PR gets the Python interpreter from the VSCode/Positron configuration, and passes that to the backend for use in project inspection and requirements scanning.

Fixes #1514

Type of Change

Approach

Get the configuration key python.defaultInterpreterPath. Perform variable substitution if needed (there is no VSCode API for this yet; see https://github.com/microsoft/vscode/issues/46471.

Automated Tests

The backend could use an API test case that includes passing the python path.

Directions for Reviewers

Test in Positron and VSCode. This especially needs testing on Windows.

In Positron: have multiple Python installations on your system, via conda, pyenv, or whatever. Select one of them from the interpreter menu at the upper right (click the ... menu next to the running Python interpreter). Run inspection and scanning and verify the resulting config and requirements file. Then activate a different interpret and verify functionality again.

In VSCode, configure the Python interpreter path to point to the one you want (it should not be the default Python on your PATH). Inspect your project and see that the correct version is in the resulting config file. Install any Python packages your project needs into the configured Python installation, then Scan Python dependencies in the extension. It should find the dependency versions in the selected Python library.

mmarchetti commented 2 months ago

What's in the Python interpreter configuration field in VSCode? Does it just say python?

tdstein commented 2 months ago

What's in the Python interpreter configuration field in VSCode? Does it just say python?

Yes

mmarchetti commented 2 months ago

Yes

I see, I'll handle that case.

mmarchetti commented 2 months ago

I'm guessing we'll have to do the same for R Version in a separate PR?

Yes. The change was more critical for Python because it's normal to have mutliple python installations (for example, in every virtual environment). For R, you can usually have a single R installation and then have a local renv library for each project (without a separate executable).

kgartland-rstudio commented 2 months ago

This doesn't appear to be working on Windows. Let me know if this is expected:

Selecting Interpreter: I select my Python Interpreter:

Then I create a Configuration and it uses the default Python (3.11.5).

Logs:

time=2024-05-28T14:01:34.838-04:00 level=INFO msg="Detecting deployment type and entrypoint..." time=2024-05-28T14:01:34.844-04:00 level=INFO msg="Possible deployment type" Entrypoint=app.py Type=python-streamlit time=2024-05-28T14:01:34.844-04:00 level=INFO msg="Looking for Python on PATH" PATH="C:\Program Files\Parallels\Parallels Tools\Applications;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Users\kgartland\publisher-client\mingw64\bin;C:\Users\kgartland\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\kgartland\AppData\Local\Programs\Python\Python311\;C:\Users\kgartland\AppData\Local\Microsoft\WindowsApps;C:\Users\kgartland\AppData\Local\Microsoft\WinGet\Packages\Casey.Just_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\kgartland\AppData\Roaming\npm;C:\Python311;C:\Users\kgartland\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\kgartland\publisher-client;C:\Users\kgartland\publisher-client\quarto-1.4.549-win\bin;C:\Program Files (x86)\R\R-4.4.0\bin;C:\Users\kgartland\publisher-client\make-3.81-bin\bin;C:\Users\kgartland\publisher-client\python-3.12.3-amd64.exe;" time=2024-05-28T14:01:34.846-04:00 level=DEBUG msg="Running command" cmd=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe args=--version time=2024-05-28T14:01:34.874-04:00 level=INFO msg="Access Log" method=GET url=/api/configurations elapsed_ms=36 status=200 req_size=0 resp_size=2360 client_addr=127.0.0.1:51578 time=2024-05-28T14:01:34.874-04:00 level=INFO msg="Getting Python version" python=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe time=2024-05-28T14:01:34.874-04:00 level=DEBUG msg="Running command" cmd=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe args="-E -c import sys; v = sys.version_info; print(\"%d.%d.%d\" % (v[0], v[1], v[2]))" time=2024-05-28T14:01:34.913-04:00 level=INFO msg="Detected Python" version=3.11.5 time=2024-05-28T14:01:34.913-04:00 level=INFO msg="Using Python packages" source=c:\Users\kgartland\publisher-client\bundles\top-5-income-share-streamlit\requirements.txt time=2024-05-28T14:01:34.913-04:00 level=INFO msg="Access Log" method=POST url=/api/inspect elapsed_ms=74 status=200 req_size=19 resp_size=530 client_addr=127.0.0.1:51579 req.python=python time=2024-05-28T14:01:34.964-04:00 level=INFO msg="Access Log" method=GET url=/api/configurations elapsed_ms=40 status=200 req_size=0 resp_size=2360 client_addr=127.0.0.1:51580

Then I tried creating a Virtual Environment Creating Environment:

Then I go to create a new Configuration and I get 3.11.5 again.

Logs:

time=2024-05-28T14:03:20.169-04:00 level=INFO msg="Access Log" method=GET url=/api/configurations/configuration-1/files elapsed_ms=15 status=200 req_size=0 resp_size=11824 client_addr=127.0.0.1:51588 time=2024-05-28T14:03:37.939-04:00 level=INFO msg="Detecting deployment type and entrypoint..." time=2024-05-28T14:03:37.950-04:00 level=INFO msg="Possible deployment type" Entrypoint=app.py Type=python-streamlit time=2024-05-28T14:03:37.950-04:00 level=INFO msg="Looking for Python on PATH" PATH="C:\Program Files\Parallels\Parallels Tools\Applications;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Users\kgartland\publisher-client\mingw64\bin;C:\Users\kgartland\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\kgartland\AppData\Local\Programs\Python\Python311\;C:\Users\kgartland\AppData\Local\Microsoft\WindowsApps;C:\Users\kgartland\AppData\Local\Microsoft\WinGet\Packages\Casey.Just_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\kgartland\AppData\Roaming\npm;C:\Python311;C:\Users\kgartland\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\kgartland\publisher-client;C:\Users\kgartland\publisher-client\quarto-1.4.549-win\bin;C:\Program Files (x86)\R\R-4.4.0\bin;C:\Users\kgartland\publisher-client\make-3.81-bin\bin;C:\Users\kgartland\publisher-client\python-3.12.3-amd64.exe;" time=2024-05-28T14:03:37.957-04:00 level=DEBUG msg="Running command" cmd=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe args=--version time=2024-05-28T14:03:37.984-04:00 level=INFO msg="Getting Python version" python=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe time=2024-05-28T14:03:37.984-04:00 level=DEBUG msg="Running command" cmd=C:\Users\kgartland\AppData\Local\Programs\Python\Python311\python.exe args="-E -c import sys; v = sys.version_info; print(\"%d.%d.%d\" % (v[0], v[1], v[2]))" time=2024-05-28T14:03:37.999-04:00 level=INFO msg="Access Log" method=GET url=/api/configurations elapsed_ms=63 status=200 req_size=0 resp_size=3146 client_addr=127.0.0.1:51591 time=2024-05-28T14:03:38.068-04:00 level=INFO msg="Detected Python" version=3.11.5 time=2024-05-28T14:03:38.068-04:00 level=INFO msg="Using Python packages" source=c:\Users\kgartland\publisher-client\bundles\top-5-income-share-streamlit\requirements.txt time=2024-05-28T14:03:38.068-04:00 level=INFO msg="Access Log" method=POST url=/api/inspect elapsed_ms=128 status=200 req_size=19 resp_size=530 client_addr=127.0.0.1:51592 req.python=python time=2024-05-28T14:03:38.087-04:00 level=INFO msg="Access Log" method=GET url=/api/configurations elapsed_ms=16 status=200 req_size=0 resp_size=3146 client_addr=127.0.0.1:51593

mmarchetti commented 2 months ago

OK, it turns out that the Python: Select Interpreter command works differently. I have a change that will fix that (it also fails on MacOS). I'll test it in Positron before I push.

kgartland-rstudio commented 2 months ago

Now it works as expected on both MacOS and Windows. I was able to Select an already-existing interpreter from the dropdown, browse for one and manually enter the path for one.

I was able to create a venv and conda environment and use that interpreter as well.

kgartland-rstudio commented 2 months ago

^ working in Linux arm and amd too