在 VS Code 中运行 Python 脚本时如何隐藏文件路径? [英] How to hide file paths when running Python scripts in VS Code?

查看:54
本文介绍了在 VS Code 中运行 Python 脚本时如何隐藏文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行我的代码时,底部的终端都会显示这个长名称(我认为是文件位置)以及它应该显示的任何输出.

有没有办法让它消失?

这是它的样子:

<块引用>

administrator@Machintosh-2 练习文件 Python %/user/bin/python3...你好,世界!管理员@Machintosh-2 练习文件 Python %

解决方案

AFAIK,没有办法隐藏路径,因为 VS Code 集成终端基本上是在使用您的操作系统/系统的底层终端.并且在终端上运行 Python 脚本需要如下形式:

<path/to/python/file>

如果你想要一个清洁工"控制台输出,您可以创建一个

不幸的是,如果您的 Python 代码使用并需要用户手动 input(),使用 Debug ConsoleinternalConsole 解决方案将不行.它将显示此错误:

没有直接的解决方案,要拥有一个干净的"输出并允许手动用户input().您可能需要考虑从文件中读取程序输入或

Every time I run my code, the terminal at the bottom is displaying this long name (I think the file location) as well as whatever output it's supposed to display.

Is there a way to get that to go away?

This is what it looks like:

administrator@Machintosh-2 Exercise Files Python % /user/bin/python3...
Hello world!  
administrator@Machintosh-2 Exercise Files Python %

解决方案

AFAIK, there is no way to hide the paths, because the VS Code Integrated Terminal is basically using your OS/system's underlying terminal. And running Python scripts on a terminal requires the following form:

<path/to/python/interpreter> <path/to/python/file>

If you want a "cleaner" console output, you could create a debug/launch configuration for Python, then set the console configuration to internalConsole:

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "run-my-script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/path/to/your_script.py",
            "console": "internalConsole"
        }
    ]
}

That would display the output in the Debug Console tab rather than the Terminal tab. There will be no more paths, just whatever your script prints out to the console.

sample output:

Unfortunately, if your Python code uses and requires manual input() from the user, using the Debug Console and the internalConsole solution would not work. It will show this error:

There is no direct solution to that, to having a "clean" output and allowing manual user input(). You might want to consider instead reading your program inputs from a file or passing them as command line arguments. VS Code supports passing in arguments ([args]) when running your script

<path/to/python/interpreter> <path/to/python/file> [args]

using the "args" option in the launch.json file. Here is a simple example with sys.argv:

python code

import sys

# This is always path to the script
arg_0 = sys.argv[0]

# Same sequence as "args" in launch.json
arg_1 = sys.argv[1]
arg_2 = sys.argv[2]

print(arg_1)
print(arg_2)

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "run-my-script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/path/to/your_script.py",
            "console": "internalConsole",
            "args": [
                "89",
                "abc"
            ]
        }
    ]
}

sample output

这篇关于在 VS Code 中运行 Python 脚本时如何隐藏文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆