Difference between Python on VSCode vs PyChamp or Console Python - python
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
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"
]
edit
I'm not quite sure what you mean by 'python console', but since I usually use ipython to test and demonstrate code, I can run a script with it's %run magic.
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']
That is not using the bash shell, so doesn't redirect.
Using its shell magic I need the quotes:
In [4]: !python3 echo.py -f "AMOUNT>=0,02"
['echo.py', '-f', 'AMOUNT>=0,02']
I use ipython "straight", but judging from other SO questions, people use in other IDEs, such as spyder. I haven't paid attention to those details. I use Geany as my main script editor. You'll also see a lot of Jupyter use, which includes ipython, and browser based notebooks.
Since you are (apparently) using Windows, your shell interactions may be different.
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.
Edit
Just found out that if you use Powershell as your integrated terminal the command arguments all get surrounded with '
You only have to split the -f AMOUNT>=0,02 in 2 elements of the args array to make sure they are passed as 2 separate arguments.
{
"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"
]
}
]
}
Related
VS Code's Python Debugger doesn't hit the breakpoint for Odoo 10
I use VS Code extension Python version 2.2x, Python interpreter version 2.7x, and use Odoo 10 of the latest version. I'm using WSL with Ubuntu 18.4 LTS. I cannot debug the custom modules my company creates. I've specified the module's path in the argument, and it does run but it's not breaking at the breakpoints I specified. Here's my launch.json: { "name": "Python: Current File", "type": "python", "request": "launch", "program": "/home/ihsansfd/odoo/odoo-bin", "python": "/usr/bin/python", "args": [ "--db_port=5434", "--addons-path=/mnt/d/kuliah/odoo/repo/MUK/base,/mnt/d/kuliah/odoo/repo/MUK/core,/mnt/d/kuliah/odoo/repo/MUK/modifier", ], "console": "integratedTerminal", "justMyCode": true }, Aside from request launch, I also tried attach and using a pip library debugpy for that but it's still only running without debugging. I am really sure that it should hit the breakpoint because I've set a print statement there and it printed! Any help would be appreciated. If you need any further detail please do ask.
Although you mentioned you've tried using attach with debugpy, I'm sharing my configuration since attach and debugpy is what I use every day without any issues. Here is the shell command I use to run odoo via debugpy. python3 /debug/debugpy --listen 0.0.0.0:5678 <odoo-bin-path> <odoo-args> Change python3 to just python for your use case. Also change 0.0.0.0:5678 to whatever you need as well. I like to run Odoo inside a Docker container, and that's also the reason why I prefer to simply attach to the process rather than launching it right from VS Code. I installed debugpy to /debug/debugpy using this command: python3 -m pip install debugpy -t /debug Here is the launch configuration I use in my launch.json: { "name": "Attach to Odoo", "type": "python", "justMyCode": false, "request": "attach", "host": "localhost", "port": 5678, "pathMappings": [ ... I need path mapping for my setup to map the location of my local Odoo source code directory to the location of the Odoo source code directory inside of the Docker container. Depending on your setup, you might be able to just skip this option. ... ] } Hopefully this helps!
VS Code - Debugger gives module error, but running same command from terminal works
So I have this launch.json launch.json { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Run main.py", "type": "python", "request": "launch", "program": "${workspaceFolder}/main.py", "console": "integratedTerminal", "justMyCode": true, "args": ["-o", "storage/test", "-b", "projects/robustnav_baselines/experiments/robustnav_train", "objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo", "-trd", "dylan/subsample_task/1_percent/train"] } ] } Which essentially runs this command python main.py -o storage/test -b projects/robustnav_baselines/experiments/robustnav_train objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo -trd dylan/subsample_task/1_percent/train. When I run this command in the vs code terminal it runs fine, but when I use the debugger associated with the launch.json file. I get this exception: allenact/init.py Then as we can see workspaceFolder is at the root So I'm not sure why it's not finding the allenact module even though you can clearly access it from the root as it is a folder accessible in the root with an __init__.py file. Here is my terminal output when I run debugger. VS code will output commands in terminal when you run debugger: cd /home/dyung6/robustnav ; /usr/bin/env /home/dyung6/anaconda3/envs/robustNav/bin/python /home/dyung6/.vscode-server/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/launcher 33361 -- /home/dyung6/robustnav/main.py -o storage/test -b projects/robustnav_baselines/experiments/robustnav_train objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo -trd dylan/subsample_task/1_percent/train
Open the "run and debug" tab from the sidebar and at the bottom you would find a section called "breakpoints". Under that, uncheck "raised exceptions" and "user uncaught exceptions".
How to launch Windows Terminal as external terminal for debugging a Python file?
Windows 10 VS Code 1.49.0 My settings.json: "python.pythonPath": "C:\\Python38", "terminal.integrated.shell.windows": "C:\\PowerShell Core\\pwsh.exe", "terminal.explorerKind": "external", // NOTE: I have a space in my username "terminal.external.windowsExec": "C:\\Users\\USER NAME\\AppData\\Local\\Microsoft\\WindowsApps\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\wt.exe", My launch.json located in the root of my workspaces .vscode directory: { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "externalTerminal" } ] } I'm trying to launch the current Python file in Windows Terminal when debugging. With my settings as is, I receive the following error (modified my username in the image and I have a space in my username): But if I place extra escaped double quotes(\") in settings.json for terminal.external.windowsExec: "terminal.external.windowsExec": "\"C:\\Users\\USER NAME\\AppData\\Local\\Microsoft\\WindowsApps\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\wt.exe\"", I receive a different error message: I would appreciate some help on getting Windows Terminal to launch in VS Code when I run debug on a Python file. SIDE NOTE For Ctrl + Shift + C usage in VS Code, if I don't add the the extra escaped double quotes to the path of wt.exe for terminal.external.windowsExec, then Windows Terminal launches properly. If I add the escaped double quotes, I get the same error as the first attach image stating Windows cannot find 'C:\Users\USER '.
launch.json should have "terminal": "external" or "console": "externalTerminal" settings.json should have "terminal.external.windowsExec": "wt cmd", or "terminal.external.windowsExec": "wt -p \"Command Prompt\" --title \"VSCode External Terminal\" cmd", the \"Command Prompt\" is your windows terminal's profile name you want to use. https://www.reddit.com/r/vscode/comments/ifou0y/using_new_windows_terminal_as_external_terminal/gaqrjt8?utm_source=share&utm_medium=web2x&context=3
Visual Studio Code: run Python file with arguments
Is there an easy way to run a Python file inside Visual Studio Code with arguments? I know I can add a custom configuration in the launch.json file with the args keyword. However, it is annoying to modify the launch.json file every time just because I want to use different arguments.
Visual Studio Code only supports one launch.json file. However, it supports two or more configurations, and they appear in the left-hand menu/pane's drop down list (instead of "No Configurations"). In the DEBUG pane, either click the Config button circled in red above or click the blue link "create launch.json file": Click it and it creates a launch.json file with debugging configurations. Edit this file and add the args in this key-pair format AND add multiple for different args including Variable Substitution! { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Current File with my args", "type": "python", "request": "launch", "program": "${file}", "args": [ "--username", "Jeremy", "--account", "Stackoverflow" ], "console": "integratedTerminal" }, { "name": "Python: Current File with UserName arg", "type": "python", "request": "launch", "program": "${file}", "args": ["${env:USERNAME}"], "console": "integratedTerminal" } ] } Put a breakpoint in your Python script, for example on the first line under def main(...) and then press F5 or click Run Menu > Start Debugging.
A workaround is to have your script ask for the command-line arguments (in the internal Visual Studio Code console). This can be made much more usable by leaning on readline, which allows you to do things like press the Up arrow key to cycle through previous commands (command history), and more. An example: import argparse, readline def main(): # Ask for additional command line arguments if needed (for VSCode) parser = argparse.ArgumentParser() parser.add_argument('--interactive', action='store_true', default=False) (args, rest) = parser.parse_known_args() if args.interactive: try: readline.read_history_file() except: pass rest += input("Arguments: ").split(" ") # Get input args try: readline.write_history_file() except: pass # Your other script arguments go here parser.add_argument("-output-dir", default="/out") # ... args = parser.parse_args(rest) print(args) if __name__ == "__main__": main() Then just set up Visual Studio Code to always pass in the --interactive argument, and your script will always ask for arguments (with history!) even as you set breakpoints.
You can add a custom task to do this. This deals with the tasks.json. You can add a default tasks.json file for you project (project folder). Follow these steps. Keyboard press Ctrl + Shift + B. It will prompt the following popup Click on the Configure Build Task If there is already a custom tasks.json file created in the following location .vscode/tasks.json editor will open it. If not, it will give a drop down of suggestions of already existing task runners. Our intention is to create a custom tasks.json file for our project, so to create one we need to select the Others option from the drop down. Check the screenshot below. Once you select the Others option, you could see a default tasks.json file will get created from the root directory of the project to the location .vscode/tasks.json. Below is an example of tasks.json. Now edit the tasks.json file to support Python. Change the Command property from "echo" to "Python" Keep showOutput as "Always" Change args (arguments) from ["Hello World"] to ["${file}"] (filename) Delete the last property problemMatcher Keep isShellCommand and version properties as unchanged Save the changes made You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B.
If you don’t have a task.json file in your project you can create a new one with press Ctrl + Shift + B. Then choose the first option showing to you then replace all of them with the below: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Run Python with argument", "type": "shell", "command": "python PROGRAM_NAME.py ARG1 ARG2 ...", "group": { "kind": "build", "isDefault": true } } ] } Otherwise add the above configuration in your existed tasks.json file. Replace PROGRAM_NAME in the above configuration with your program name and ARG1 ARG2 ... indicate your specific arguments. After all, you can execute your created task with Ctrl + Shift + B and choose the new "Run Python with argument" task.
Another option is you can run your python program from the commandline via python3 -m debugpy --wait-for-client --listen localhost:5678 myprogram.py Then you can use a Python: Remote Attach launch.json configuration to connect to the program. It means an extra step every time to you turn on your program: run the program on the CLI then attach debugger, but it does make specifying the arguments more fluid. To make things even simpler, you could put the above in a bash script and pass through args from CLI to python: start.sh "my arg that changes all the time" Then attach via "Remote Attach".
I have looked for a solution to the issue described here but don't think any of the answers are sufficient so I have created a debugpy-run utility to solve it. If you have the VS Code Python extension installed then the full debugpy debugger is already bundled with it. The utility finds the path where debugpy is installed and then runs it for program and arguments you specify, in listen mode. Connect to it from within VS Code using the Python "Remote Attach" debug configuration (using the default host and port settings). You can just control+c and then re-run the command with changed arguments using your shell history and command line editing facilities, for each debug run.
In addition, to xdhmoore's answers, for those not too familiar with creating configurations in launch.json, it should look as follows: "configurations": [ ..., { "name": "Python: Remote Attach", "type": "python", "request": "attach", "host": "127.0.0.1", "port": 5678 } ] To start debugging, first issue the command line command in the terminal and then launch the (newly created) launch configuration Python: Remote Attach from the debug launch menu in VS Code.
I had a similar problem in VS 2019. I wanted to start python file with arguments in environment generated by VS. I wasn't able to find good simple solution, so basically I went to ENV\Scripts folder, opened terminal there and run python "FullPath\MyFile.py" -some arguments It works OK, but VS needs to be closed.
If you are using a virtual environment, be sure to use the full path to the environment's Python interpreter.
Maybe this will do what you want: create a python.bat file in your %PATH% that points to python.exe: C:\Users\joecoder\AppData\Local\Programs\Python\Python37\python.exe %* Then use a Visual Studio Code terminal window (you can open a new one from the Terminal tab at the top of Visual Studio Code) to change directory to where your .py file resides, and run as normal: PS C:\Users\joecoder> cd vscode\python PS C:\Users\joecoder\vscode\python> python test.py 1 2 3 Of course this runs outside of Visual Studio Code, so be sure to write out changes after edits and you'll have to use print() style debugging.
Configure Visual Studio Code to run Python in bash on Windows
I want to run python .py file in Visual Studio Code using Windows bash console. What I tried to do: Change default shell in settings.json: { "terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe" } Add task in tasks.json to run python command with file name as an argument: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "python", "isShellCommand": true, "showOutput": "always", "tasks": [ { "taskName": "Run python in bash", "suppressTaskName": true, "args": ["${file}"] } ] } There are a few problems to solve here: Tasks are not being run in bash as I wanted To access C drive I need to replace C:\ with /mnt/c in file path Can you share with my solutions to those problems?
I don't have Windows 10 with bash but I'd imagine the problem is that you're not actually trying to run Python. You're trying to run bash (and then run python). Try setting the command to bash with params ["python", "$file"].