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

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

Related

Cannot run Python Script inside VSCODE terminal

However simple my python script is, I cannot get it running inside VSCODE Terminal by clicking on the "RUN" icon at the top right, whereas it runs fine when called from a CMD window
I had the same problem, this thread helped me I get a syntax error about File "<stdin>", line 1
You are now working inside your Python shell (>>>) while the code you are running (& python "FILENAME.py") is no Python, therefore syntax error.
exit()
to exit the python shell and return to the PowerShell environment.
Simply
python
to enter again, to run python code directly
It seems python is not recognized in your PATH variable. I think the 2 ways to fix are to find your python filepath and adding it your User Variables:
Control panel > System > advanced settings > Advanced tab > Environment Variables
select PATH and then click edit..
in a new line add the path to your python installation (ie C:\Users\YOURUSERNAME\AppData\Local\Programs\Python\Python38-32)
OR, not sure this works out of the box but you could:
install Python module from within VS Code, published by Microsoft
After editing the PATH as suggested by #mah111, I needed to restart VSCODE application

Running Python Scripts from Anywhere

I'm new to programming. Giving this another try.
I've been able to make Python run from PowerShell through changing the Environment variables. When I try to launch Python using Win + R, only py launches python. This puzzles me. PowerShell and cmd have no problem with just typing python.
I'm also trying to make it so that I can launch a python script from anywhere. I made a simple script called again.py in visual studio code and saved the script in a specific folder. When I try to run the script in VSC, I always get the error:
[Running] python -u "c:\Users\xyz\Programming\Python\again.py"
'python' is not recognized as an internal or external command,
operable program or batch file.
[Done] exited with code=1 in 0.137 seconds
By my understanding, if a folder is included in Path, and a file is called that is stored within that folder, the file/script should run. I should be able to call that file from anywhere, but I am just not able to type the file name in PowerShell or Run and have it run.
win + R returns:
Windows cannot find 'again.py'. Make sure you type the name correctly, and try again.
PowerShell: start again.py
start : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ start again.py
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Typing again.py into cmd causes it to run just fine.
The Powershell error is caused by a space in the file name. Powershell looks at input, start again.py, and parses it like so
first token on row is word "start"
start is an alias to start-process
second token on row is "again.py"
that will be passed as argument to start-process
The easiest work-around is to avoid spaces in file names. Use an underscore or camel case instead. That is, startAgain.py or start_again.py. One can use spaces, but there are extra quoting tricks involved. What's worse, those depend on shell. Cmd has its own rules, Powershell has very different ones. Since you are learning how to program, consider focusing on Python for now and worry later on shell quoting issues.

First time trying to run python from Powershell. Am I using the right command/ what do I need to do to permit use of Python?

I'm trying to run some Python files from command line, using PowerShell, but I'm really unfamiliar with the shell, and I've run into some issues trying to run python. I think there is some preparation process for python that I've missed, but I don't know what.
In trying to run Python through Powershell, I've tried installing Python 2.7 on my computer. While it changed the nature of the error message I was first receiving, it has otherwise not done much.
I've also tried doing some research to see what might be going wrong, personally, but I'm having some trouble finding an exact answer to my problem, and therefore figured I'd try asking, on here.
From what I've seen, online, I think this is the correct way to call and run a python file in Powershell. It's what I'm currently using:
PS C:\Users\nickd\documents\Coding\Comp_Sci_Class\Python\debugger> python my_test.py
This should run a program that tests whether or not a debugger is working. Instead, I get the following error message when I try and use the command 'python':
python : The term 'python' 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:1
+ python my_test.py
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
As such, I'm led to believe that there is some step in preparing Python for use on Windows Powershell that I'm just missing, but I'm not finding a clean cut answer for what that might be.
Could somebody please help me out, and describe what it is I might need to do? I know that what I'm missing is likely extremely simple, and I apologize upfront if this is a silly problem to have. That said, if somebody could point me in the right direction as to what I need to do, I'd be really grateful.
Thank you.
Powershell doesn't find where you have installed python. If you add the folder where python.exe is located to path then it will work.
See here https://superuser.com/a/143121
You have to add location of Python.exe to your environment variable..
Your path might be C:\Program Files\Python27
Please follow the below steps :
1) Right Click on This PC (My Computer) and Select Properties ( Win Key + Pause key)
2) Click on Advance system setting from left pane
3) Click on Environment Variables...
4) select variable called "Path" from second list box
5) Click on Edit button below the same list box
6) Add python.exe full path ( do not include filename )
7) close all dialogs by clicking OK
8) restart your powershell | ISE
Since Python 3.3, the PythonLauncher is installed with Python on Windows. It is py.exe and is already in the PATH (c:\windows) if you did an "all users" installation. The default installation of Python also registers py.exe as the default handler for .py files.
Normally, with py.exe registered as the default handler, mytest.py would be sufficient to run the script, but since Powershell doesn't search the current directory by default, you can use .\my_test.py if it is in the current directory.
Otherwise, just run py my_test.py (if "all users" installation) or add the user directory where py.exe was installed to PATH if not. If you have multiple versions of Python installed, py -3.6 (for example) will run Python 3.6.
More information:
Python Launcher for Windows
Using Python on Windows

Visual Studio Error Ctrl + Alt + N with 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

Visual Studio Code Terminal

When I try to run Python code in the terminal with a print command, nothing happens. When I try the test.py command in the terminal itself, the text copied below is what gets output.
Please assist in how to get the terminal to print my code.
E:\Kath\Documents\blockchain> test.py
test.py : The term 'test.py' 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:1
+ test.py
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound:
(test.py:String) [], CommandNotFoundExce
ption
+ FullyQualifiedErrorId : CommandNotFound
Exception
Once you open the VS Code, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the command.
You can also use the Select Python Environment option on the Status Bar if available.
Now, create a new file, and save it with the extension .py. Once done this, write the required code in the workspace. And then Right Click on the workspace -->Run Python file in the terminal.
This runs the selected file.
Hope this solution helps.
In "Terminal" you get what is essentially a regular Command prompt, and command interpreter in your case is PowerShell.
So, to execute the python script you should run it as:
python test.py
or
python3 test.py
or similar, depending of your python executable.
As Powershell is your default command shell in Visual Studio Code, if the ".py" extension is registered with Python, you can try:
.\test.py
If you use virtual environments, make sure you select the correct Python interpreter.

Categories