VSCode suddenly started executing Python scripts as a subprocess - python

Normally when I would click on "Run" button in VSCode for a Python script it would activate the currently selected virtual environment and simply call python <script_name.py> in the terminal and it all worked fine.
Now all of a sudden every time I try to run a script what is does is instead call a subprocess via conda like so:
conda run -n <environment_name> --no-capture-output --live-stream python <script_name.py>
And this new version is causing some issue because for whatever reason conda refuses to recognise some of the packages as having been installed. The code still works fine when I manually type the run command in the terminal but I want the old behaviour to be back.
Anyone knows how to fix this?

Input: Ctrl + Shift + P .
Enter: Terminal: select default profile .
Change the default to CMD .
Maybe this can help you.

Related

Vscode uses wrong terminal when running python code

I always used the "Run python file in terminal"-button in Vscode, but some recent update seems to have broken it for me. My default terminal is cmd, but when I use the button, it now uses powershell and does not activate the conda environment, which prevents my code from running.
When I open up a new terminal manually, it rightfully uses cmd, activates the env and python <path> works again.
This is the output after pressing the run-button:
Does anyone know how to set the button to use cmd?
This is an issue of Python Extension. And it should be fixed tomorrow with the new release. You can refer to here for more details.
And have you add "python.terminal.activateEnvironment": true, in the settings.json file? Because it's weird, as the cmd activates the environment while the Powershell does not.

How does one connect a Jupyter Kernel to VS Code if one does not connect automatically?

I want to be able to have a working jupyter notebook working for VS code out of the box, with minimal work on my side.
I tried opening a jupyter notebook. I immediately found the interpreter and used my conda env with the command pellet (command + shift + P then in the drop down menu found my conda env). This seems to make the terminal work since which python points to the right place:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python
also running python scripts from VS Code seems to be working fine too, see output:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/automl-meta-learning/python_playground.py
x = 1
my_str = this is a string
y = 2
but when I try the jupyter notebook it doesn't work.
Most noticeably my VS code does not have a kernel connected, look at the screenshot:
I tried clicking on a couple of arrows as suggested on reddit (vs_code_jupyter_server_no_kernel_python_not):
When I try running things in my jupyter notebook I get the following error:
Error: Activating Python 3.7.6 64-bit ('base': conda) to run Jupyter failed with Error: StdErr from ShellExec, /Users/brandomiranda/.bashrc: line 31: jump-module.bash: No such file or directory
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
.
which is strange. My shell is not even bash so I don't know why that message is being shown to me (maybe VS Code doesn't know Mac OS Catalina uses zsh? not sure how to fix that for VS Code). Regardless, I proceeded to do what I think the error message is suggesting. So I did the following:
I ran conda init and then went down to the terminal inside vs code and restarted the shell by running zsh but the jupyter kernel still doesn't work on my notebook in vs code. Not sure what's wrong.
In addition, I did what the error message suggested:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ conda init zsh
no change /Users/brandomiranda/miniconda3/condabin/conda
no change /Users/brandomiranda/miniconda3/bin/conda
no change /Users/brandomiranda/miniconda3/bin/conda-env
no change /Users/brandomiranda/miniconda3/bin/activate
no change /Users/brandomiranda/miniconda3/bin/deactivate
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.sh
no change /Users/brandomiranda/miniconda3/etc/fish/conf.d/conda.fish
no change /Users/brandomiranda/miniconda3/shell/condabin/Conda.psm1
no change /Users/brandomiranda/miniconda3/shell/condabin/conda-hook.ps1
no change /Users/brandomiranda/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.csh
no change /Users/brandomiranda/.zshrc
No action taken.
but it seems it made no difference.
Any idea how to fix this?
I was suggessted by the developers of the VS code python extension to follow whatever they did here:
https://github.com/microsoft/vscode-python/issues/9566
but I can't figure out exactly it is they want me to do.
How do I fix this?
Related resources:
reddit: https://www.reddit.com/r/vscode/comments/eq2bfv/vs_code_jupyter_server_no_kernel_python_not/
gitissue: https://github.com/microsoft/vscode-python/issues/9636
I initially thought it was a conda issue so I looked into this question: Activate conda environment stopped working in vscode
https://www.reddit.com/r/vscode/comments/eshxka/how_does_one_connect_a_jupyter_kernel_to_vs_code/
https://www.quora.com/unanswered/How-does-one-connect-a-Jupyter-Kernel-to-VS-Code-if-one-does-not-connect-automatically
You need to select the python interpreter for jupyter, you can do it by following the step
Open command panel
Mac: CMD+Shift+P
PC: CTRL+SHIFT+P
Then search for select Interpreter to start jupyter server then hit enter, it will list all the interpreter, then select any interpreter and done!
it's just a one time process, after this, it will get connected automatically.
As bizarre as it seems, I also noticed this the other day and the only thing that works so far for me is to open VS Code by launching it from the Anaconda Navigator:
Then I get, as expected:
If you see the very long discussion I had in the git issue (https://github.com/microsoft/vscode-python/issues/9566) once I removed all the errors thrown by my .bashrc and .zhrc, the jupyter feature in VS code started working for me again. It's super weird (specially because I am NOT using bash at all and I am using zsh as my shell, I would have expected VS code to be robust to my .SHELLrc files throwing errors but it's not).
If that doesn't work, then you might have to install the vsix view extension and install the ms-python-insiders.vsixlinked in the issue (https://github.com/microsoft/vscode-python/issues/9566).
If on Windows VSCode, what worked for me was installing and enabling the Python and Jupyter extensions, then CTRL+SHIFT+P, select Interpreter to start jupyter server. Those extensions were the bottleneck.

VS Code not running a Python file in the conda environment mentioned

I have both anaconda and VS Code installed in my system. When I start writing a script, VS Code gives me an option to chose the interpreter. I choose my particular conda environment. This works fine. However when I try to run the script using Ctrl+Alt+N or using the play button on the top right (using the extension Code Runner), the terminal that opens in the VS Code doesn't recognize that I am now working in a conda environment.
As a result, it gives an error that the module is not installed (because the module is available only in the conda environment).
When I try to manually activate the conda environment from the VS Code terminal, it doesn't work.
What is the workaround? I am using Windows.
Update: If I use cmd as the default integrated terminal in VS Code in place of PowerShell, then the command activate NAME_OF_ANACONDA_ENV works. And then, I can manually run the script. However, running the script using Ctrl+Alt+N still doesn't work; it doesn't recognize that I am in a conda env.
After a few tries i figured out how to do it. So, this works for me.
vs code settings search: code-runner.executorMap
Choose User and
Edit in settings.json
find: "python": "python -u"
change: "python": "$pythonPath -u $fullFileName"
if you encounter tempCodeRunnerFile or selection problem:
vs code settings search: code-runner.ignoreSelection
tick the box
On windows 10: Ctrl+Shift+P and selecting the environment gets the job done.
After setting the environment, I execute by pressing Crtl+F5 to execute without debugging and F5 to debug.

Anaconda on Windows 10: iPython and Spyder fail to start in Python3 environment

I'm using Windows 10 and I have Anaconda with Python 2 installed, so my root environment is Python 2. I created an additional Python 3 environment and among other packages installed iPython and Spyder into it. I used the Anaconda Navigator to install the packages.
I can activate and deactivate the environment using Windows CMD just fine. After activating the Python 3 environment in the CMD the ipython command typed into the same CMD starts up Python 3.6.1.
The Anaconda Startmenu folder does contain shortcuts to iPython and Spyder both for Python 2 and Python 3 now. I can use those to start both for Python 2 as before, but the Python 3 versions won't start. And there is no error message or crash or anything.
When clicking on the Python 3 iPython shortcut a command prompt pops up for a split second and immediatly closes again. Spyder does not even open a command prompt, it does absolutely nothing, I presume it's because iPython fails. Checking the task manager shows that there is no Python running in the background at all, so it really does not start.
Now I know that iPython 3 itself is not broken because I can start it from within CMD after switching environments, nonetheless I deinstalled and reinstalled them both, no change.
I then went into the shortcut to get the exact command it was executing to write a small batch file with a pause command to see if anything gets displayed when iPython fails. Doing a right-click on the shortcut and executing "open file location" leads me to python.exe in the Python 3 environment base folder, and executing that works fine of course.
So now I'm stumped since I have no leads to solve or even analyze the problem properly, over the entire course of action not a single error message ever appeared anywhere.
Any hints and suggestions are appreciated.
EDIT:
The target of the Python 3 shortcut in the properties looks like this:
C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\envs\Python3\python.exe C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\cwp.py C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\envs\Python3 "C:/Users/My.Name/AppData/Loca
The working shortcut to Python 2 looks pretty much the same:
C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\python.exe C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\cwp.py C:\Users\My.Name\AppData\Local\Continuum\Anaconda2 "C:/Users/My.Name/AppData/Local/Continuum/Anaconda2/pyth
I found the problem.
As it turns out the module menuinst wasn't automatically installed into the new environment so I had to manually install it. Now everything works.

Python Launcher Mac

On my mac I am trying to make a Python script open by default in the Python Launcher.
I left click on the script and select "open with" and select the Launcher. Every-time I do this Terminal pops up, the Launcher window appears briefly. Then a messages pops up in a window: "Python Launcher can only run certain scripts try another."
Something along those line (the message has stop popping up since the first time I tried it but the script still doesn't run). I tried everything, GUIs, and basic scripts (like print "Hello World"). None work!
I tried doing the same thing on other computer and I get the same problem. What am I doing wrong ?
Does your script use a shebang line:
#!/path/to/executable/python2.6
And does it have execute permissions?
chmod +x path_to_file.py
It should run OK like this.
Assuming you're talking about Anaconda, Launcher is now deprecated, so your approach will have to change anyway. It has been replaced by Navigator.
Make sure you have updated Anaconda within terminal using
conda update conda
Then, you can install Navigator with
conda install anaconda-navigator
You will see a new icon show up that functions much the way Launcher used to. Your script will now have to be tied to it instead of launcher, assuming you figured that out with previous commenters' help.

Categories