Python Forum

Full Version: VSCode can't select interpreter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been fighting this for a week (or more) now, without success.
I am running a virtual environment, and the terminal shows proper python, etc.
however when I try to run by choosing 'Run python file in terminal' The virtual environment interpreter is not being used, I know this as it is saying a package isn't installed, but it is (in venv).
If I try to change interpreter from command palette I get 'cannot read property map of undefined'.

looking on the VSCode site, others are having the same problem, but none of the suggested solutions work.
I have deleted all extensions except for python to no avail.

This is really slowing me down, and I'm at a loss as to a remedy.
I'm thinking there's probably something that needs to be modified in settings, but don't know what that might be.

Any ideas?

Snippsat,
In your VSCode tutorial, you advise setting:
"python": "$pythonPath $fullFileName",
in code-runner.executorMap json

Now I have done that, but do I have to manually set that path in my environment, or is this something that VSCode will automatically set to my virtual environment?
I ask because the virtual environment appears not to be set.
I am not using code-runner plugin. I select the python interpreter by Cntr+Shift+P and selecting the interpreter for the virtual environment I have. Then it works fine when use Run python file in terminal
No need to activate the environment in the terminal if you are just going to run the file. Of course if you also plan to write commands (e.g. install something via pip in the environment) then it's another story
Quote:I select the python interpreter by Cntr+Shift+P and selecting the interpreter for the virtual environment I have
This worked for me until I updated. Now when I use Ctrl+Shift+P, I see 'Python: Select Interpreter', when I click on that, I get the error 'Cannot read property of undefined'. So cannot set up virtual environment.
That's why I ask about "python": "$pythonPath $fullFileName", as this looks lake a possibility (or grasp for the ring). So now to use the virtual environment, since I can't use 'Run python file in terminal', I manually switch to the terminal (which I have activated in the virtual environment) and run python from bash. Pain in the butt as I so liked the shortcut, but it works.
(Oct-24-2018, 03:42 PM)Larz60+ Wrote: [ -> ]Now when I use Ctrl+Shift+P, I see 'Python: Select Interpreter', when I click on that, I get the error 'Cannot read property of undefined'
Then you have a error somewhere.
Look at Select an environment
Look your setting.json.

Search for error or Report it.
Clean install
Quote:Look at Select an environment
Have done this,
Quote:After using Python: Select Interpreter
point is I cannot use Python: Select Interpreter I get the error 'Cannot read property of undefined'

I have completely removed code several times, including deleting the .vscode directory completely.
Have followed you instructions on VSCode from start completely (which has always worked before.
Still get same results.

There are other odd things happening with openSuse (which is now somewhat suspect) such as inability to install pyenv 3.7.0. I am ready to try another distro, but reluctant because of the amount of setup pains required.
I am going to attempt one more complete reload of VSCode.
Quote:Search for error or Report it.
As stated earlier, this error has already been reported (by others), but no clear resolution to date.
I re-installed VSCode one more time and finally everything works.
There are a few things that I did differently, one of these was the answer, but I can't say which, I'm just glad that all is working now:

1. I restored code-runner.executorMap to default values, then changes python entry to:
"python": "$pythonPath $fullFileName"

2. before reloading code, I deleted anything in /tmp related to VSCode.
I am suspecting that this was the part that needed to be done.

3. This is not new as I have been doing it all along, so don't suspect it.
Delete entire .vscode directory in /home/user

I will post this on the VSCode github page as well.
New information:
The above is still valid for re-installation, and I have been running without issue until yesterday, when the original:
Quote:however when I try to run by choosing 'Run python file in terminal' The virtual environment interpreter is not being used, I know this as it is saying a package isn't installed, but it is (in venv)
issue reoccurred. After a lot of digging I found something very subtle was causing my problem:

here's the sequence of events that caused the issue: when switching from project to project (all with virtual environments), occasionally the compiler used when i chose 'run python file in terminal' was not synced with the virtual environment.

  1. Close project 1 (virtual environment) using 'File --> Close Folder' to close the the first session (while terminal window still open)
  2. exit VSCode using 'File --> Exit'.
  3. (sometime later, but during same login session) Open VSCode from GNOME icon.
  4. Select project 2 from welcome menu, using shortcut. Upon opening, the terminal window automatically opens (this was the state I left it upon exit from project 1), but it's in the proper directory for project 2, however it's not using the proper virtual environment.
  5. From terminal window, I issue command to start venv: . ./venv/bin/activate. The virtual environment starts, and I see python -V using it and also see pip -V using it, so I feel ebevrthings ok with the terminal.
  6. Using command palette, I select the proper interpreter (or so I think) by selecting (cached)./venv/bin/python.
  7. Everything appears OK.

some time later:
  1. I try compiling using 'Run python file in terminal', and get an error message that I am missing a package.
  2. Switch to terminal window and type 'pip list'. The package is there.

So what went wrong?
It appears that the sequence of events needs to be modified as a work around for what appears to be a subtle bug.
  1. Whenever exiting project by using 'File --> Close Folder', first close terminal window.
  2. Open project 2 (from welcome menu shortcut). If you forgot to close terminal window from previous session, close now.
  3. Select the python interpreter for current session.
  4. Now, only after selecting python interpreter, open terminal window, using either View --> Terminal' or 'Run python file in terminal'.
  5. all really will bo OK now.
I use same name for all of my virtual environments, and that probably has something to do with the problem. I will update the github VSCode posting I made to reflect the new observations, in the meantime, following the final sequence above will suffice.
Still causing issues:

Snippsat wrote:
Quote:Then you have a error somewhere.
Look at Select an environment
Look your setting.json.

  • Cannot use python: select interpreter,
    get error:
    Error:
    expected a string, got undefined
  • I have re-installed VSCode no less than five times.
    removed ./config/Code
    removed ./vscode
    removed any vscode references in \tmp
    searched \etc for any -- did not find entry

    Does not do the trick
    Snippsat, used your VSCode from start -- followed each and every step, without success:

  • ~/.config/Code/User/settings.json =
    {
    "php": "php",
    "python": "$pythonPath $fullFileName",
    "perl": "perl",
    "code-runner.runInTerminal": true,
    "terminal.integrated.scrollback": 50000
    }
  • $pythonPath $fullFileName -- Both environment variables are empty.
  • can manually start my virtual environment, and run python from command line,
    python -V and pip -V show proper versions, which python a,d which pip both show proper location in virtual environment bin location
  • I can run python by using run button, but not by right clicking in code and selecting run code in terminal. The python version being run (from run button) is wrong, not my virtual version.

Manual start virtual environment
Output:
larz60p@Larz60p:~/.config/Code/User> cd .../CIA-FactBook/ larz60p@Larz60p:.../CIA-FactBook> . ./CIA-Factbook_venv/bin/activate (CIA-Factbook_venv) larz60p@Larz60p:.../CIA-FactBook> which python .../CIA-FactBook/CIA-Factbook_venv/bin/python (CIA-Factbook_venv) larz60p@Larz60p:.../CIA-FactBook> python -V Python 3.6.7 (CIA-Factbook_venv) larz60p@Larz60p:.../CIA-FactBook> which pip .../CIA-FactBook/CIA-Factbook_venv/bin/pip (CIA-Factbook_venv) larz60p@Larz60p:.../CIA-FactBook> pip -V pip 18.1 from .../CIA-FactBook/CIA-Factbook_venv/lib/python3.6/site-packages/pip (python 3.6) (CIA-Factbook_venv) larz60p@Larz60p:.../CIA-FactBook> Python 3.6.7
the thing that bothers me the most is that the environment variables pythonPath and fullFileName are not being populated.
where is that supposed to occur? Is it something that I am supposed to set in my virtual environment? or point to something in .pyenv?
Ok,
I'm back up and running again, at least for now.
Totally removed VSCode
... and totally removed pyenv.
found an Opensuse install for pyenv here: https://software.opensuse.org/download.h...kage=pyenv
I installed that. It is not as up to date as snippsat's method, but it's a version set up for OpenSuse.
Then reinstalled VSCode (which I think had nothing to do with the cause of my issues)
For now all is working well.