How can I see the global environment in pycharm similar to Rstudio? - python

I have been work with R in Rstudio since 2013, but now I decide to move to Python and I have been used Pycharm IDE. This IDE is very stable and friendly, but I can't see the objects and the results of the code processing.
My question is: How can I see the global environment (like Rstudio). It's important to see what my code has doing.
Any idea to solve this problem?

You can use the Python Console (Tools --> Python Console). On the left side of the python console, there is a button called Show variable (Below the question mark button). It will allow you to see the global variables like RStudio.
If you are writing a script, you can use the Debugger. You can read more about it here

Maybe a bit too late, but for those who just see this, pycharm now supports the scientific mode, which for me is even better compare to Rstudio(R)

Related

Dig into variables while debugging

I am currently debugging code using Python. I have not been using Python for a while. I put some breakpoints on a variable which is an integer. Let's say this variable is X = 10. How can I:
see what is in the variable? (I can highlight and a yellow case appears but if there are a lot of information it is not practical to display like this)
do some manipulation of the variable, for example I would like to do X+2 and get the result?
As noted in the comments, there are many possible IDEs you can use with Python. The original question was specifically about Eclipse and so my answer focuses on a solution using that IDE. Other solutions are possible if you prefer a different environment...
First off, you need to sort out which plug-in you use for eclipse. You have a few options as you can see on the python wiki. It looks like PyDev is more popular, but you could pick others.
Assuming you go for PyDev, you can use the watch facility as described here to evaluate any expression. Alternatively you can use the console debugger to evaluate code directly in the debugger.
EDIT: As per the comments, you can also open the Expressions window using Window > Show View > Other > Debug > Expressions. This is the same window as used for the watch facility and the contents can be edited directly.
I'm sure everyone has their own opinion, but I find the python tools for Eclipse quite complex. If you aren't tied to an Eclipse IDE, try PyCharm. It has very good tools for doing exactly what you want, and many tutorial videos. This video shows you how to do exactly what you have requested: https://www.youtube.com/watch?v=QJtWxm12Eo0
I don't think you can do this purely in Eclipse, not without extensive plugins. However, there are some very good python debugging tools out there.
If you're willing to get your hands dirty, the builtin python debugger pdb does everything you want and more. Import pdb, pdb.set_trace() where you want to break, and you step into an interactive debugger that's very familiar if you've used gdb before.
If you want to use something more IDE focused, I recommend Immunity Debugger. It's really rather good, and has a lot of documentation. This might be a little more than you're looking for though.
Try opensource, free VS Code (with Python plugin). It has a slick intellisense feature and you can watch, inspect the variables, debug them. The editor is node.js based and can work in every platform. Eclipse is overkill for python development IMHO.
ipdb is a handy tool for python debugging:
ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module.
To install ipdb, simply run pip install ipdb --user in your shell.
To set breakpoint, add import ipdb; ipdb.set_trace() before the line where you want to jump into the debugger. E.g.:
import ipdb; ipdb.set_trace()
X=10
Once you run your program python myfunc.py, an IPython-like interactive shell will be triggered and you can run python commands in it. E.g.:
ipdb> p X
10
ipdb> X+2
12
Here is a simple tutorial: An Introduction to Python Debugging

In Python, how do I debug with an interactive command line (and visual breakpoints?)

I've recently moved from Matlab to Python. Python is a much better language (from the point of view of a computer scientist), but Python IDEs all seem to lack one important thing:
A proper interactive debugger.
I'm looking for:
The ability to set breakpoints graphically by clicking next to a line of code in the editor.
The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc.
When an error occurs, the debugger should automatically open an interactive console at the error line.
Once done with the interactive console, you can resume normal execution.
Matlab has all these features and they work incredibly well, but I can't find them anywhere in Python tools.
I've tried:
PyCharm: the interactive console is clunky, often fails to appear, and crashes all the time (I've tried several different versions and OSs).
IPython: can't set breakpoints -Launching a Python console programatically: you have to stop your code, insert an extra line of code, and run again from the beginning to do this. Plus, you can't access functions already imported without re-importing them.
Being able to debug and fix problems THE FIRST TIME THEY APPEAR is very important to me, as I work in programs that often take dozens of minutes to re-run (computational neuroscience).
CONCLUSION: there is NO way to do all of these in Python at the moment. Let us hope that PyLab development accelerates.
At the top of your code, write
import pdb
And within your code, use the following statement wherever you want to debug.
pdb.set_trace()
You will have an interactive shell thus, whenever the set_trace() statement is met.
You can then use step(s), next(n), continue(c) and so on to check the execution flow, and print values of variables like print var
For more details on pdb, refer here
There are many Python IDEs. That was a topic here: What IDE to use for Python?
"The ability to set breakpoints graphically by clicking next to a line of code in the editor."
PyDev has this. Double-click in the gray margin bar.
"The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc."
PyDev has this. It's not the only one. PyScripter's stated features seem to include this.
"When an error occurs, the debugger should automatically open an interactive console at the error line."
PyDev does this. (I think. Or at worst do you need to double-click on the console message that states the error's location in the code?)
"Once done with the interactive console, you can resume normal execution."
PyDev has this. It's called "resume". It's what the green "play" triangle in a toolbar does. Some other software calls this feature "continue".
I've been searching for the same, but unfortunately Python IDEs are not as well-featured as Matlab's at this point. For scientific programming, you will also want graphics/plotting to run in an entirely different thread, so IPython integration is essential. As far as I can tell, the Matlab IDE feature to change the workspace from the debugger, which then affects code running subsequently, is quite unique. Each of the features exist in some IDE, but none exist in all:
Spyder has good integration with scientific tools, but its debugging is limited to the built-in pdb, which lacks the requirement to execute any code and have this code affect the namespace after continuing.
PyDev and PyCharm, and quite a few others, have decent debugging features, but I don't think it has good integration with scientific tools. That means, if you plot, you lose access to your prompt. Not good.
As far as I've experienced, the closest comes Wing IDE. It is a propietry product, but if you're making the transition from Matlab 89$/year for non-commercial-use should be acceptable (you can evaluate it first). But for me, I've ultimately settled to alter my workflow, and not using any sophisticated IDE at all. When I looked was some years ago, so perhaps the situation has improved.
You might also be interested in this article from April 2013, Evaluating IDEs for Scientific Python. It doesn't really reach a conclusion either.
Seeing as you are comming from Matlab, I would suggest you give a look at
Python(x,y)
The page decribes it as follows:
Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces and Spyder interactive scientific development environment.
It will not cater to all your wishes, but it certainly made me feel comfortable when I started out with Python, coming from Matlab.
Hope it helps
You can do all this in the iPython Notebook. Use the magic command %pdb to stop on error.
If using the command line,
alias ipythondebug='ipython --InteractiveShell.pdb true'
in your ~/.profile will give you debug on error like Matlab. This of course requires ipython installed.
Not sure about the resuming part.
You can also edit the ipython config file if you want the debug on error to be permanent. See
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb

Is it possible to implement automatic error highlighting for Python?

Are there any IDEs for Python that support automatic error highlighting (like the Eclipse IDE for Java?) I think it would be a useful feature for a Python IDE, since it would make it easier to find syntax errors. Even if such an editor did not exist, it still might be possible to implement this by automatically running the Python script every few seconds, and then parsing the console output for error messages.
eclipse+pydev
pycharm
many others ....
If you use VIM or don't have a problem with it, try this extension. https://github.com/klen/python-mode
This is for Emacs as well: https://github.com/gabrielelanaro/emacs-for-python
Also pycharm and eclipse with pydev work fine.
If I don't use vim I really enjoy spyder. It is easy to use and has some really nice features, like integrated debugging and profiling, graphical variable explorer and object inspector. The latter shows, e.g., the integrated documentation for every function of class you use.
I built an extension to Eclipse and PyDev that does what you describe, it runs the Python code as you're typing, and displays all the variable values and any exceptions that occur. It's called Live Coding in Python, and the web site has a tutorial and a demo video.
PyDev can highlight some problems in your code by analysing it, and Live Coding in Python can show you problems that happen when you run it.

Experienced Python programmers (ESPECIALLY former php programmers) : How do I debug python?

In PHP, it was extremely easy to start hacking away and figuring out what was going on on a page. Just throw in a bunch of echos and print_r's and that was about it. It appears that this technique is not working for me in python. I am getting practice by hacking around in a python photo upload module, and when a photo is uploaded, it creates 3 different size photos. I found the code that does this, but I want to see the state at that particular moment. I tried doing a "print" on the size variable, but it did not show up in my browser.
I guess a more straightforward question would be, is it "pythonic" do debug using the browser ( equivalent to echo's and print_r's in php ), or is this what the python console is for? Thanks!
Use the logging module rather than printing stuff to stdout.
Using the interpreter in interactive mode is a great way to try out code, and pdb is very useful for real debugging.
It's "pythonic" to do debugging using the pdb module.
But really, if you're just "hacking around", then I suggest messing around with an interactive interpreter interface, especially one that supports autocompletion (Python itself comes with IDLE right out of the box).
You need to learn how to use a debugger ;) Hacking with prints is cool for simple php, but you can save a lot of time in higher languages with a debugger.
As mentioned, PDP is a place to start http://docs.python.org/library/pdb.html
As others have mentioned PDB, I'll take the opportunity to sing the praises of Eclipse using the pydev plugin, which is absolutely fantastic. IDLE is also well worth a go. Both of these IDEs allow you to step through code, inspect variables, auto-complete, etc. etc.
http://pydev.org/
Download Eclipse then use the Software Updates menu to add in PyDev.
http://pydev.org/updates
PyCharm from JetBrains is as good as all their other products. It has an integrated debugger and lots more.

Python Debugging: code editing on the fly

I am new to python and haven't been able to find out whether this is possible or not.
I am using the PyDev plugin under Eclipse, and basically all I want to find out is, is it possible to edit code whilst you're sitting at a breakpoint? I.e. Edit code whilst you're debugging.
It allows me to do this at present, but it seems to still be executing the line of code that previously existed before I made changes.
Also, are you able to drag program execution back like you can in VBA and C# for example?
If either of these are possible, how can I enable them?
PyDev supports this to some extend since version 1.4.8, see the change notes and the corresponding blog entry.
When you start a Python program, it will be compiled into bytecode (and possibly saved as .pyc file). That means you can change the source but since you don't "open" the source again, the change won't be picked up.
There are systems like TurboGears (a web framework) which detect these changes and restart themselves but that's probably going to confuse the debugger.
Going back in time also isn't possible currently since the bytecode interpreter would need support for this.
You can run arbitrary commands in the console during the breakpoint. For my needs, this typically achieves the same purpose as coding live, although I do wish it were as elegant as simply using the editor.

Categories

Resources