I previously thought that was the issue with IPython, but today I tested again, here is what I did:
Run emacs -Q in cmd window
Open a .py file
M-x, then run python-shell-switch-to-shell, RET, and RET. Then I have the Python shell ready
I in put the following code then:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.ion()
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x03068610>]
>>>
Actually after this, no figure shows up, and the shell is frozen, e.g., when I input:
>>> print("hello")
nothing happened...I haven't tested other plotting tools but matplotlib. I don't know if it is a bug. I've searched for a while, here and though Google, but no luck. My system is: Emacs 24.3 32 bit for Windows, under Windows 7. If others can duplicate same issue as here, I will report this as a bug.
I used IPython as the Python shell by:
C:/Python27/python.exe -i C:/Python27/Scripts/ipython-script.py --pylab
Then, I input figure(); plot([1,2,3]), as expected, the figure popup and freezes. Then I did: C-c C-d which runs comint-send-eof, and the figure actually get updated! But my IPython shell session is also terminated with the following message:
In [6]:
Do you really want to exit ([y]/n)?
Traceback (most recent call last):
File "C:/Python27/Scripts/ipython-script.py", line 9, in <module>
load_entry_point('ipython==0.13.1', 'console_scripts', 'ipython')()
SystemExit
If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev#scipy.org
You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True
Any helpful clue here?!
one solution is:
(setq python-shell-interpreter "C:\\YourPython3Dist\\python.exe"
python-shell-interpreter-args "-i C:\\YourPython3Dist\\Scripts\\ipython3-script.py console --pylab=qt")
The Argument console in the call of ipython-script.py is the important one!
In Python 3 with qt backend it works for me. I don't know how it works with py 2.7. (should be no problem if these arguments are supported for ipytho-script.py)
I think it would take sometime until the problem is fixed. Until some Windows user actually debugs python.el.
Until then, why not try Emacs IPython Notebook? It is a better IPython binding for Emacs. You don't need to use the notebook part. You can think it as a replacement for python shell in python.el. (disclaimer: I am the author)
Related
I sometimes use jupyter console to try out things in python.
I'm running arch linux and installed everything through the arch repos.
I hadn't ran jupyter console in quite some time, but while trying to launch it, i can't get it to work anymore.
Here is the error :
Jupyter console 6.5.1
Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.10.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
Task exception was never retrieved
future: <Task finished name='Task-7' coro=<ZMQTerminalInteractiveShell.handle_external_iopub() done, defined at /usr/lib/python3.10/site-packages/jupyter_console/ptshell.py:839> exception=TypeError("object int can't be used in 'await' expression")>
Traceback (most recent call last):
File "/usr/lib/python3.10/site-packages/jupyter_console/ptshell.py", line 842, in handle_external_iopub
poll_result = await self.client.iopub_channel.socket.poll(500)
TypeError: object int can't be used in 'await' expression
Shutting down kernel
I tried reinstalling everything through pacman in case I accidentally changed something I shouldn't, but it changed nothing.
Any tips on what could be wrong ?
I don't have enough rep to comment but I do not have the same issue. I can launch Jupyter QT Console just fine, and I have the same python version and IPython version. Just thought I would share, even though I don't use Jupyter Console. I do all my .ipynb in vscode and all other coding in neovim. I don't know if there is a difference between the console you are talking about and QT console, but Jupyter QT Console works fine for me, just unbearably light theme :).
I have a Python script, and I want to execute it up to a certain point, then stop, and keep the interpreter open, so I can see the variables it defines, etc.
I know I could generate an exception, or I could invoke the debugger by running pdb.set_trace(), then stop the debugger, which is what I currently use.
...but is there a command that will just stop the script, as if it had simply reached its end? This would be equivalent to commenting the entire rest of the script (but I would not like doing that), or putting an early return statement in a function.
It seems as if something like this has to exist but I have not found it so far.
Edit: Some more details of my usecase
I'm normally using the regular Python consoles in Spyder. IPython seems like a good thing but ( at least for the version I'm currently on, 2.2.5) some of the normal console's features don't work well in IPython (introspection, auto-completion).
More often than not, my code generates matplotlib figures. In debug mode, those cannot be updated (to my knowledge), which is why I need to get completely out of the script, but not the interpreter).
Another limit of the debugger is that I can't execute loops in it: you can copy/paste the code for a loop into the regular console and have it execute, but that won't work in the debugger (at least in my Spyder version).
If you invoke your program with python -i <script>, the interpreter will remain active after the script ends. raise SystemExit would be the easiest way to force it to end at an arbitrary point.
If you have ipython (highly, highly recommended), you can go to any point in your program and add the following lines
import IPython
IPython.embed()
Once your program reaches that point, the embed command will open up a new IPython shell within that context.
I really like to do that for things where I don't want to go the full pdb route.
If you are using the Python Shell, just press CTRL + C to throw a KeyboardInterrupt. You can then check out the state of the program at the time the exception was throw.
x = 0
while True:
x += 1
Running the script...
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
Traceback (most recent call last):
File "C:/Python27/test.py", line 2, in
while True:
KeyboardInterrupt
>>> x
15822387
I have a data processing pipeline setup that I want to debug.
The pipeline consists of a bash script that calls a python script.
I usually use iPython's embed() function for debugging. However, when calling the python script from the bash file, the embed() function is called but immediately exited, without me being able to interfere. When running the same python program directly from the command line I don't observe this kind of behavior. Is this intended behavior or am I doing something wrong?
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Do you really want to exit ([y]/n)?
'follow up code prints here'
I can replicate the problem like this:
# test.py
import IPython
import sys
print(sys.stdin.read())
IPython.embed()
# session
❯ echo 'foo' | python test.py
foo
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: Do you really want to exit ([y]/n)?
❯ # I didn't quit on purpose, it happened automatically
STDIN is not a TTY, so I'm thinking that IPython is worried that the inbound text (via the pipe) won't be a user typing. It doesn't want foo (from my example above) to spew into the IPython shell and do something unexpected.
You can work around this by getting your terminal id via the tty command, and redirecting stdin to the calling terminal after it has finished reading from the pipe, something like this:
with open('/dev/pts/16') as user_tty:
sys.stdin=user_tty
IPython.embed()
For more on ttys, see this post. Note also that if you put the wrong tty in there, input from some other terminal will control IPython.
I'm not sure if it's possible for IPython to know what the calling tty would have been, had it not been overwritten by bash to be the output-side of the pipe.
Edit: Here's my workaround put more simply: How do I debug a script that uses stdin with ipython?
I ran some experiments to see the behaviour. I noticed that IPython shows the console if any of the ancestor process is terminal.
Following are the files in /tmp directory:
x.py
import IPython
IPython.embed()
call.sh
/usr/bin/python /tmp/x.py
call2.sh
/tmp/call.sh
Experiment 1
Running python x.py does open the IPython shell and waits.
Experiment 2
Running bash call.sh also opens the IPython shell and waits.
Experiment 3
Running bash call2.sh also opens the IPython shell and waits.
As you can see, it does not matter how deep is your IPython.embed call is. It always starts the interactive console and waits.
Lets try if it also works when we fork a new process.
fork.sh
/usr/bin/python /tmp/x.py &
Experiment 4
In this case, IPython shell started but immediately exited. Notice the & at the end. It starts a different process. IPython was not able to access the terminal in this case and hence exited gracefully.
Steps to repeat:
gfixler#gigabox:/autodesk/maya2012-x64/bin$ ./mayapy
Python 2.6.4 (r264:75706, Nov 3 2009, 14:09:42)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import maya.standalone
>>> maya.standalone.initialize() # this hangs until I ^C
^CResult: untitled
Fatal Error. Attempting to save in /usr/tmp/gfixler.20120908.1953.ma
gfixler#gigabox:/autodesk/maya2012-x64/bin$
I think it's a library path issue of some sort, but I don't know how to find out.
I figured out the issue.
In trying to solve this I learned about python -m trace --trace script.py, and also a bit about pdb, a Python debugger. I tied these together by calling a trace on a file containing this:
pdb.run(maya.standalone.initialize(), globals(), locals())
I don't know if that was using either incorrectly, or overkill (trace alone was hanging after printing out a tremendous amount of information, which redirected into a file yielded nothing useful), but after hitting n (next) and s (step) followed by hundreds of enter keypresses in pdb got me nowhere, on a whim I typed help and got a help menu. I decided to try the listed EOF command, and it ran until it crashed with a message about being unable to load the commandPort. I remembered I set that value to autoload (Preferences window, Applications section) last week while fighting with nose, and apparently that was causing it to hang on a bad entry (":12345"), with absolutely no messages about anything. I opened UI Maya, deleted that preference, and now mayapy initializes fine. Phwew.
I'm a newbie programmer so I'll do my best to clearly ask my question. I'm running Python scripts in Mac 10.6.5 and now trying to write and save to a text file (following instructions in HeadsUp Python book). Whenever I hit function+F5 (as instructed) I get the same "invalid syntax" error and Idle highlights the "1" in "Python 3.1.3" of the header. Here's the header to which I'm referring:
Python 3.1.3 (r313:86882M, Nov 30 2010, 09:55:56) [GCC 4.0.1 (Apple Inc. build 5494)] on darwin Type "copyright", "credits" or "license()" for more information.
Extremely frustrating. I've checked and rechecked the code but this doesn't seem to be code related because the "syntax error" is in regards to the header text that posts in every Idle/Python session. Help anyone?
... and Idle highlights the "1" in "Python 3.1.3" of the header ...
Standalone Python scripts used to contain a "header", but that would be just
#!/usr/bin/env python
or, depending on the name of the interpreter maybe
#!/usr/bin/env python3.1
Not sure I understand your question, though.
you are writing your script in the wrong IDLE window ! when starting IDLE, it opens 2 windows: one for writing a script and another one with an interactive python shell. executing the content of the interactive python shell makes no sense.
#squashua: I have the same issue when I try to run the code either in IDLE or Ubuntu terminal.
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25)
it highlights "5" as syntax error.