简体   繁体   English

在 VS Code 中运行 Python 脚本时如何隐藏文件路径?

[英]How to hide file paths when running Python scripts in VS Code?

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.每次我运行我的代码时,底部的终端都会显示这个长名称(我认为是文件位置)以及它应该显示的任何 output。

Is there a way to get that to go away?有没有办法让 go 消失?

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. AFAIK,没有办法隐藏路径,因为 VS Code 集成终端基本上是在使用你的操作系统/系统的底层终端。 And running Python scripts on a terminal requires the following form:在终端上运行 Python 脚本需要以下形式:

<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 :如果你想要一个“更干净”的控制台 output,你可以为 Python 创建一个调试/启动配置,然后将console配置设置为internalConsole

.vscode/launch.json .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.这将在“调试控制台”选项卡而不是“终端”选项卡中显示 output。 There will be no more paths, just whatever your script prints out to the console.将不再有路径,只是您的脚本打印到控制台。

sample output :样品 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.不幸的是,如果您的 Python 代码使用并需要用户手动input() ,则使用调试控制台internalConsole解决方案将不起作用。 It will show this error:它将显示此错误:

在此处输入图像描述

There is no direct solution to that, to having a "clean" output and allowing manual user input() .没有直接的解决方案,有一个“干净的” output 并允许手动用户input() You might want to consider instead reading your program inputs from a file or passing them as command line arguments .您可能要考虑改为从文件中读取程序输入或将它们作为命令行 arguments 传递 VS Code supports passing in arguments ( [args] ) when running your script VS Code 支持在运行脚本时传入 arguments ( [args] )

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

using the "args" option in the launch.json file.使用 launch.json 文件中的"args"选项。 Here is a simple example with sys.argv :这是sys.argv的一个简单示例:

python code python代码

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 .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样品 output

带有命令行参数的调试控制台

I ran into the same problem and I found an alternative solution.我遇到了同样的问题,我找到了替代解决方案。 It is possible to clear the console after starting the program with a simple line.启动程序后,可以通过简单的一行来清除控制台 This will remove the initial path output in the terminal as well:这也将删除终端中的初始路径 output :

from os import system

system("clear")
print("Hello World")

The simplest/quickest way I do this is by putting我这样做的最简单/最快的方法是把

print("\033c")

at the top of my file (or anywhere else where I want everything above that print("\033c") erased (so I can get rid of the textual noise my other print statements might be making).在我的文件顶部(或我希望删除print("\033c")之上的所有内容的其他任何地方(这样我就可以摆脱其他打印语句可能产生的文本噪音)。

In ASCII encoding the octal number 33 is the ESC (escape) character, which is a common prefix for terminal control sequences.在 ASCII 编码中,八进制数33是 ESC(转义)字符,它是终端控制序列的常用前缀。 In combination with c we have an old VT100 reference essentially asking the terminal to reset to default, thus erasing all that textual noise so we only see the output of our code.结合c ,我们有一个旧的VT100 参考,基本上要求终端重置为默认值,从而消除所有文本噪音,所以我们只看到我们代码的 output。

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

相关问题 VS Code 没有运行 python 脚本 - VS Code not running python scripts 在 VS Code 中同时运行 Python 脚本 - Running Python scripts simultaneously in VS Code 如何制作定义了相对路径的python配置文件,但是当其他目录中的脚本导入配置时,路径正确吗? - How to make python config file, in which relative paths are defined, but when scripts in other directories import config, paths are correct? VS Code - 在终端中选择运行 Python 文件时打印的文件路径 - VS Code - printed file paths when selecting Run Python File in Terminal 如何在 VS Code 终端中从.py 文件运行多个 django python 脚本? - How to run mulitible django python scripts from .py file in VS Code Terminal? 通过 VS Code 终端运行 python 文件 - Running python file through VS Code terminal 在带有 WSL 的 VS 代码中没有运行 Python 的此类文件或目录 - No such file or directory running Python in VS Code with WSL 在 VS 代码中运行 python 文件的快捷键 - Key shortcut for running python file in VS code 在 VS Code 中运行 Python 时出错 - Error when running Python in VS Code Python-在Shell中运行与在另一个脚本中运行时对路径的解释不同吗? - Python - Interpreting paths differently when running in shell vs. when running within another script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM