microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.32k stars 1.19k forks source link

Cannot run or debug python files: An Invalid Python interpreter is selected #23878

Open Nika-St opened 3 months ago

Nika-St commented 3 months ago

VS Code Version: 1.91.1 Local OS Version: Windows_NT x64 10.0.22631 Remote OS Version: SUSE Linux Enterprise Server (SLES) , version 15-SP3 I develop on a remote server using the VSCode extension: Remote Development v0.25.0 Python extension: v2024.10.0 Python Debugger extension: v2024.8.0

I cannot run or debug python files. Keep getting the error: "An Invalid Python interpreter is selected, please try changing it to enable features such as IntelliSense, linting, and debugging. See output for more details regarding why the interpreter is invalid."

Steps to Reproduce:

  1. export PATH=/python/Python-3.11.9.build/bin:$PATH
  2. create and activate a venv
  3. my settings.json in the .vscode dir in the repo: {"python.defaultInterpreterPath": "/bin/python",}
  4. try to run a python file in the repo using F5 or the 'Run Python File' button
  5. Error: "An Invalid Python interpreter is selected, please try changing it to enable features such as IntelliSense, linting, and debugging. See output for more details regarding why the interpreter is invalid."

After selecting the path to my venv in the UI, the same outcome repeats over and over

paulacamargo25 commented 3 months ago

Thanks for the bug report, what is the value that is shown for the interpreter in the bottom right bar?

Nika-St commented 3 months ago

Thanks for getting back, Paula! It shows 'Select Interpreter', and when I press on it, I only see the global python installations (not the locally installed one) and my venv set in "python.defaultInterpreterPath". When I select any of them, I get the error Vscode

|My venv, created off of the locally installed version, is active in terminal and runs without problems

paulacamargo25 commented 3 months ago

Thanks for your reply, it looks like an error in how the Python Extension select the interpreter, for some reason it is not taking your path as valid. Sending back to the Python Extension.

anthonykim1 commented 3 months ago

Hello @Nika-St Thank you for filing the issue. Can you please post

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Would love to dig into this further. Thanks.

Nika-St commented 3 months ago

Hi @anthonykim1 , thanks for responding! Now I'm away for a few weeks, will comment as soon as I'm back!

YaoJusheng commented 2 months ago

I ran into a similar but not identical problem.

After I create a virtual environment using the python version installed by pyenv:

~/.pyenv/versions/3.10.12/bin/python -m venv ~/Desktop/.venv/test-3.10.12 

No matter how many times I select the virtual environment in the selector, it will eventually show that the path to the interpreter is the python version under pyenv, not the created virtual environment.

image

This is the print information:

image

This is quite frustrating, any suggestions on how to solve this problem?

anthonykim1 commented 2 months ago

@YaoJusheng Are you also on a remote scenario?

Can you guys add:

 "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

in your settings.json and see if you can repro the same problem?

Also try search for the native locator setting and change it to js instead of native and give that a try if above doesn't work. Let me know if these things work out. Thanks

YaoJusheng commented 2 months ago

@YaoJusheng Are you also on a remote scenario?

Can you guys add:

 "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

in your settings.json and see if you can repro the same problem?

Also try search for the native locator setting and change it to js instead of native and give that a try if above doesn't work. Let me know if these things work out. Thanks

Thanks for the reply. It is not a remote scenario. I created a new virtual environment test-3.10.12. After testing, I found that the same problem still exists.

$ pyenv versions --skip-aliases 
  system
* 3.8.12 (set by /Users/yaojusheng/.pyenv/version)
  3.10.12
  pypy3.10-7.3.16

$ ~/.pyenv/versions/3.10.12/bin/python -m venv ~/CodeSpace/.venv/test-3.10.12

But when I copy the python interpreter under pyenv to the virtual environment, it appears that python can be recognized as Venv normally, and the virtual environment is loaded correctly.

$ rm ~/CodeSpace/.venv/test-3.10.12/bin/python
$ cp ~/.pyenv/versions/3.10.12/bin/python ~/CodeSpace/.venv/test-3.10.12/bin

After selecting, Output -> Python:

2024-08-27 01:06:03.184 [info] Running installed packages checker:  {
  id: '/Users/yaojusheng/CodeSpace/.venv/test-3.10.12/bin/python',
  sysPrefix: '/Users/yaojusheng/CodeSpace/.venv/test-3.10.12',
  envType: 'Venv',
  envName: 'test-3.10.12',
  envPath: '/Users/yaojusheng/CodeSpace/.venv/test-3.10.12',
  path: '/Users/yaojusheng/CodeSpace/.venv/test-3.10.12/bin/python',
  architecture: 3,
  sysVersion: '3.10.12 (main, Sep 23 2023, 04:09:33) [Clang 15.0.0 (clang-1500.0.40.1)]',
  version: {
    raw: '3.10.12',
    major: 3,
    minor: 10,
    patch: 12,
    build: [],
    prerelease: [ 'final', '0' ]
  },
  displayName: "Python 3.10.12 ('test-3.10.12')",
  detailedDisplayName: "Python 3.10.12 ('test-3.10.12': venv)",
  type: 'Virtual'
} 

If select python3, it will be recognized as Pyenv, but the virtual environment will not be loaded correctly. Instead, it will be recognized as the python version under pyenv.

After selecting, Output -> Python:

2024-08-27 01:06:54.481 [info] Running installed packages checker:  {
  id: '/Users/yaojusheng/.pyenv/versions/3.10.12/bin/python3.10',
  sysPrefix: '/Users/yaojusheng/.pyenv/versions/3.10.12',
  envType: 'Pyenv',
  envName: '3.10.12',
  envPath: '/Users/yaojusheng/.pyenv/versions/3.10.12',
  path: '/Users/yaojusheng/.pyenv/versions/3.10.12/bin/python3.10',
  architecture: 3,
  sysVersion: '3.10.12 (main, Sep 23 2023, 04:09:33) [Clang 15.0.0 (clang-1500.0.40.1)]',
  version: {
    raw: '3.10.12',
    major: 3,
    minor: 10,
    patch: 12,
    build: [],
    prerelease: [ 'final', '0' ]
  },
  displayName: "Python 3.10.12 64-bit ('3.10.12')",
  detailedDisplayName: "Python 3.10.12 64-bit ('3.10.12': pyenv)",
  type: undefined
}

This is really weird, it can't seem to find the correct location automatically.

anthonykim1 commented 2 months ago

@Nika-St Can you try modifying '{"python.defaultInterpreterPath": "/bin/python",}' to point to absolute path to your Python rather than relative?

anthonykim1 commented 2 months ago

@YaoJusheng Thanks for the input here.

  1. I see "~/CodeSpace/.venv/test-3.10.12" Are you on codespaces?
  2. Which operating system and shell type are you using?
  3. Are you setting anything special in your shell initialization file?
Ha-Yen commented 1 month ago

I am currently facing the same issue.

I have cloned repo from my GitHub to my laptop and whenever i activate venv i have created on my PC the interpreter does not change to .\venv\Scripts\python.exe

When I select it manually I recieve an error "An Invalid Python interpreter is seleceted"

Output:

Identifier for virt-virtualenv failed to identify ...\venv\Scripts\python.exe [Error: ENOENT: no such file or directory, scandir '...\venv\Scripts'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '...\\venv\\Scripts'
}

Same thing happens when setting "python.defaultInterpreterPath" to absolute path of venv's python.exe

anthonykim1 commented 1 month ago

@Ha-Yen Does relative path (in workspace stand point) to your venv work?

Nika-St commented 1 month ago

"python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

Thanks for your reply @anthonykim1 ! This didn't change anything

Nika-St commented 1 month ago

@Nika-St Can you try modifying '{"python.defaultInterpreterPath": "/bin/python",}' to point to absolute path to your Python rather than relative?

Neither did this: I actually have the absolute path there, just didn't post the complete text here

Nika-St commented 1 month ago

Hello @Nika-St Thank you for filing the issue. Can you please post

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python) Would love to dig into this further. Thanks.

This shows:

2024-09-17 16:18:51.768 [info] > [path to venv]/bin/python -I ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:18:51.770 [warning] Failed to check if [path to venv]/bin/python is an executable [Error: ENOENT: no such file or directory, lstat '[path to venv]/bin/python'] { errno: -2, code: 'ENOENT', syscall: 'lstat', path: '[path to venv]/bin/python' } 2024-09-17 16:18:51.772 [warning] Failed to check if [path to venv]/bin/python is an executable [Error: ENOENT: no such file or directory, lstat '[path to venv]/bin/python'] { errno: -2, code: 'ENOENT', syscall: 'lstat', path: '[path to venv]/bin/python' } 2024-09-17 16:18:51.772 [warning] Failed to check if [path to venv]/bin/python is an executable [Error: ENOENT: no such file or directory, lstat '[path to venv]/bin/python'] { errno: -2, code: 'ENOENT', syscall: 'lstat', path: '[path to venv]/bin/python' } 2024-09-17 16:18:51.772 [error] Error: Command failed: [path to venv]/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py /bin/sh: [path to venv]/bin/python: No such file or directory

at genericNodeError (node:internal/errors:984:15)
at wrappedFn (node:internal/errors:538:14)
at ChildProcess.exithandler (node:child_process:423:12)
at ChildProcess.emit (node:events:531:35)
at maybeClose (node:internal/child_process:1105:16)
at Socket.<anonymous> (node:internal/child_process:457:11)
at Socket.emit (node:events:519:28)
at Pipe.<anonymous> (node:net:338:12) {

code: 127, killed: false, signal: null, cmd: '[path to venv]/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py' }

And also this:

2024-09-17 16:16:55.612 [info] Found: [path to venv]/bin/python3.11 --> [path]/python/Python-3.11.9.build/bin/python3.11 2024-09-17 16:16:55.614 [info] Found: [path]/python/Python-3.11.9.build/bin/python3 --> [path]/python/Python-3.11.9.build/bin/python3.11 2024-09-17 16:16:55.614 [info] Found: [path]/python/Python-3.11.9.build/bin/python3.11 --> [path]/python/Python-3.11.9.build/bin/python3.11 2024-09-17 16:16:55.624 [info] Found: /usr/bin/X11/python --> /usr/bin/X11/python2.7 2024-09-17 16:16:55.624 [info] Found: /usr/bin/X11/python2 --> /usr/bin/X11/python2.7 2024-09-17 16:16:55.625 [info] Found: /usr/bin/X11/python2.7 --> /usr/bin/X11/python2.7 2024-09-17 16:16:55.626 [info] Found: /usr/bin/X11/python3 --> /usr/bin/X11/python3.4 2024-09-17 16:16:55.626 [info] Found: /usr/bin/X11/python3.4 --> /usr/bin/X11/python3.4 2024-09-17 16:16:55.649 [info] > /usr/bin/python -I ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:16:55.655 [warning] Error: Command failed: /usr/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py Unknown option: -I usage: /usr/bin/python [option] ... [-c cmd | -m mod | file | -] [arg] ... Try `python -h' for more information.

at genericNodeError (node:internal/errors:984:15)
at wrappedFn (node:internal/errors:538:14)
at ChildProcess.exithandler (node:child_process:423:12)
at ChildProcess.emit (node:events:531:35)
at maybeClose (node:internal/child_process:1105:16)
at Socket.<anonymous> (node:internal/child_process:457:11)
at Socket.emit (node:events:519:28)
at Pipe.<anonymous> (node:net:338:12) {

code: 2, killed: false, signal: null, cmd: '/usr/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py' } 2024-09-17 16:16:55.655 [error] Support for Python 2.7 has been dropped by the Python extension so certain features may not work, upgrade to using Python 3. 2024-09-17 16:16:55.668 [info] > /usr/bin/python ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:16:55.687 [info] > /usr/bin/python3 -I ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:16:55.714 [info] > /usr/lib64/python -I ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:16:55.724 [error] Error: Command failed: /usr/lib64/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py /bin/sh: /usr/lib64/python: Is a directory

at genericNodeError (node:internal/errors:984:15)
at wrappedFn (node:internal/errors:538:14)
at ChildProcess.exithandler (node:child_process:423:12)
at ChildProcess.emit (node:events:531:35)
at maybeClose (node:internal/child_process:1105:16)
at Socket.<anonymous> (node:internal/child_process:457:11)
at Socket.emit (node:events:519:28)
at Pipe.<anonymous> (node:net:338:12) {

code: 126, killed: false, signal: null, cmd: '/usr/lib64/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py' } 2024-09-17 16:16:55.744 [info] > [path to venv]/bin/python -I ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py 2024-09-17 16:16:55.747 [error] Error: Command failed: [path to venv]/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py [path to venv]/bin/python: /lib64/libm.so.6: version GLIBC_2.29' not found (required by [path to venv]/bin/python) [path to venv]/bin/python: /lib64/noelision/libpthread.so.0: versionGLIBC_2.30' not found (required by [path to venv]/bin/python) [path to venv]/bin/python: /lib64/libc.so.6: version GLIBC_2.28' not found (required by [path to venv]/bin/python) [path to venv]/bin/python: /lib64/libc.so.6: versionGLIBC_2.25' not found (required by [path to venv]/bin/python) [path to venv]/bin/python: /lib64/libc.so.6: version GLIBC_2.26' not found (required by [path to venv]/bin/python) [path to venv]/bin/python: /lib64/libc.so.6: versionGLIBC_2.27' not found (required by [path to venv]/bin/python)

at genericNodeError (node:internal/errors:984:15)
at wrappedFn (node:internal/errors:538:14)
at ChildProcess.exithandler (node:child_process:423:12)
at ChildProcess.emit (node:events:531:35)
at maybeClose (node:internal/child_process:1105:16)
at Socket.<anonymous> (node:internal/child_process:457:11)
at Socket.emit (node:events:519:28)
at Pipe.<anonymous> (node:net:338:12) {

code: 1, killed: false, signal: null, cmd: '[path to venv]/bin/python -I [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/get_output_via_markers.py [path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py' } 2024-09-17 16:16:55.772 [info] Finished searching for interpreters in posix paths locator: 268 milliseconds

Ha-Yen commented 1 month ago

Yeah venv was pointing out to absolute path. I have changed to relative but interpreter was still not working.

Only work around I have found is excluding venv from git. Creating requirements.txt and creating new venv on other machine where I used requirements.txt to install all packages that were installed in original venv.

Nika-St commented 1 month ago

Hi @anthonykim1, any updates? :)

anthonykim1 commented 4 weeks ago

Interesting... @Nika-St Have you tried to venv path setting?

Screenshot 2024-10-05 at 7 42 36 PM
Nika-St commented 3 weeks ago

@anthonykim1 , oh I have...

anthonykim1 commented 3 weeks ago

I see.. I cannot repro this - Two things that remain suspicious to me are:

[path]/.vscode-server/extensions/ms-python.python-2024.10.0-linux-x64/python_files/interpreterInfo.py
/bin/sh: /usr/lib64/python: Is a directory

Is your .../python a directory? because python extension is expecting an executable

2024-09-17 16:18:51.770 [warning] Failed to check if [path to venv]/bin/python is an executable [Error: ENOENT: no such file or directory, lstat '[path to venv]/bin/python'] {
errno: -2,
code: 'ENOENT',
syscall: 'lstat',
path: '[path to venv]/bin/python'
}

I'm not sure where this [path to venv] is coming from. From extension standpoint its the file "src/client/pythonEnvironments/base/locators/composite/envsResolver.ts" but I do not see [path to venv] coming from extension.

Nika-St commented 3 weeks ago

@anthonykim1 , just to disambiguate: [path] stands for my home dir, i. e. "/h/[username]" and [path to venv] stands for my venv, i. e. "/h/[username]/venvs/cv/bin/python3"

It shouldn't even look under /usr/lib64/python, because I created the venv off of a python version installed for my user, and not globally, and the path to it is set wherever available:

I have path to venv in "python.defaultInterpreterPath": "/h/[username]/venvs/cv/bin/python3", "python.pythonPath": "/h/[username]/venvs/cv/bin/python3", (looks deprecated) "python.venvPath": "/h/[username]/venvs/",

Python: Venv Path is defined on the remote, Python: Default Interpreter Path is defined on the remote, for the workspace and folder

anthonykim1 commented 3 weeks ago

because I created the venv off of a python version installed for my user, and not globally, and the path to it is set wherever available:

This would actually be incorrect because you have path such as: "/h/[username]/venvs/cv/bin/python3",

Ideally you should create virtual environment INSIDE and dedicated for your workspace. Give these a try if you haven't already - create environment command and select the interpreter with the interpreter selector. Image

Image

Nika-St commented 3 weeks ago

Tried unsuccessfully: 2024-10-11 15:06:09.104 [info] Selected interpreter /h/denistr1/python/Python-3.12.0/python for creating virtual environment. 2024-10-11 15:06:28.292 [info] Running Env creation script: [ '/h/denistr1/python/Python-3.12.0/python', '/h/denistr1/.vscode-server/extensions/ms-python.python-2024.16.0-linux-x64/python_files/create_venv.py', '--git-ignore' ] 2024-10-11 15:06:28.292 [info] > ~/python/Python-3.12.0/python ~/.vscode-server/extensions/ms-python.python-2024.16.0-linux-x64/python_files/create_venv.py --git-ignore 2024-10-11 15:06:28.292 [info] cwd: . 2024-10-11 15:06:28.295 [info] /h/denistr1/python/Python-3.12.0/python: /lib64/libm.so.6: version GLIBC_2.29' not found (required by /h/denistr1/python/Python-3.12.0/python) /h/denistr1/python/Python-3.12.0/python: /lib64/noelision/libpthread.so.0: versionGLIBC_2.30' not found (required by /h/denistr1/python/Python-3.12.0/python) /h/denistr1/python/Python-3.12.0/python: /lib64/libc.so.6: version GLIBC_2.28' not found (required by /h/denistr1/python/Python-3.12.0/python) /h/denistr1/python/Python-3.12.0/python: /lib64/libc.so.6: versionGLIBC_2.25' not found (required by /h/denistr1/python/Python-3.12.0/python) /h/denistr1/python/Python-3.12.0/python: /lib64/libc.so.6: version GLIBC_2.26' not found (required by /h/denistr1/python/Python-3.12.0/python) /h/denistr1/python/Python-3.12.0/python: /lib64/libc.so.6: versionGLIBC_2.27' not found (required by /h/denistr1/python/Python-3.12.0/python) 2024-10-11 15:06:28.295 [error] Error while running venv creation script: undefined 2024-10-11 15:06:28.295 [error] Failed to create virtual environment with exitCode: 1

Nika-St commented 3 weeks ago

because I created the venv off of a python version installed for my user, and not globally, and the path to it is set wherever available:

This would actually be incorrect because you have path such as: "/h/[username]/venvs/cv/bin/python3",

What's incorrect @anthonykim1 ?

anthonykim1 commented 1 week ago

What's incorrect @anthonykim1 ?

Meaning "because I created the venv off of a python version installed for my user, and not globally, and the path to it is set wherever available:" is off in a way that is your "/h/[username]/venvs/cv/bin/python3" remains global. If it were to be non-global, it would not be under your such path and under venvs, but rather will be under your project workspace directory.

More common way of non-global environment would be per-workspace virtual environment where you would have virtual environment under your project workspace directory.

anthonykim1 commented 1 week ago

Also is /python/Python-3.12.0/python the very first Python folder where you store multiple Python versions? It may be helpful for you to rename the very first Python word to something else usually "python" would be the executable itself.