Visual Studio Code Terminal - python

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.

Related

How to run a Python file in Visual Studio code from the terminal?

I have tried to run a pretty simple code
x = input("What's x? ")
y = input("What's y? ")
z= int(x) + int(y)
print (z)
But, when I try to run that code from the terminal writing "name_of_the_file.py", I find this error:
"The term "name_of_the_file.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."
If a right click on where you write the code, and then click on "run python file in terminal", it runs!
I am taking the CS50P, and I see that this should be possible because the teacher is able to do that. What am I doing wrong guys?
Click the play button to run the code, watch the terminal and you can see that it is using the command & c:/WorkSpace/pytest11/.venv/Scripts/python.exe c:/WorkSpace/pytest11/main.py to run the code.
So if you need to manually type commands in the terminal to run the code. You can directly copy the above command.
If you use a virtual environment or have system environment variables configured, you can simplify the python path with the following command
python main.py
PS: main.py is my script file name, you need to modify it to your own file name.
try to follow these steps:
Create a folder where you want your script to run
Open the folder using VS Code: File -> Open Folder
Create your script and save it in the folder
Open a new terminal: Terminal -> New Terminal
Type the command: python name_of_the_file.py
If it doesn't work, try: py name_of_the_file.py
If you are using a python 3 version, try: python3 name_of_the_file.py
My output:
Do follow below steps
Open terminal in VS code
go to directory where you have .py file located using cd command
run command in terminal (eg. python3 file_name.py
Me#MyMacBook-Air string % python3 str_to_int.py
Hope this helps
try this one:
open terminal in vscode.
check the directory in terminal, it must be same path to where you file is saved.
run command in terminal :
python3 name_of_the_file.py

VScode won't run Python scripts anymore, going on the last 2 weeks and can't figure it out

Not sure what changed but now if I try to run any --------.py file, I get this response.
**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) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException Suggestion [3,General]: The command test.py was not found, but does exist in the
current location. Windows PowerShell does not load commands from the
current location by default. If you trust this command, instead type:
".\test.py". See "get-help about_Command_Precedence" for more
details.**
I've been learning and running Python over the last few months, and now something changed and can't run Python files. Any help would be great.
You could not run the python file directly.
You need to run it like this: python test.py.
If you are using the VSCode and want to run the python file. Press F5 directly and follows the instructions. You could look into here for help.

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

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.

found error while trying to run print('hello world') by a python script that executes the code within powershell

I am currently trying to run a simple hello world program in powershell using this code :
import subprocess
import sys
p = subprocess.Popen(["powershell.exe",
'C:/users/m.m/Desktop/hello.ps1'],
stdout = sys.stdout)
p.communicate()
what i'm trying to do is this; i have saved the "hello.ps1" python program with powershell extension containing print("hello, world!") in desktop and I want to create a program that reads the file within my python files , opens them and runs those scripts in powershell after invoking python by python command.
I have already tried the code above as suggested in here ==> Running powershell script within python script, how to make python print the powershell output while it is running, but it gives me security error.
This is the error that i get when run the code above:
C:/users/m.m/Desktop/hello.ps1 : File C:\users\m.m\Desktop\hello.ps1 cannot
be loaded because running scripts is
disabled on this system. For more information, see about_Execution_Policies
at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ C:/users/m.m/Desktop/hello.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
The main goal is this:using python to write scripts for powershell.
You're getting that error likely for two reasons, the first is because you're not running PowerShell as an administrator, you should save your script and reload ISE as an administrator.
Secondly, you must set run the following:
Set-ExecutionPolicy RemoteSigned
or
Set-ExecutionPolicy unrestricted
Run this before you run your script.
Additional information on Set-ExecutionPolicy can be found here
Make sure you can run powershell scripts (it is disabled by default).
Run this as an Administrator into your PowerShell window
Set-ExecutionPolicy RemoteSigned
because the error that you're getting is
cannot be loaded because running scripts is disabled on this system.

Categories