[英]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:但是,当我尝试从编写“name_of_the_file.py”的终端运行该代码时,我发现了这个错误:
"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." “术语“name_of_the_file.py”未被识别为 cmdlet、function、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试”
If a right click on where you write the code, and then click on "run python file in terminal", it runs!如果右键单击您编写代码的位置,然后单击“在终端中运行 python 文件”,它就会运行!
I am taking the CS50P, and I see that this should be possible because the teacher is able to do that.我正在参加 CS50P,我认为这应该是可能的,因为老师能够做到这一点。 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.点击播放按钮运行代码,观察终端可以看到使用命令& c:/WorkSpace/pytest11/.venv/Scripts/python.exe c:/WorkSpace/pytest11/main.py
运行代码。
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路径
python main.py
PS: main.py
is my script file name, you need to modify it to your own file name. PS: main.py
是我的脚本文件名,需要修改成自己的文件名。
try to follow these steps:尝试按照以下步骤操作:
My output:我的 output:
Do follow below steps请按照以下步骤操作
Open terminal in VS code在 VS 代码中打开终端
go to directory where you have.py file located using cd command go 到您使用 cd 命令找到 .py 文件的目录
run command in terminal (eg. python3 file_name.py在终端中运行命令(例如 python3 file_name.py
Me@MyMacBook-Air string % python3 str_to_int.py Me@MyMacBook-Air 字符串 % python3 str_to_int.py
Hope this helps希望这可以帮助
try this one:试试这个:
open terminal in vscode.在 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 python3 name_of_the_file.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.