I'm having trouble using the manage.py shell inside Emacs. I get something like this:
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- 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.
[J[?7h[?12l[?25h[?2004l
[?12l[?25h
And every time I press enter I get these escape character in the beginning of the line. The shell actually still works, but it is very unpleasant.
I think it is an encoding error of some kind, but I have no idea how to fix it.
I'm using Emacs 24.3.1, and use the django-python package to run the shell inside Emacs.
I'm kind of a newbie to all this, sorry if the question is not properly put.
Thanks
This was answered on the Emacs stackexchange website: Weird shell output when using IPython 5
The short story is that IPython switched to using prompt_toolkit which is incompatible with the Emacs shell. Add the --simple-prompt argument to switch to an emacs-compatible prompt. This solves the problem for me in Emacs 24 and IPython 6.
You could add this to your .emacs file:
(setq python-shell-interpreter-args "--simple-prompt")
Or in Emacs, do:
Meta+x customize-group RET python RET
and then add --simple-prompt to "Python Shell Interpreter Args".
I experimented with the --color argument but it doesn't seem to affect the colors displayed by emacs.
It is probably not encoding error but codes which IPython uses to set color text.
Normally you could do
ipython --colors=noColor
to run IPython without colors.
But I don't know how to do it in this extension.
Related
ORIGINAL QUESTION
Printing unicode character \u0332 to command prompt with python results in an underscore after the previous letter, not underneath.
I ran chcp 65001 based on some suggestions I found on the web, it displays properly neither before nor after running that command.
Example:
C:\>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print(u"this is a te\u0332st.")
this is a te_st.
Expected output:
this is a te̲st.
(note: copy pasting the output from console to here displays the character correctly, as seen in expected output)
EDIT 1:
It seems that no unicode combine characters are displaying combined in the command prompt.
NEW QUESTION BASED ON COMMENTS BY #ErykSun
Is there a simple way to either redirect python print and input calls to ConEmu (or similar program), or write custom functions that replace them?
EDIT 1:
Based on the suggestion from #lenz, is there a way to run ConEmu and pass it a command to run from a command?
Thanks to #ErykSun's comments, I've set my default terminal to ConEmu which displays unicode combined character correctly.
To future users looking for an answer to this question, assuming you're using ConEmu you can set it to the default console by doing this:
right click the top bar and go to Settings... (or use the keyboard shortcut Win+Alt+P)
go to Intergration -> Default term in the left menu
Check the following boxes:
Force ConEmu as default terminal for console applications, Register on OS startup, Leave in TSA, and Aggressive mode
Then hit Save settings
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.
I don't know about the meaning of calculator mode in Python, and I've put the portion of documentation below.
If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
(Emphasis mine)
Here's the original document.
Interactive mode and Calculator mode are the same thing. This is a mode that comes with Python. If you have installed Python then you have also installed something called the Python Shell.
There are two ways you can access the Python Shell:
Typing python or python[version-number] in your command
prompt/terminal window:
Assuming that you have python in your PATH variable, you can access
the Python Shell by simply typing python or python[version-number]
in your command prompt/terminal window.
Running the Python Shell in the Python IDLE(Integrated Development Environment) GUI:
To run the Python Shell in the Python IDLE GUI, you can type(again i'm assuming that the path to your python installation folder, is in your PATH variable), just type idle into your command prompt\terminal window and this should start the Python Shell in the Python IDLE GUI.
Of course the exact text for the Python Shell heading will vary between OS's, but all of them look very similar. Here is an example of what the heading appears like on my Mac:
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
As you can tell from the text above, a newline in the Python Shell is denoted by three caret symbols: >>>. For each newline three new carets are printed. Using Python Shell is different from typing a script because the script is predefined and the shell is written line-by-line.
Here is an example to illustrate my point further:
>>> xyz = 100
>>> for i in range(1, 10):
... xyz += i
... print(xyz)
...
101
103
106
110
115
121
128
136
145
As you can tell from the above program, indention is noted by three dots: ..., and the only time the Python Shell shows only one line at a time unless it is 'echoing' back what you typed in.
Why is it called interactive?
One of the main reason it's called interactive is that to display variable values or run the module in general you don't have to explicitly invoke the Python interpreter. Take the example below:
>>> name = "some name"
>>> print(name)
some name
>>> name
'some name'
As displayed above, you can access the values of a variable without needing to call print on the variable. This can be very useful when debugging or trying to understand your code.
The Python Shell is not really a practical way to write long/complex programs. A better choice would be to use the Python IDLE built-in script editor or another text-editor or IDE.
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)
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.