Home > Back-end >  Difference between Python on VSCode vs PyChamp or Console Python
Difference between Python on VSCode vs PyChamp or Console Python

Time:03-15

A little bit of context...I'm new to python so, Since I am new to the language I've started by googling the best IDE to use with python, many sites recommended Pycharm so I started using it, But since I use VSCode at work I decided to try it but I've experienced some issues with it, and for that reason I not able to use it at the moment. Let me explain maybe someone knows the reason of a possible solution.

I'm currently running Python 3.10.2

In one of my script I use argparse to receive serval execution parameters, in particular a parameter called "FILTER" so in argparse I have defined this parameter as follows:

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

Basically the idea is to receive a, NAME, OPERATOR and VALUE to filter. ie.

-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)

By debugging the script I've noticed that argparse.parse_args() function returns the AMOUNT filter as follows... filter:[' AMOUNT,02'] in VSCode (Incorrect) whereas on the Python console or PyCharm I see it like [' AMOUNT>=0,02'] (Correct)

Just as a side note, when I invoke the script form the console I pass the argument like this...

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

From Pycharm I've configured the parameters for the script from the option Run -> Edit Configurations... in the parameter option I introduced

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

From VSCode from what I've found on the internet the way to provided the parameter for the execution of the python script is by creating the file "Launch.json".

This is my actual configuration...

{
    "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"
            ]
        }
    ]
}

I've also noticed that Console or Pycharm only returns the actual parameter value whereas VSCode also return SPACE character between the -f and the Name of the filter as if the space char was part of the actual parameter name, even though I was not expecting that it's easy to solve.

On the other hand, the issue with the ">=" it's not. I have no idea what's the correct way to configure VSCode to accept such types of parameters as plain text just like the PyCharm or the Python console does, It seems that some kind of parsing is causing the issue, but I have no idea Why or any possible workaround for it.

One additional note if I change the ">=" for "=" everything works as expected.

Thanks in advance Regards

CodePudding user response:

Your problems aren't with argparse, but with how the various inputs are "split" by the shell or ide.

I have a simple script that echos the sys.argv list that argparse works with:

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

Just using a linux terminal window (console)

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']

The last redirected the output to a file1 because of how the shell interpreted the '>'

quoting to block the redirect. This is often needed in shell to block special character handling:

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

Notice in all of these the '-f' is a separate string. So your json probably should be:

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

CodePudding user response:

The problem with arguments containing < and > is cmd.

The Python extension passes a command to cmd in a string

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

cmd somehow removes the < and > and makes it file redirections for stdin and stdout. I have not found a way to escape the > to prevent cmd from removing it when passed with /C.

The solution is to use a task to start the debugger and attach VSC to this debugger. In the task you can add " to prevent the shell cmd to process the > as a file redirect.

What to do is explained in: How do I redirect input and output in VSC python code to find the text needed for the <path> part in task.json, and you might need to change the extension name ms-python.python-2021.12.1559732655

For your case I use the following

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": []
    }
  ]
}

Watch the passing of the argument -f \"AMOUNT>=0,02\". You have to add " to prevent the terminal shell/cmd to create a file redirect. You have to escape the " inside a JSON string.

launch.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"
      ]
    }
  ]
}

Select Debug filter in the debug bar and start the debug session.

  • Related