Breakpoints in callbacks are not hit in Python in VS Code - python

I am new to Python and doing some development on Windows using Python 3.7 + Anaconda + VS Code. Now when I debug my Python program by hitting F5 with a debug configuraiton like this :
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode":false,
"env": {
"PYTHONPATH": "PATH\\TO\\PYTHON"}
}
I discovered that the breakpoints I placed in the callback functions are not hit, while the "normal" breakpoints are. The callbacks are definitely called because I can see the output from the terminal. It should be noted that only callbacks from external packages are not paused by the breakpoints. The callback functions that I created myself do not have this kind of problem. The function can be paused if I place a line breakpoint() but obviously it makes the debug routine very clumsy.
I have searched the web but didn't have too many references of this problem. Anyone happenes to see this before?
UPDATE:
Here I have attached some code and steps I used when launching and debugging. The code looks like this:
class MyDerivedClass(BaseClassOfExternalPackage):
def __init__(self, settings : ExternalPackageSetting):
if settings:
super(MyDerivedClass, self).__init__(settings)
def onConnect(self, msg):
# breakpoint()
self.write_log("Connect success. " + msg)
And the function onConnect is a callback defined in the BaseClassOfExternalPackage from external package. So the breakpoint I put in the self.write_log line, never gets hit although the log is written successfully. And if I uncomment the breakpoint line, the program is paused successfully.
How I debug the program: I click the side-bar of VS-code to select the python executable that I set for this conda env. And the configuration part "env": {"PYTHONPATH": "PATH\\TO\\PYTHON"}, PATH\\TO\\PYTHON is no conda env path, but the root folder of my program (if I don't set it like this it won't import the modules I write)

Related

Cannot launch debugger for azure function app in vs code

I am trying to debug an azure function app in VSCode using Python in a Windows10 environment. Whenever I launch the debugger it hangs for a long period of time and then opens a message box that says
ECONNREFUSED 127.0.0.1:9091
There are a bunch of posts about this but none seem helpful/can solve the problem. Here is what I've tried:
uninstalling and re-installing different versions of
azure-function-core-tools using windows installer, npm and chocolatey
uninstalling and re-installing Azure Functions extension in VS Code
changing the extension bundle
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
}
modifying local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}
deleting C:\Users\admin.azure-functions-core-tools\Functions\ExtensionBundles
creating a function app from command line using "func init" and lauching debugger by running "func host start" in active venv
I am using Python38 and really have no idea what else to try. Any suggestions are welcome.
Thanks!
Cannot launch debugger for azure function app in VScode-
ECONNREFUSED 127.0.0.1:9091
This type of generic error may occur for a variety of reasons.
Need to check and modify:
First and foremost, check whether the versions of Azure functions core tools and Pip are upgraded to the current version:
To upgrade pip:
python -m pip install --upgrade pip
To install and upgrade azure-functions:
pip install azure-functions
Go to the below path,
view -> Command palette -> User Settings
Python functions, task runFunctionsHost windows command only work with powershell:
Set the integrated > default profile: Windows to PowerShell as PowerShell runtime host is functional with Python functions. It was previously set to "null".
The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder.
As stated here , the default listening port in launch.json is set to 9091. Here, I updated it to "port: 7071" which is open for traffic on my function project, launched the files, and then executed the "Attach to Python Functions" debug task once again.
Under .VScode folder -> launch.json file, this configuration changes works for me.
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "python_modules./.bin/func",
"console": "integratedTerminal"
},
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 7071,
"preLaunchTask": "func: host start"
}
]
}
Added multiple debug points, debugged and triggered successfully as shown below:
Also Check here for more approaches given by #Hari Krishna
found the solution at:
https://github.com/Azure/azure-functions-core-tools/issues/3160#issuecomment-1266273749
I ran command func start in verbose mode
func start --verbose
from there it was clear that the process timed out when trying to download a new extension bundle. Most likely due to slow internet. I manually installed the new extension bundle:
https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/3.15.0/Microsoft.Azure.Functions.ExtensionBundle.3.15.0_any-any.zip
(the full path should be in the --verbose output) and extracted to
C:\Users[user name].azure-functions-core-tools\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\3.15.0
It now works. Thanks everyone for input.

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!

How to run Run only a Single test in Visual Studio Code and Python with output

Does anyone know how to run only one python test with Test Explorer in Visual Studio Code?
I have installed https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter.
I am using pytest.
In the picture below:
If I press "Run Test" then only one test runs, but I do not see any log / output.
To see the output I have to press "Run", but this runs all my tests. This is incredibly frustrating.
I would like to be able to run only the one test with the Visual Studio Code interface and see the log for this run.
I did not find one elegant way to just run one test and then see the correct sampled/collected output from that test, but I found a way to run only the test I was working on by tagging it with:
#pytest.mark.run_this_test
and hit F5 anywhere in VSCode to just run this test over and over again. The PyTest debug config looks like this:
{
"name": "PyTest",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "pytest",
"args": [
"-s", "-m run_this_test"
],
"justMyCode": false,
"pythonPath": "${config:python.pythonPath}",
"env": {
"PYTHONPATH": "${workspaceRoot}"
}
This 'tagging' does not scale in a team obviously, but it works for me alone.
I created a repo containing my solution: https://bitbucket.org/geircode/vscode-testsetup-python
To test it:
run the script "requirements.install.bat"
open vscode in the root folder
select "PyTest" in the "debug and run" tab
Hit F5 to start debugging and see the correct output from the test
Check the Output Panel for output (look under the View menu if you're unfamiliar with it). There's probably a channel there where test output is sent (e.g. for the Python extension for VS Code it creates a "Python Test Log" channel for all test output).
From VS Code test explorer it's possible. Hope this helps.

Python: Cannot debug using vs code

I am new to python as well as anaconda, I installed and setup everything including environment variables.
then i opened up vs code and typed in
print("hello world")
and tried debugging.
I did not get any output and got a couple of timeout on debugger.
this is what was on terminal after i ran the debugger:
(base) C:\Users\Arun>cd e:\pythontutorials && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && E:\Python\pythonw.exe c:\Users\Arun.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd_launcher.py --default --client --host localhost --port 62293 e:\pythontutorials\firstprogram.py "
This probably is a path error. As I have encountered in the past VS Code doesn't allow you to run python commands from its terminal unless its paths are also set in the system variables. Do recheck these.
Install Python by microsoft, seems you already have
open up your settings ctrl + , search for python path and change the python configuration > python path to python3
From your debug tab, click on settings icon and add the code below to your launch.json file, save it and make sure it is selected as your config in the debug tab.
{ "name": "Python: Current File (Default Debug)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "none"
}
If all goes well, you should be able to debug from the DEBUG CONSOLE` not the terminal

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.

Categories

Resources