Change Python:Run Python File in Terminal command in Visual Studio Code - python

I am using an anaconda environment in VS code when suddenly the Run Python File in Terminal command bound to the green play-button in the top right corner stopped working. It used to input: python_path file_path/file.py into the terminal and it worked perfectly fine.
But after running my code in the anaconda prompt program it has now changed to:
& conda run -n env_name python file_path/file.py and now I get a ModuleNotFoundError, that I don't have cv2 installed, which I do.
I can still run the code if I simply input: python file_path/file.py. My question is if it is possible to change the command that is bound to Run Python File in Terminal. I could not find a solution to this in a couple hours of searching. Thanks in advance.

One solution I found was to reinstall the older python plugin and the "conda run -n" command would not be used at runtime.
Reinstallation method, Extension --> Python--manage--> install another version

You're most likely running into a bug in conda 4.8. You can either downgrade the extension until a point release comes out with a fix or simply wait for a fix (hopefully Monday).

Related

visual studio code (run Python file in terminal does not work)

visual studio code, pressing run does not work on mac. Let say I want to print the word "Hello" and run the code print("Hello"), it will not be executed. I tried reinstalling python and visual studio but I could't resolve the problem.
When this happens to me, the Python interpreter borked and I need to press ctrl + c (abort) in the terminal a couple of times. It's still in the Python process and vscode is trying to input the run command into a Python process that's not actually a terminal.
I ran into the same issue. Figured out that I forgot to select the Add Python 3.8 to PATH while installing. You can either choose to uninstall and reinstall Python and make sure to select the above option OR you can manually update the user environment variable for PATH and add the path to scripts and python like the following.
C:\Users\<addyourusernamehere>\AppData\Local\Programs\Python\Python38-32\Scripts\
C:\Users\<addyourusernamehere>\AppData\Local\Programs\Python\Python38-32\
For me, downgrading VS Code extension called Python helped.
This shows where the option to install another version is in VS Code Extensions
As for today, I had to go back to version 2021.11.1422169775
M1 MacBook, macOS Monterey 12.2.1

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.

VScode didn't recognize python3

I just installed my ubuntu on my PC then i installed python 3 & Vscode on my PC. but my VS Code didn't recognize the python3. maybe my vs code looking for my python. how to solve this?
In my VS Code when I compiled the class then the output:
python -u "/home/kisiniki/Documents/python/tempCodeRunnerFile.py"
/bin/sh: 1: python: not found
In my terminal already installed python 3.6.8.
First, try to see if you are able to run the same command in terminal.
python -u "/home/kisiniki/Documents/python/tempCodeRunnerFile.py"
If you are getting the same error, "python: not found", then likely python installation was not completely successful, you can either uninstall and reinstall or you can try to update PATH to contain the folder containing the python executable. See https://askubuntu.com/questions/637848/how-to-reset-python-path-to-usr-bin-python for more detail.
If you can run the command in terminal, then that means VS Code cannot find your installed python. See if you need to edit some settings in VS Code for it to know what python executable to run. See
https://code.visualstudio.com/docs/python/environments for more detail.
It's probably because you used the command python without having an activated virtual environment or not using python3 or python3.6. Try it with python3.6 and see if that fixes it. If it doesn't then Python isn't on your PATH.

"conda" command returns "failed to start process" after updating Continuum Anaconda

I had a working version of Anaconda 2.3 on my Windows 10 PC. (I use PowerShell as my preferred command line interface.)
After updating to Anaconda 2.4, any command using conda or anaconda resulted in the message failed to start process.
I tried and uninstalling, redownloading and reinstalling, and even tried reverting to 2.3. Nothing worked!
Starting to get desperate, I eventually found the solution: I used python to run the script conda-script.py, which is apparently what the command conda.exe runs when called:
python PATH_TO_ANACONDA\Anaconda3\Scripts\conda-script.py
(Here, I'm using PATH_TO_ANACONDA to represent the directory in which the Anaconda3 folder resides on my particular system.)
Thereafter, both conda and anaconda commands work perfectly, and continue to work. (e.g. No need to repeat the process every time I launch PowerShell.). But I have no idea why the problem occurred, or why that happened to be the solution.
If you know, please reply. If not, I just hope this solution saves others an hour or so of frustration.

VS code runs python with no errors only if started from conda terminal

One of the projects that I work on in Python is pretty big and uses a lot of libraries.
I started developing it in Spyder then switched to VS Code.
I also use conda environment.
The problem is with VS Code only: when I open VS Code itself and try to run the project - I get all kinds of import errors.
However, if I open the conda terminal first and just type "code" in it and execute it - the VS Code opens, I can select my project and everything runs just fine, no errors.
In both cases the environment is the same, I did the Ctrl+Shift+P to select it, plus it shows as selected in the bottom left corner anyway. It's the default env.
How can I fix this so I don't have to start VS Code from conda terminal each time?
at the conda the modules are installed by default.
in vscode or any other of ides you have to install it manually.
open your terminal or cmd and type:
pip install module-name
I found what the problem was so I'll just leave it here in case it may happen to someone else.
My VS Code was using Powershell as the default shell, even though the input was right with the correct conda env selected (the base in my case), even with running
conda activate base
command to ensure it, it would still somehow mess up and complain about missing imports
So the solution was to set the default shell in VSC to regular Windows cmd.exe - that was the only needed change

Categories