How can I debug external python package in PyCharm - python

I have downloaded python package from the web (e.g., github).
The usage is via CLI, for example:
python functionality.py opt1 opt2
Since I want to understand what is going on under the hood in functionality.py
I want to be able to debug it line by line.
How can I achieve that using PyCharm?

Sorry for not writing a comment, but I don't have the right.
You could use the package in a simple script (import it). Afterwards, press CTRL+LEFT CLICK on the function/class that you have used in order to see the source code. Now you can put breakpoints my clicking on the space near the line number. The red point indicates that a breakpoint is put.
You can run your script in debug mode using the debugger from the menu up right (second icon) and see what's happening.

Related

Cannot get VS Code to pass arguments to Python from launch.json

I cannot figure out how to get VS Code for Mac to start debugging my Python script with the arguments I have defined in my launch.json file by using the debug button to the right I have circled here. The only way I can get my arguments to be recognized is if I go to the menu and select "Run|Start Debugging" or if press F5.
How do I tell VS Code to use my launch.json file when using that button???
If you look at the answer from Sourya Dey at the link below, this is what made me write this. There must be some simple setting or step missing to link that button to F5. It appears others are also confused about this.
Visual Studio Code: How debug Python script with arguments
For clarity, here is my launch.json file which works as expected with F5. It does not work with the button I have circled in red above
Update May 2022 from Microsoft tech support on github issues for vscode-python
You need to add this section to your launch.json file:
"purpose": ["debug-in-terminal"]
I just tested it on VS Code 1.67.1 and it works. The documentation appears to state that if you add this "flag" then F5 or Run > Debug won't use the launch.json settings but they do. I have asked them to update their docs to make this clearer as it is confusing.
============
I filed a bug with the Microsoft VS Code team regarding this issue and they sent me over to the Microsoft Python extension group. I then filed a bug there referencing the VC Code ticket and it was immediately assigned for "area-debugging triage". I presume this will be fixed in a later version (I am on 1.63.2)
First, it is not obvious where this button comes from. I have noted this issue as well with Microsoft so they can improve their documentation. When you install VS Code with no extensions, this button is not present. But as soon as you load your first Python file, Microsoft urges you to install the official "Microsoft Python Extension" and then the button shows up. In my mind, I assumed this was a native VS Code button that showed up when you had a language debugger installed. When I removed all extensions, except the Microsoft Python extension, I was convinced this was part of VS Code.
The answer above incorrectly states that this is the correct behaviour for the button. It is not. Microsoft has confirmed this and if you read issue #2338 ("Documentation for python.terminal.launchArgs is unclear") there is a response in there stating:
"Unfortunately that's just how vscode works. When debugging all settings are read from launch.json and non-debugging settings.json is used
This applies to all languages (extensions) in vscode."
So for now, there is nothing much to do other than wait for this to be fixed.
Lastly, for those who are wondering why I wanted to use that button in the first place. For the most part, I use keyboard mappings to start debugging when I am inside VS Code. But for this particular project, I was going to other external windows and when coming back to VS Code, since I was already using the mouse, I just wanted to click there out of convenience. I don't keep the debug tab open which also has a debug button that works as intended.

How to Auto-format Code in Python in NPP or Online

I do most of my Python coding in Notepad++. I would like a way to quickly and easily reformat my code using the default rules from PyCharm - things like spacing, number of blank lines between functions etc.
I find black playground too harsh for my requirements, and opening up PyCharm to paste code and reformat it defeats to purpose of using Notepad++ (for it's lightness).
So do you know of either a plugin for Notepad++ which does this (and I'm not just referring to Python Indent), or an online resource which applies PyCharm style formatting to pasted code?
PyCharm comes with a CLI, like documented here https://www.jetbrains.com/help/pycharm/command-line-formatter.html.
Alternatively and making the call slightly shorter: parallel to the mentioned there general batch file, you probably also have 'format.bat'. You can integrate this into Notepad++ via the [F5] button "Run...":
Put there
C:\__YourPyCharmRoot__\bin\format.bat "$(FULL_CURRENT_PATH)"
and press the [Save] button in this "Run..." dialog to assign a short-cut.
Pressing the newly defined short-cut on a saved and writable Python file should let Notepad++ ask you after a while (of running PyCharm in Windows cmd.exe) to reload the changed file .

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

Running a module from the pycharm console

I'm new to python and pycharm and I'd like to run a module from the pycharm console in the same way as you can from IDLE, if it's possible.
The idea is to create simple functions and test them "live" using the console.
...how do you do that in pycharm?
Running python scripts using pycharm is pretty straightforward, quote from docs:
To run a script with a temporary run/debug configuration Open the
desired script in the editor, or select it in the Project tool window.
Choose Run on the context menu, or press Ctrl+Shift+F10. So
doing, a temporary run/debug configuration is created on-the-fly.
Besides there is a "Python Console" available in pycharm: see documentation.
UPD:
Here's an example.
Imagine you have a python module called test_module.py:
def a(*args, **kwargs):
print "I'm function a"
def b(*args, **kwargs):
print "I'm function b"
Then, in pycharm's "Python Console" you can do this:
>>> from test_module import *
>>> a()
I'm function a
>>> b()
I'm function b
If you need to execute a part of an existing code, you can use the Execute Selection in Console feature: select the code snippet -> right click -> "Execute Selection in Console".
For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.
Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)
Right Click --> Run File In Console
Done!
Looks like in version 2018.3, this option is now Run with Python console in Run/Debug Configurations:
What you're looking for is the feature called Execute Selection in Console which is described in section Loading Code from Editor Into Console of PyCharm's online help.
Select the script lines that you want to execute and press Shift+Alt+E
You can run the Find Action shortcut (Ctrl+Shift+A or ⌘+⇧+A on mac), then type run file, and choose the option Run file in Console.
In pycharm do:
Run>Edit Configuration>Show command line afterwards
Assuming your code is in file MySimpleCode.py you can simply say
run MySimpleCode
in the PyCharm console. This assumes that you have set your working directory properly; e.g. if MySimpleCode.py is located in d:\work on a Windows system you must execute
cd d:\work
first. In my opinion the other solutions miss what the post really wants: simply executing a file like from a DOS or Unix shell, or a .m script in MATLAB. No messing with imports, projects and so on. If you use CTRL SHIFT F10 your code gets executed, sure, but in a different environment, so you have no access to variables created in your code. I assume the question means that you want to do further work with the results of the script.
Explanation for people with MATLAB background: In most Python IDEs you have to configure an interpreter first in some kind of project. The MATLAB equivalent would be a master IDE where you can choose your MATLAB version for each project. This makes it possible to run your Python code on the CPU, your GPU or even an external NVIDIA board with different settings (after several days in the installation hell). For the beginner this is very confusing, because for simple code samples any "default" interpreter should suffice. Unfortunately this is not the case for Python (2 or 3? 2.x or 2.y? which package version?), and it will get worse as you progress (which 32 or 64 bit version of TensorFlow is available for Python 3.x? and so on).

Interactive console using Pydev in Eclipse?

I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.
For example, the code stopped at a breakpoint and I want to inspect an "action" variable using the console. However my variables are not available. How can I do things like "dir(action)", etc? (even if it is not using a console).
This feature is documented here:
http://pydev.org/manual_adv_debug_console.html
The console that opens in the debug perspective is in fact interactive, although it took me a while to realize it. You need to hit return twice after typing something and then it gets evaluated.
More info on the Pydev site here: http://pydev.org/manual_adv_debug_console.html
Double click on "action" or any other variable.
ctrl+shift+D
And if you're using watches, I cant imagine better interaction. You are able to see every change.
When I set a break point and hit F11 Eclipse launches the debugger and prompts to open the "Debug Perspective". You can then open the Window-->Show View --> Expressions which opens the expressions view, you can then right click in the Expressions view windows and choose "Add Watch Expression" to add any expression(such as dir)
Conversely I was also able to type in dir(some expression) in the PyDev console and get the same effect.
I'm using PyDev 1.4.6.2788
On a small monitor, you may not realize that the debug interactive console is different from the regular interactive console: it has a second command prompt at the bottom where you type, not at the top like the normal console.

Categories