PyDev Interactive Python Shell in Eclipse - python

I've been using Wing IDE for python programming and I am trying to switch to Eclipse, PyDev.
When I run my code in Wing IDE, after finishing the execution the console goes right back to the interactive shell and I can continue on testing, but I don't know how to do this in Eclipse.
I'm not sure if I am describing my problem properly so I'll use an example:
Let's say I had a simple source code that looked like this (e.g. test.py):
print("hello")
When I run this in Wing IDE by clicking that green arrow, the console would look like this after execution:
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate untitled-1.py]
hello
>>>>
And I can keep doing whatever on the shell and it would know my code (defined functions etc.).
But when I do the same thing in Eclipse, the console would simply look like this:
hello
and I have to click "Remove All Terminated Launches" button to go back to the shell.
Can this be done in Eclipse?

What you want to use is the interactive console in PyDev (not the regular output when you do a run).
To use it do: Ctrl+Alt+Enter.
Note that if you're in the middle of a debug session, you can also use the debug session console to interact with the program.
It can also be created from the UI in the console view as shown below:
[

From what I know, we can open multiple consoles of a particular type in Eclipse.
Whenever we run a script within PyDev, it opens a new console to which it prints the output from the script (including error output). However this is just a new console that is added to the list of already opened consoles. Hence you can switch back to a previously open console by using the Display Selected Console option within the console view ( refer here for a list of all the available console options).
What does this mean?
You can open a new Python interpretor console using the Open Console option within the Eclipse Console view. You can define your methods and play with the interpretor within that console. You now run a Python script that is open within the PyDev editor. A new console gets opened up where-in you see the output from the script (includes error output too). Now if you want to go back to the interactive console, you simply choose the Python Interepretor console that you opened previously from the Display Console option.
Personally, I like this design where-in the output from your script does not mingle and mess up with your experimental sojourns on the Python console. This in turn results in a crisp, clear and concise view of what is happening within the various python environments.
Hope this bit of information helps.

Related

Sending code snippets to the debug console interpreter

I'd like to send code I have selected from the editor to the interpreter running in the debugging console during an active debugging session. Can I do this in VSCode? If so, how?
Update 1
While Mark (accepted answer) provided what seems to be the right command (it works for me from the contextual menu with the mouse), this isn't working for me yet as a keyboard binding, and I reported this issue here.
Update 2
This started working again as of April 21, 2020 (latest Insiders version).
See https://code.visualstudio.com/docs/python/editing#_run-selectionline-in-terminal-repl
Run Selection/Line in Terminal (REPL)
The Python: Run Selection/Line in Python Terminal command
(Shift+Enter) is a simple way to take whatever code is selected, or
the code on the current line if there is no selection, and run it in
the Python Terminal. An identical Run Selection/Line in Python
Terminal command is also available on the context menu for a selection
in the editor.
VS Code automatically removes indents based on the first non-empty
line of the selection, shifting all other lines left accordingly.
Source code that runs in the terminal/REPL is cumulative until the
current instance of the terminal is closed.
The command opens the Python Terminal if necessary; you can also open
the interactive REPL environment directly using the Python: Start REPL
command. (Initial startup might take a few moments especially if the
first statement you run is an import.)
On first use of the Python: Run Selection/Line in Python Terminal
command, VS Code may send the text to the REPL before that environment
is ready, in which case the selection or line is not run. If you
encounter this behavior, try the command again when the REPL has
finished loading.
And see Use IPython REPL in VS Code for info about the IPython REPL.
--------- generic info for other languages -------------------------------------------------------------
It sounds like you want to send it the repl. There is an unbound command:
editor.debug.action.selectionToRepl
which will send selected text to the debug repl.
{
"key": "alt+y", // whatever you want here
"command": "editor.debug.action.selectionToRepl"
},
No, you cannot send a selection of code to the debugger. You need to debug the whole file or use the Interactive Window support to debug a specific cell.

how can I configure PyCharm so that running a Python script file is visible for Python Console

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.

How to Pre-load Python Modules in Python IDLE on Startup?

I am on Windows 7. When I launch Python IDLE, I want it to pre-load: pandas, numpy and matplotlib without me having to type out the import statements. I import them very frequently.
I have waded through several posts:
This one has to do with iPython, not IDLE-specific
This one has to do with running a script in IDLE
This one talks about PYTHONSTARTUP for interactive sessions
From these posts, I have determined that there is a distinct difference between Windows command-prompt python interactive shell and IDLE's interactive shell.
For example, I created defaultimports.py and put it in this location:
C:\Python34\Lib\site-packages\jaradspythonstartup
That script contains the following:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
print('pd, np and plt imported')
Next, on my machine, I went to Start > Computer > right-clicked Properties > Advanced system settings > environment variables > clicked New... and added a new variable named PYTHONSTARTUP, then put my path C:\Python34\Lib\site-packages\jaradspythonstartup\defaultimports.py
However, this seems to only work in Windows Command prompt when I open command prompt and type python. I do see it loads my print statement.
When I launch IDLE, I don't see the message: pd, np, and plt imported print statement. While in IDLE, if I import os and os.environ['PYTHONSTARTUP'], I do see my environment variable defined but don't know if that's important to note or not.
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.environ['PYTHONSTARTUP']
'C:\\Python34\\Lib\\site-packages\\jaradspythonstartup\\defaultimports.py'
>>>
My Question
How can I pre-load modules in IDLE on startup?
python -m idlelib -h on a command line, where python runs recent 3.x, will display startup commands for IDLE; use idlelib.idle for 2.x. It says that
idle idlelib -s "runs $IDLESTARTUP or $PYTHONSTARTUP before anything else".
If that does not work, try python -m idlelib -s.
Currently, when you restart the shell, which happens when you run a file from the editor, ....STARTUP does not get re-run. I hope to fix that.
EDIT: How to start IDLE with arguments from the desktop instead of command line.
Make a properly labelled IDLE desktop icon. Go to Start Menu > Python x.y > IDLE... and copy to Desktop. Control-Left Mouse Button drag or (Win 7, at least) Right click, Send to > Desktop (create shortcut). If for Python before 3.4, right click icon, select Rename, and label with the version number. (We recently got complaint "I installed 3.5 and (pre-existing) desktop icon opens 2.7.).
Make icon open IDLE with arguments. Right-click icon, select properties, click end (you may or may not have to click box first). Cursor should be at end of line with ...pythonw.exe...idlew.py. Space and add arguments. I tried -c "print('hello')" to test. Add -s for ...STARTUP. Consider renaming to indicate addition, such as IDLE 3.5 64 bit STARTUP, in case you want another IDLE desktop icon.

Python Interpreter crashing in Powershell ISE

I have python 3 installed on my system and a path to the executable has been added to the PATH. When i inter python in Windows PowerShell (win8.1) it runs fine, however i'd like to use PowerShell ISE for the advanced features it has. However running python in PowerShell ISE crashes with the following log:
python : Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
In Zeile:1 Zeichen:1
+ python
+ ~~~~~~
+ CategoryInfo : NotSpecified: (Python 3.4.3 (v...ntel)] on win32:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Type "help", "copyright", "credits" or "license" for more information.
>>>
(sorry its partly in german)
I then can't enter anything and have to Ctrl+C to get back to PowerShell.
What might be the issue here?
PowerShell ISE isn't meant for running typical interactive console programs such as python.exe. It hides the console window and redirects stdout to a pipe. To see this in practice run the following in ISE:
python.exe -i -c "import ctypes; ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 5)"
Enter text in the console window, and you'll see the input echoed in the console, but output gets piped to ISE.
Here's some a brief overview of Windows console applications. powershell.exe, cmd.exe, and python.exe are all console applications that function as clients of a console server (or host) process, conhost.exe. The host process creates the window and runs the typical GUI event loop. When you run python.exe from a GUI application, such as explorer.exe, Windows executes a new instance of conhost.exe, which creates a new console window. When you run python.exe from another console application, such as powershell.exe, the default behavior is to inherit the console of the parent.
The console API communicates with the attached console host. Many of the functions, such as WriteConsole, require a handle to a console input or screen buffer. If you're attached to a console, the special file CONIN$ represents the input buffer, CONOUT$ is the current screen buffer, and CON can refer to either depending on whether it's opened for reading or writing. (You may have seen a command in cmd.exe such as copy con somefile.txt.)
A Windows process has three fields used for standard I/O handles. For a console process StandardInput defaults to a handle for CONIN$, and StandardOutput and StandardError default to handles for CONOUT$. The C runtime library opens these as the standard FILE streams stdin, stdout, and stderr using file descriptors 0, 1, and 2. When starting a process any of the standard handles can instead be set to an open file or pipe.
While a process can attach to only one console at a time, multiple processes can be attached to a single console. However, usually only one process is active. In the case of powershell.exe, for example, after running python.exe its main thread is waiting in the background for python.exe to exit. (Note that this execution model fails badly if in python.exe you start another interactive console process and then exit, since now both the shell and the child process compete for access to the console.)

Has PyDev an interactive shell (during debugging) as in Komodo?

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.

Categories

Resources