VSCode上的Python与PyChamp或控制台Python之间的差异



一点上下文。。。我是python的新手,所以,由于我是python语言的新手,我通过谷歌搜索最好的IDE来开始使用python,许多网站推荐Pycharm,所以我开始使用它,但由于我在工作中使用VSCode,我决定尝试它,但我遇到了一些问题,因此我目前无法使用它。让我解释一下,也许有人知道可能的解决方案的原因。

我目前正在运行Python 3.10.2

在我的一个脚本中,我使用argparse来接收一些执行参数,特别是一个名为"的参数;过滤器";所以在argparse中,我定义了这个参数如下:

parser.add_argument('-f', '-filter', '-F', '--FILTER', dest='filter', action='append',
help='Apply column filters according to the operator en value provided')

基本上,这个想法是接收一个、NAME、OPERATOR和VALUE进行过滤。即

-f TERM_LN!=PLUSS (this works fine in both IDE and python console)
-f CRNCY_CDE=032 (this works fine in both IDE and python console)
-f AMOUNT>=0,02 (this only works on PyCharm and console BUT not in VSCode)

通过调试脚本,我注意到argparse.parse_args()函数返回AMOUNT筛选器,如下所示筛选器:['AMOUNT>=0,2']VSCode中(不正确),而在Python控制台PyCharm上,我看到它类似于

['AMUNT>=0.02']顺便说一句,当我从控制台调用脚本时,我会传递这样的参数。。。

(venv) C:devPythonProjectsPyAuthomationvenvScripts>python ../../generator.py "-f AMOUNT>=0,02" ../../Info.xls --VERBOSE (Verbose is only for getting diagnostics info of the running script)

在Pycharm中,我通过选项Run->为脚本配置了参数;编辑配置。。。在参数选项中,我引入了

"-f AMOUNT>=0,02" Info.xls --VERBOSE

从我在互联网上发现的VSCode中,为python脚本的执行提供参数的方法是创建文件";启动.json"。

这是我的实际配置。。。

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"-f AMOUNT>=0,02",
"INFO.xls",
"--VERBOSE"
]
}
]
}

我还注意到,Console或Pycharm只返回实际的参数值,而VSCode也在-f和过滤器名称之间返回SPACE字符,就好像空格字符是实际参数名称的一部分一样,尽管我没想到这很容易解决。

另一方面>"事实并非如此。我不知道什么是正确的方式来配置VSCode以接受像PyCharm或Python控制台那样的纯文本参数类型。似乎是某种解析导致了这个问题,但我不知道为什么,也不知道任何可能的解决方法。

如果我把">"对于"="一切如预期。

提前感谢问候

您的问题不在于argparse,而在于各种输入的方式;"分割";通过外壳或侧面。

我有一个简单的脚本,它呼应了argparse使用的sys.argv列表:

0012:~/mypy$ cat echo.py
import sys
print(sys.argv)

只需使用linux终端窗口(控制台)

0012:~/mypy$ python3 echo.py -f TERM_LN!=PLUSS
['echo.py', '-f', 'TERM_LN!=PLUSS']
0014:~/mypy$ python3 echo.py -f CRNCY_CDE=032
['echo.py', '-f', 'CRNCY_CDE=032']
0014:~/mypy$ python3 echo.py -f AMOUNT>=0,02
0015:~/mypy$ ls =* 
'=0,02'
0015:~/mypy$ cat =0,02
['echo.py', '-f', 'AMOUNT']

最后一次将输出重定向到文件1,原因是shell如何解释">">

引用以阻止重定向。这在shell中经常需要阻止特殊字符处理:

0018:~/mypy$ python3 echo.py -f "AMOUNT>=0,02"
['echo.py', '-f', 'AMOUNT>=0,02']

请注意,在所有这些字符串中,"-f"是一个单独的字符串。所以你的json可能应该是:

"args": [
"-f",
"AMOUNT>=0,02",
"INFO.xls",
"--VERBOSE"
]

编辑

我不太确定你所说的"python控制台"是什么意思,但由于我通常使用ipython来测试和演示代码,我可以用它的%run魔力运行脚本。

0845:~/mypy$ ipython3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.0.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: %run echo.py -f AMOUNT>=0,02
['echo.py', '-f', 'AMOUNT>=0,02']

这不是在使用bashshell,因此不会重定向。

使用它的外壳魔法,我需要报价:

In [4]: !python3 echo.py -f "AMOUNT>=0,02"
['echo.py', '-f', 'AMOUNT>=0,02']

我使用CCD_;直的";,但从其他SO问题来看,人们在其他IDE中使用,如spyder。我没有注意那些细节。我使用Geany作为我的主要脚本编辑器。您还会看到许多Jupyter的使用,其中包括ipython和基于浏览器的notebooks

由于您(显然)使用的是Windows,您的shell交互可能会有所不同。

包含<>的参数的问题是cmd

Python扩展在字符串中向cmd传递命令

cd <somedir> && cmd /C "bit debug command with parameters"

cmd以某种方式删除了<>,并使其成为stdin和stdout的文件重定向。我还没有找到逃离>的方法,以防止cmd在与/C一起传递时将其移除。

解决方案是使用任务启动调试器并将VSC附加到此调试器。在该任务中,您可以添加"以防止外壳cmd>作为文件重定向进行处理。

如何在中解释:如何重定向VSC python代码中的输入和输出,以在task.json中找到<path>部分所需的文本,并且您可能需要更改扩展名ms-python.python-2021.12.1559732655

对于您的情况,我使用以下

task.json

{
"version": "2.0.0",
"tasks": [
{
"label": "Script for filter",
"type": "shell",
"command": "${command:python.interpreterPath} <path>\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy --listen 5678 --wait-for-client ${file} -f "AMOUNT>=0,02""
"problemMatcher": []
}
]
}

注意参数-f "AMOUNT>=0,02"的传递。您必须添加"以防止终端shell/cmd创建文件重定向。您必须在JSON字符串中转义"

启动.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach to Process port 5678",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
},
{
"name": "Python: Start Process port 5678",
"type": "python",
"request": "launch",
"code": "print()",
"preLaunchTask": "Script for filter"
}
],
"compounds": [
{
"name": "Debug filter",
"configurations": [
"Python: Start Process port 5678",
"Python: Attach to Process port 5678"
]
}
]
}

在调试栏中选择Debug filter并启动调试会话。


编辑

刚刚发现,如果你使用Powershell作为你的集成终端,命令参数都会被'包围

您只需将-f AMOUNT>=0,02拆分为args数组的两个元素,即可确保它们作为两个单独的参数传递。

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"-f",
"AMOUNT>=0,02",
"INFO.xls",
"--VERBOSE"
]
}
]
}

最新更新