Code-Runner Extension Refuses to Use the Correct Environment - visual-studio-code

I'm having a bit of trouble getting code-runner to play nice with my conda environments.
Checklist;
The correct python interpreter is selected.
I've explicitly changed the pythonPath and executorMap objects in the settings.json file to the correct environment.
I've tried reinstalling VSCode, Conda and the Code-Runner extension.
I've run a quick script to check which environment is being used and it confirmed that code-runner insists on using the base environment, rather than the one selected, as shown below.
Just to clarify, the code runs perfectly fine, and shows the correct environment selected when I use Ctrl+F5 instead of code-runner (or when I uninstall code-runner and use the normal run feature), but I'd like for it to work with the extension too.
Please help, thanks in advance!

Related

Elixir: VS Code ExUnit cannot find Mix

I cannot load or run my tests, from within VS Code.
I'm a new user to Elixir, and to VS Code. I'm running Lubuntu 21.10 (Impish). I've downloaded Erlang/OTP 25 (.deb), and Elixir 1.14 (precompiled binary in /usr/share/elixir), and can get anything I need running in a Bash terminal. Again, in a standard QTerminal window,
erl, iex, mix, elixir, etc. all work fine.
In VS Code, however, I get some errors. I feel stupid, but I'm coming from Sublime Text, so please forgive me.
In the left pane of VS Code, ExUnit shows an error (red):
Clicking on this error gives me this, on the bottom right pane. The command line options, passed to mix test, seem to be the default configuration:
This result is bizarre to me, because I can open the integrated terminal, execute /bin/sh, and then run the exact mix test line that's displayed:
/usr/share/elixir/bin has been added to my PATH variable, in ~/.bashrc, ~/.profile, and /etc/environment.
However, I am further confused by all tests being excluded, and wonder if there's some connection to the core issue:
Note that I can run my tests just fine, using different command line options. I've tried adding tags, but that didn't fix the problem.
I tried Google'ing this, and played around with my settings. Here is what I have configured in the "User" settings.json, and I made sure nothing overrides this in "Workspace" settings:
Changing the useNativeTesting setting doesn't solve the problem.
On another (?) note, I get a "failed to run elixir" upon VS Code startup:
Again, I have no problem running commands from a Linux terminal, or from a terminal within VS Code.
Plot twist: If I remove the precompiled Elixir 1.14, and downgrade to an older version, via apt, the problem goes away. But Lubuntu 21.10 doesn't offer Elixir 1.14, and I'm really into using the new dbg() feature.
But for now, I cannot load or run my tests, from within VS Code, apparently because Mix cannot be found.
Thanks to Daniel Imms, from the VS Code team, for answering my question on Twitter:
"Try moving where ever you init mix and elixir (.bashrc?) into your .bash_profile and then logging out and in again or restarting. I'm guessing it's in your bashrc which doesn't run in non-interactive sessions like in tasks."

Use micromamba virtual environment in VS Code

We have to use micromamba for our app because conda is prohibitively slow for installing our packages. We use a devcontainer to install micromamba and its packages. This works for the VS Code terminal but the editor still cannot find my packages.
I only see a way to activate the micromamba environment with a shell script snippet or shell rc file. The works for the terminal, but I dont see a way to activate it for the editor processes. The closest setting I found is specific to venvs.
The solution was to add a .env file setting PYTHONPATH to the modules. Then setting "python.envFile" in .vscode/settings.json to point to that .env file.

VSCode doesn't show poetry virtualenvs

VSCode wouldn't show venvs created with poetry in "change kernel" window.
I tried this thread but it didn't work. As well as the topic being 2 years old.
Eventhough I would like to avoid installing all the dependencies in local folder for each project (one of the suggested answers), I still tried it but VSCode doesn't see it either (.venv folder created inside the project).
Right now poetry saves all the venvs in: ~/.cache/pypoetry/virtualenvs after poetry shell any of the venvs. I added it to settings.json with
"python.venvPath": "/home/gobsan/.cache/pypoetry/virtualenvs",
"python.venvFolders": [
"/home/gobsan/.cache/pypoetry/virtualenvs"
],
2x times just in case, but it is greyed out and doesn't seem to work.
I have also tried to change poetry config virtualenvs.path to
~/.local/share/pypoetry/venv/bin
~/.local/share/pypoetry/venv
~/.local/share/pypoetry
hoping VSCode will see it since it can see something there
My main goal is to be able to see and switch between different venvs inside Jupyter. Switching poetry venvs for python scripts is no problem.
Thank you for any help.
ps. working through wsl2
You should end your path with a forward slash, like "/home/gobsan/.cache/pypoetry/virtualenvs/".
I was just researching Poetry and VSCode and ended up doing this as well.
Here's my own path and what my interpreter looks like after the settings update
The settings have changed for the Python extension in VS Code. I was able to select my Poetry virtual environment for my interpreter/ipynb kernel again after changing the dated python.pythonPath setting (yours might be python.venvPath) to python.defaultInterpreterPath in the VS Code settings.json file.
I don't know if changing this setting will allow VS Code to automatically pick up other Poetry virtual environment options listed under poetry env info --path in your CLI. The guidance here will hopefully be of use for that: https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter
{
"python.defaultInterpreterPath": "/Users/myname/Library/Caches/pypoetry/virtualenvs/projectname-randomnumbers-py3.9/bin/python",
}
(my work computer is a Mac)

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

"Running cells requires Jupyter notebooks to be installed" error in VS Code

I'm trying to use the new Jupyter integration for the Python extension in VS Code, and I'm getting the above error even though I have Jupyter installed and it works fine from the command prompt.
Here's my environment:
Python extension version 2018.10.1, and I see Run Cell/Run All Cells tooltips above #%% comments.
I've used the Python: Select Interpreter command to select my Anaconda environment, which is at ~/AppData/Local/Continuum/anaconda3/python.exe.
I have Jupyter installed in that interpreter (jupyter.exe is in the Scripts sub-folder under that location), and it runs fine with the jupyter notebook command at the Anaconda prompt.
But whenever I click on Run Cell or press shift-enter, I get this error message:
"Running cells requires Jupyter notebooks to be installed." Source: Python (Extension)
Is there something else I need to do to configure this?
You may give one try by restarting VS Code in following mentioned way [ It worked for me. ]
Open Bash or any other cmd
Activate any conda environment [ See below command ]
source activate base [ means activate base environment ]
Run VS Code instance [ See below command ]
code .
Now when you'll click on Run Cell or press shift-enter, it should work.
The problem is an issue in the VS Code python extension itself. There are a number of issues related to this open in the repository: #3354, #3343, #3330, and the issues are being worked on, see #3374.
The reason, as far as I understand, is that in this case - and in some other cases - the anaconda environment is not activated before running the command. Situations where the environment is activated are e.g. opening a python terminal or running a file in the python terminal, but this also needs to happen for Jupyter, Tests, and so on.
While theoretically, adding the Scripts folder to your PATH, as David mentioned, could help, it did not help in my case. This may just not be enough to properly reflect what happens on activation.
My guess is that we will have to wait for this issue to be resolved in the repo, but if someone else finds a workaround, I'd be happy.
Simply running vscode from the activated environment did not work for me, here is what did:
In terminal (bash) I ran:
conda activate <environment-name>
conda install jupyter notebook
When the install finishes, open vscode from terminal (the same shell with activated environment) with the command:
code .
Notes:
Replace '.' with the path to the directory you want to open if it's not the current directory.
I've written 'conda install ...' but mamba also works.
If the terminal command for 'code ' does not work, it's likely you need to add it to environemnt variables; in such a case, this post might help.