so far I used the Komodo IDE for Python development, but I'm now testing Eclipse with PyDev. Everything works fine, but there is one Komodo feature that I'm missing.
In Komodo I can inspect the running application in a debugger shell. I.e. after hitting a breakpoint I can not only read the content of variables, but I can execute arbitrary Python code (e.g. changing the value of variables) and then continue program execution.
PyDev has also some interactive shell during debugging, but I can only read variables and not change their content. Is this feature not available in PyDev or am I missing something here?
Many thanks,
Axel
As you've seen, you can just use the console directly:
http://pydev.org/manual_adv_debug_console.html
Now, you can also connect the interactive console (which is a bit more advanced) by selecting a stack frame to attach it:
http://pydev.org/manual_adv_interactive_console.html
Yes you can do that. Just type in the console what ever commands you want :). I usually have to right click then
Debug As >> Python run
PyDev is a little bit quirky, but you get used to it.
Related
I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.
I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.
So I installed PyDev in Eclipse and started testing it and I have come to an issue.
While using IDLE to run Python I could, for example, create a file, set a variable x = 10 and then make IDLE run said file. I would then be able to ask python for x and it would give me 10. I don't know how to do that in PyDev.
I created a python interactive console and then when prompted chose the "Console for currently active editor" but the console will not recognize x even though the editor has x defined to 10. I did save before creating the console, I also ran the file before I opened the console... I do not know what to do...
Thank you!
Hmmm, I am not familiar with the IDE IDLE, nor do I typically run a file via the console, but maybe I understand your question. The core answer is you need a breakpoint so that execution does not terminate and therefore x=10 is resident in memory. If the breakpoint is set post x=10, then when you reach the breakpoint and execution stops and you type "x" you will get 10.
There is documentation online on the console about how to use it in context of a load of file from with the console. I tend instead to hit shift-F9 while in the file to run it in debug mode. This leaves you in debug console rather than interactive console (you'll see no prompting ">") but you'll still be able to have x=10 when you enter x at break.
Probably misunderstood but though I would give it a shot. Good luck!
After creating the console you have to do an execfile of the python in the console... (ctrl+alt+enter).
See: http://pydev.org/manual_adv_interactive_console.html for details
Note that I believe the current version has an issue with that command -- but I'll probably do a release tomorrow which has a fix for that (PyDev 3.8)
How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.
Let's consider 3 situations:
1) I write a pyhon module in Eclipse (pydev) and run it Ctrl-F11. The module runs and I don't have any control or access (AFAIK) to the module variables and functions.
2) I have defined a python interpreter as an external tool in Eclipse, so I can run it within Eclipse. It works fine, but it does not have tab completion.
3) I open my module with python IDLE (not eclipse) and press f5 (run). It runs on the IDLE opened window and when it finishes I have all the variables and functions form the module to play with.
So I have 2 questions:
a) how to enable, if possible, tab completion in python interpreter in Eclipse? If it's not possible, do I have any alternative to standard Python interpreter?
b) I would like to run step 1) and then be able to continue developing / testing with python interpreter, just like I do with IDLE, but all inside Eclipse. Any suggestion?
Thanks in advance
Not sure how much has changed since the question was asked, but it's now possible to get code completion in the debug console (as of PyDev 1.6.0 according to http://pydev.org/manual_adv_debug_console.html, I'm on PyDev 2.8.1, Eclipse 4.3.0)
Set a breakpoint in the code, on an executable statement (even just print('') or True). Double clicking on the grey vertical bar to the left of the code should do it.
Run in debug mode with F11
Eclipse may prompt to switch to the Debug Perspective; either way, the Console should be visible
You should now be able to click in the Console pane and begin interactive debugging with code completion, even after a traceback. (Ctrl+Space works for me.)
Be aware that multi-line statements won't work unless they're split on : or /:
Update in 1.6.0: commands are evaluated on each new line unless the line starts with ' ' or '/t' or ends with ':' or '/' (so, for entering multi-line statements, the input must be entered properly respecting those limitations).
If you run it as a debug operation in Eclipse, you should be able to set a breakpoint, and you can then examine variables, etc. But you can evaluate random python scripts via the watch functionality.