简体   繁体   English

如何从终端在 Visual Studio 代码中运行 Python 文件?

[英]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:尝试按照以下步骤操作:

  1. Create a folder where you want your script to run创建一个要在其中运行脚本的文件夹
  2. Open the folder using VS Code: File -> Open Folder使用 VS Code 打开文件夹:文件 -> 打开文件夹
  3. Create your script and save it in the folder创建脚本并将其保存在文件夹中
  4. Open a new terminal: Terminal -> New Terminal打开一个新的终端:Terminal -> New Terminal
  5. Type the command: python name_of_the_file.py输入命令:python name_of_the_file.py
  6. If it doesn't work, try: py name_of_the_file.py如果它不起作用,请尝试:py name_of_the_file.py
  7. If you are using a python 3 version, try: python3 name_of_the_file.py如果您使用的是 python 3 版本,请尝试:python3 name_of_the_file.py

My output:我的 output:

在此处输入图像描述

Do follow below steps请按照以下步骤操作

  1. Open terminal in VS code在 VS 代码中打开终端

  2. go to directory where you have.py file located using cd command go 到您使用 cd 命令找到 .py 文件的目录

  3. 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:试试这个:

  1. open terminal in vscode.在 vscode 中打开终端。

  2. check the directory in terminal, it must be same path to where you file is saved.检查终端中的目录,它必须与保存文件的路径相同。

  3. run command in terminal:在终端运行命令:

    python3 name_of_the_file.py python3 name_of_the_file.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 Python:在 Visual Studio Code 的终端命令中运行 Python 文件 - Change Python:Run Python File in Terminal command in Visual Studio Code Visual Studio Code Python 扩展“在终端中运行文件”不起作用 - Visual Studio Code Python extension 'Run File in Terminal' not working Visual Studio 代码(在终端中运行 Python 文件不起作用) - visual studio code (run Python file in terminal does not work) 如何在终端Visual Studio Code中运行Python3函数脚本 - How to run Python3 function script in terminal Visual Studio Code 如果我使用 Visual Studio 安装 python,如何从终端运行它? - How to run python from the terminal if I installed it using Visual Studio? Visual Studio Code - 缺少图标“在终端中运行 Pyrhon 文件” - Visual Studio Code - Missing Icon "Run Pyrhon File in Terminal" Microsoft Visual Code 中的差异“运行 python 文件”和“终端中的运行行” - Difference 'run python file' & 'run lines in terminal' in Microsoft Visual Code 如何在 Visual Studio Code 中运行需要外部数据的 Python 文件 - How to run a Python file that requires external data in Visual Studio Code 如何在 Visual Studio 中运行单行 python 代码并在终端中获取 output? - How to run a single line of python code in Visual Studio an get the output in Terminal? 带有 Visual Studio Code 的 Python - 运行特定文件 - Python with Visual Studio Code - Run specific file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM