Set breakpoint in imported python module in vs code - python

I am trying to set a breakpoint in an external Python module in VS code.
I have tried editing the source file and inserting import pdb; pdb.set_trace() where I want the breakpoint.
This enters the pdb command line debugger rather than the debugger in the VS Code GUI.
How do I set a breakpoint in an imported Python module so that I enter the VS Code debugger?

Marvin's suggestion appears to be sufficient:
add a launch configuration "justMyCode":false .
See code.visualstudio.com/docs/python/debugging#_justmycode

You need to import the folder containing the source code of the imported module into the project, using file -> add folder to workspace. In my case this was /Users/robinl/anaconda3/lib/python3.6/site-packages/great_expectations/
Within VS code, you can then navigate to the file you want to debug and set a breakpoint by clicking to the left of the code as normal.

Related

How can I run a .py file with its options in Python console?

I am trying to run this GitHub project in python, but I could only run it using the Terminal of Pycharm IDE.
According to the guide from the GitHub repository, I removed the $ sign from the beginning of $ python train.py RGCN PPI and could run it there. What does $ mean here and how can I run a file like this in Python Console (for example after >>> sign)?
The '$' isn't part of Python's syntax, it's a visual cue in the documentation representing the command prompt.
To answer the question from the title of this post, I'll provide some
instructions first on how to load scripts into the Python console.
However, for your specific case, you don't need this. Scroll down to
the part about debugging in PyCharm.
There's two ways you can get your script into the console. One is to simply load it using the right version of the two lines I give right below, or you can load it as a module - even if it wasn't intended to be one.
In general, to execute a script in the Python shell on Python 2 you can do
>>> execfile(r"<path to script here>")
On Python 3 it's more verbose:
>>> exec(open(r"<path to script here>").read())
The effect this has is as if you cut-n-pasted the script into the console. The console's global scope will get all the functions, classes, and variables that are leftmost indented in the file. Also it might not run your if __name__ == '__main__': block. But you could hack that.
If you want the vars/classes/etc to be put in another scope than your console's global scope, then there are two additional parameters to the above commands. The first one is a dictionary for the globals , and one for the locals. You can get away with only supplying the globals parameter - it's just an ordinary dictionary object you need.
If the file you want to load is a module, you could import it as you would any other module by appending its home folder to the Python module search path, and using the import directive. You can load your script this way even if it wasn't intended to be module.
>>> import sys
>>> sys.path.append(r'/Users/todd/projects/mymodule_folder')
>>> import mymodule
If you make modifications to it and want to reload it:
>>> import importlib
>>> importlib.reload(mymodule)
Loading your script as a module avoids polluting your console's global scope. After it loads, just prefix the names of your script's functions and variables with the module name. The module name will be the name of the file without the .py extension.
If the script requires command line options, you could just hard code values for those into the script and disable lines of code that try and get values from the CLI. If it gets complicated, consider running it in an IDE as described in the next section.
So the above is how you can run your python scripts in whatever Python REPL console you want.
BUT loading your scripts into the Python console may not be at all
required for your purposes. You wanted to debug some scripts (train.py,
test.py) from this project:
https://github.com/microsoft/tf-gnn-samples).
Debugging Command Line Script With PyCharm
In many cases, a Python script is written to run from the OS shell and take command line options from the user. These kinds of script could be loaded into the Python console, but most require some minor hacks to run. However, if all you want to do is debug such a script, you don't need to muck with the console.
PyCharm supports running these as is (as does Eclipse and other IDEs) like any other script. It's just a matter of creating a run/debug configuration for the project. I just installed PyCharm and gave it a try in order to record the details. Easy task.
Just open the project in PyCharm, and above the editor pane, on the toolbar, there's a menu option for Edit Configurations. Click that to open the Run/Debug Configurations dialog and click the + to add a configuration. A small dialog will appear with predefined templates - select Python as your template and accept.
Then in the main dialog, fill in Script path: with the path to train.py (or another script), then click the checkbox, [x] Emulate terminal in output console. Also, you can add command line options in the Parameters: text box (I put in the text: mymodel mytask just to satisfy the script's need for two parameters). Click OK at the bottom to accept the configuration and shut the dialog.
Now you should see a green bug icon on the toolbar.Set a breakpoint in the __main__ block of the script and click the debug icon to start debugging the script. That should do it!
Debugging Python Command Line Script with PDB
PDB - the Python Debugger can be run without an IDE. This is another way to debug a script of any sort. If it requires command line parameters, provide them from the OS shell when you start the debugger:
$ pdb myscript.py mymodel mytask
That's really all there is to starting a debug session. PDB requires some knowledge of its text based commands. After starting a session, you can get a listing of code near the current line of execution by entering l. Enter help to see a listing of the commands.
To step one line of execution, enter 's' for step, or enter 'step'. To set a breakpoint, enter break <line-number>, or set a breakpoint on an expression. A reference on the commands available can be found here: https://docs.python.org/2/library/pdb.html . There are also plenty of versions of pdb cheatsheets available online - just google "pdb cheatsheet" and select one.

Setting startup script in PyCharm debugger console

In PyCharm, it is possible to set a script that runs upon opening a new console (through Settings -> 'Build, Execution, Deployment' -> Console -> Python Console -> Starting script).
Is there a way to similarly apply a startup script to the debugger console? I find myself importing the same packages over and over again, each time I run the code.
When you run Python Console inside PyCharm, it executes custom PyCharm script at <PYCHARM_PATH>/plugins/python/helpers/pydev/pydevconsole.py.
On the other hand, when you run PyCharm Debug Console while debugging, it executes custom PyCharm script at <PYCHARM_PATH>/Plugins/python/helpers/pydev/pydevd.py with command line parameter --file set to script you are debugging.
You can modify pydevd.py file if you want (Apache 2 license), but the easier approach would be to create startup script in which you import modules you need, functions and such and import ALL while inside PyCharm Debug Console. This would reduce all your imports to one.
Walkthrough:
Let's create 2 files:
main.py - Our main script which we will debug
startup.py - Modules, functions or something else that we would like to import.
main.py content:
sentence = 'Hello Debugger'
def replace_spaces_with_hyphens(s):
return s.replace(' ', '-')
replace_spaces_with_hyphens(sentence) # <- PLACE BREAKPOINT!
When breakpoint is hit, this is what we have inside scope:
If you always find yourself importing some modules and creating some functions, you can define all that inside startup.py script and import everything as from startup import *.
startup.py:
# Example modules you always find yourself importing.
import random
import time
# Some function you always create because you need it.
def my_imported_function():
print("Imported !")
Inside Python Debugger Console, use from startup import * as mentioned above and you would see all modules and function inside scope, ready for use.
you could just create a new debug configuration (run > edit configurations) and point it to a script in your project (e.g. called debug.py that you gitignore). Then when you hit debug it will run that script and drop you into a console.
Personally, I prefer to just launch ipython in the embedded terminal than using the debug console. On linux, you can create a bash alias in your .bashrc such as alias debug_myproject=PYTHONSTARTUP=$HOME/myproject/debug.py ipython. Then calling debug_myproject will run that script and drop you into an ipython console.

PyCharm and IPython Notebook: Include files from project

Within my project XYZ, I have a file superSource.py, which contains some functions.
Now, I've used the new cool pyCharm feature of creating an IPython notebook, which I calltest test.ipynb, and saved it in the projects main directory (next to superSource.py).
However, when I run import superSource; foo = superSource.parameters() nothing happens, I don't even get a warning. pyCharm will underline superSource within the code though, warning me that there is no module called superSource.
How can I include other files from the same directory using the IPython notebook and/or pyCharm?
I've had the same problem and have a partial solution.
To include your file, add the following to a cell:
execfile("superSource.py").
This should load and execute it and make its contents available for reuse so that you can access variable and call functions defined or imported by it in other cells.
Unfortunately, PyCharm does not know about it, so that as you type, there is no statement completion and if you have "Show import popup" enabled in PyCharm, it will suggest adding an import but highlight it as an error afterwards. It should still work, however.

Start ipython notebook with predefined imports [duplicate]

I'm trying to to auto load the division module from __future__ on startup,
i've currently got a simple script in the IPython startup libray with the line:
from __future__ import division
which works fine when run directly from the shell,
however, the module does not appear to load when the line is run from the script,
i made sure that the startup script is loaded by adding some arbitrary variable assignments to it:
from __future__import division
x=1
y=2
and the variables were preassigned when IPython was launched (as expected).
I've tried looking at some solutions here and here but got nowhere,
any help would be appreciated,
thanks
i've found a solution to this one, in your IPython profile directory (by default - .ipython\profile_default), edit the file ipython_config.py (create it with ipython profile create if it does not exist) with the following lines:
# loads the root config object
c=get_config()
# executes the line in brackets on program launch
c.InteractiveShellApp.exec_lines = ['from __future__ import division']

How to restart pydev interactive console after a code change?

I use Eclipse with pydev for python development. I would like to test my code using pydev's interactive python console. When I make a change in the code, interactive pydev console doesn't notice it, and i have to close the current pydev console and open a new one to reflect the changes. Is there an easier way to get the changes to current active console? maybe a restart button?
Ok, I found a cool way to do it. Whenever you launch a new Pydev console; choose the option of Console for currently active editor.
Within the python prompt in the interactive console type the following
execfile('<full_path_to_your_python_script>')
Now you can experiment with the code in your script.
If you change something within the file (such as a method or class definition), then again within the same Pydev console execute the execfile statement. This will re-load the currently active editor file and you will now have access the modified code.
Note: I guess this behavior is because, the importing activity in Python are idempotent - i.e. a module can imported only once. After it has been imported, any subsequent imports for the module will refer and return the previously imported module instance. The only way to get the latest definition of the module then is to use a reload('module_name') method. I guess this is what execfile does - it reloads the python module corresponding to the currently active editor file.
Do a ctrl+alt+enter in the coding window (with a console open or not) and you will automatically load the current code for use.

Categories

Resources