Visual Studio Error Ctrl + Alt + N with Python - python

I'm new to coding and just started using Visual Studio to learn Python. In a tutorial video, the professor said that I can hit Ctrl + Alt + N to automatically run my code. I get the error "[Running] python -u "c:\Users***\Desktop\Test\app.py"
'python' is not recognized as an internal or external command,
operable program or batch file."
I am able to run it when I go to my terminal and run through CMD but not when I do the shortcut. I looked through settings and there wasn't anything I saw that stood out. Any suggestions?
Thanks

Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
Click the “Environment Variables…” button
Click on Path variable
Click on edit button
Create a new address, where you have installed python. The default address is C:\Python34
Save and exit the window
Install Python extension on VScode
Restart VScode
If problem continues then, open command pallete( CTRL + Shift + p )
9.1. Type Python
9.2. Select python interpreter from the drop down menu

Related

VS Code won't open terminal when trying to launch Python code

It just seems like I can't open a terminal in VS Code. Every time I try to open a terminal, it gives me this error:
I would be grateful if anyone could help me with this.
I tried reinstalling VS Code but it didn't work. I also tried disabling all extensions except python extension but it didn't work too.
Try this:
Ctrl + Shift +P
Select "Preferences: Open User Settings"
Write "terminal" in the search box
Search for "Terminal > Integrated > Default Profile: Windows"
Set value to "Command Prompt"
Helpful Image
Ctrl + Shift +P
Select "Preferences: Open User Settings"
Write "terminal" in the search box
Search for "Terminal > Integrated > Default Profile: Windows"
Set value to "Command Prompt"
or to "Powershell"

Using VSCode with WSL, run button does not run with mnt. Hence, my code is not found to run

I am using WSL ubuntu on a windows computer while using VSCode as my code editor.
I'm trying to run python files on WSL but when pressing on the run button (https://i.stack.imgur.com/J5DfM.png) (ctrl + alt + n) the code gets executed with the default windows explorer pathway (c:\users...) so that makes my file not readable for WSL to run.
Here is a screenshot on what it seems like:
Screenshot of the shell
As you can see, when I press on the run button it comes with the error:
python: can't open file 'c:\Users\name\Desktop\firstone\newfolder\base.py': [Errno 2] No such file or directory
Because it is coming in the format of c:\Users...
Meanwhile when I run a command that I typed on my own of the correct format:
python -u /mnt/c/Users/name/Desktop/firstone/newfolder/base.py
It works correctly.
Do you know how I can automatically map the run button to run for a wsl environment?
I would really appreciate your help, thank you.
I have found the answer for anyone what is wondering later.
Follow the instructions on this article.
https://learn.microsoft.com/en-us/windows/python/web-frameworks
The most important part is to initiate the "Run Python File in Terminal" first before pressing the run button.

VS Code 'Run Python in Terminal' Play Button Not Working

Now I know that this question has been asked before at: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings
but I have tried doing everything that all the answers said to do but nothing works.
I am using Visual Studio Code to run a Python file, however when I try running the file using the 'Run Python File' play button in the top right corner, it gives me this error:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
I have made sure that python is installed, but just to be sure I uninstalled and reinstalled it again. I also made sure to check the box saying 'Add Python to PATH' before reinstallation. Furthermore I have also tried doing as the message prompts you to, and turned off the shortcut from Settings > Manage App Execution Aliases; but when I do that it gives me the following error:
& : The term 'C:/Users/haide/AppData/Local/Microsoft/WindowsApps/python.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
& C:/Users/haide/AppData/Local/Microsoft/WindowsApps/python.exe "g:/M ...
+ CategoryInfo : ObjectNotFound: (C:/Users/haide/...Apps/python.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have also manually checked the PATH in System Properties > Environment Variables and it's fine there as well. I have also made sure that I am in powershell in the terminal rather than cmd.
The file does run if I type py filename.py in the terminal, but why is the play button not working, even though it was working until yesterday?
You may check in vscode:
ctrl + maj + p
on the prompt, type python then select an interpreter
select the path you want
That's all, this pop up is for making the green play button work fine

How do I find/excute Python Interactive Mode in Visual Studio Code?

I'm unable to find/execute the interactive mode in visual studio code - could you guys step-by-step me? Seems like a ridiculous question, I guess, but none of the waypoints I Googled seem to work;
"View > Other Windows > Interactive menu commands)" https://learn.microsoft.com/en-us/visualstudio/python/python-interactive-repl-in-visual-studio?view=vs-2019
I don't have "other windows"
If there's something I need to install, please let me know.
(Yes, I'm a beginner).
In VSCode, you can enter the Python interactive window in the following places:
Option 1
F1 -> Python: Create Python Interactive Window
This is the Python interactive window that comes with VSCode.
You can enter the interactive window without inputting instructions.
Please use "Ctrl+Enter" to execute the code:
Option 2
Ctrl + Shift + `
This is the cmd window that comes with the VSCode integration computer.
We need to enter the command "python" to enter the Python interactive window:
PS
For more information about using Python in Visual Studio Code, you can refer to the official VSCode documentation: Python in VSCode.
If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e. snippets of code executed in one go).
This article writes about the interactive window and many more possibilities of using Jupyter in VS Code: https://code.visualstudio.com/docs/python/jupyter-support-py
Best option
When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.
I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.
Terminal fallback
Press:
Ctrl+`
This opens a system shell (bash, or on Windows: Powershell or cmd).
Run python, or ipython if you have it (pip install ipython), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments.
If you type # %% in your vscode editor while editing a .py file,
then a interactive cell is created and it can be evaluated. The nice part of this is that # denotes comment in a .py file so you can save and run your code as a script

Python 3.6 in Visual Studio 2017 How to Run Program in the Interactive Shell

I downloaded and installed Visual Studio along with Anaconda to get access to all of the packages that come pre installed with Anaconda. I am trying to figure out how to run code such that it runs in the interactive shell. Right now when I hit F5 an Anaconda 3 cmd line window comes up with the prompt "Press any key to continue..." comes up. My question is: how can I make it so that when I hit F5 my code is executed in the interactive Python shell much like it does on the basic IDLE that comes with Python.
This seems like a question that a simple Google Search could fix, but for some reason I cannot find the answer. I've done some google searching, and I watched the Visual Studio python official Microsoft series about it. One of the videos touched on using the interactive shell, but even in the video, when he clicked the Start (Run) button, the code ran in what looked like the command line.
I have used IDLE in the past, and now I think it is time to make the change to a bigger IDE. I love the code completion and templates of visual studio, and I can't wait to solve this (noob) question.
Thanks
I am struggling with this as well. There is a Visual Studio Shell command execute file in Python interactive which is bound to Shift+Alt+F5 by default.
This works: if the focus is in a code window then the current file is executed. If the focus is in the Solution Explorer window, the file selected as "Startup item" is executed. There seems to be a glitch however: Some import statements from the specific file which work fine on the standard Ctrl+F5 will fail on Shift+Alt+F5. I need to figure out why this is the case and will report here.
EDIT: Once in the interactive windows, change the working directory to the folder containing the project: os.chdir etc. Then import your-filename works flawlessly. So I assume that there is some problem with selecting the working directory while executing Shift+Alt+F5.
Add
import os
os.chdir(r"C:\My\script\\path\")
to the top of your script.
Then Shift+Alt+F5 works as expected.
I'm not sure if it's the best way to do that, but here is what I do to quickly run Python script in Interactive Shell in Visual Studio 2017:
Add #%% sequence at the beginning of *.py file
Press Ctrl+Enter having cursor active in text editor
Code from #%% to end of file (or another #%%) will appear on Interactive Window
#%% is basically beginning of the cell. Cell is a part of code you would want to run at once in Interactive Shell. Cell begins with #%% and ends with another #%% which initializes another cell.
For example: you have following code in Visual Studio:
#%% Cell 1
print("Hello world1")
print("Hello world2")
#%% Cell 2
print("Hello world3")
When you click/focus on third line and press Ctrl+Enter you will run second and third line in Interactive Window.
To make a same experience like in c# where you can use F5 to start debugging python in Visual studio 2017 , you need to
1) create a new visual studio project ( ctrl + shift + N)
2) Select python as project type
3) Now you can create new python file ( *.py) and start code python ( ctrl + N)
4) Now you can right click the py file that you just created and use "set as startup file" command
5) Hit F5 to start python IDE debug experience ( breakpoint , inspect value , data type etc) , see below screen shoot

Categories