How would I open a second python interpreter in emacs? I am using emacs 24.3 and Ubuntu 12.04 LTS. I have opened the SQL interpreter/program via a prefix argument of 2. I tried this with python and it did not work.
Any suggestions and ideas are welcome. The mode in my current python interpreter buffer says: Inferior Python: run Shell-Compile I have downloaded python-mode 6.10 from ELPA the emacs package manager.
Thanks for all the help!
M-x describe-function (RET) run-python:
run-python is an interactive compiled Lisp function in `python.el'.
(run-python &optional CMD NOSHOW NEW)
Run an inferior Python process, input and output via buffer Python.
CMD is the Python command to run. NOSHOW non-nil means don't show the
buffer automatically.
Interactively, a prefix arg means to prompt for the initial Python
command line (default is `python-command').
A new process is started if one isn't running attached to
python-buffer', or if called from Lisp with non-nil arg NEW.
Otherwise, if a process is already running inpython-buffer', switch
to that buffer.
...
In the *scratch* buffer:
(run-python nil nil 't)
That will give you a new Inferior Python process.
You could code up a new interactive emacs command in your .emacs file, something like:
(defun my-run-python ()
(interactive)
(run-python nil nil 't))
C-u M-x python
BTW your python-mode version is outdated.
Recommend to fetch a new one doing
bzr branch lp:python-mode
or visit
https://launchpad.net/python-mode
Related
BACKGROUND
I am writing a small package to farm out Emacs Lisp computations to a Python sub-process so I don't have to reproduce complicated numerical routines. For my particular problem, I need Python as a sub-process because I need to maintain certain time-consuming computations done by Python. For small things, I can do things perfectly fine by sending computations to Python via shell commands. However, I DO need the sub-process for more intensive stuff.
SETUP
GNU Emacs 25.2.1 under Windows 10
Anaconda's Python 3.5 distribution for Windows 10
SAMPLE RUN
From any buffer, hit M-x ielm. This will bring up the IELM Emacs Lisp shell. Here is a transcript of my session a few minutes ago:
*** Welcome to IELM *** Type (describe-mode) for help.
ELISP> (setq PyProc (start-process "python" "*python*" "python"))
#<process python>
ELISP> (process-list)
(#<process python> #<process ielm>)
ELISP> (process-status PyProc)
run
ELISP> (process-type PyProc)
real
ELISP> (process-send-string PyProc "1+1")
nil
ELISP> (process-send-eof PyProc)
#<process python>
As you run the above Emacs Lisp in IELM (or whatever way you want), you can watch the python buffer. Nothing displays. However, as you clearly see, I can kill the process and query its running status.
PROBLEM
Why is the Python process unable to display ANYTHING in the python buffer? I have done this with MySQL to a remote server (using the local install of MySQL) with no problems.
I have done some investigations. I have a partial workaround. However, I still have not clue why the original attempt using start-process does not work.
I know that one can use
M-x run-python
to launch an inferior Python process. One can then use use
(process-send-string PyProc "1+1\n")
where PyProc is the symbol holding your process object. This sends the command to the inferior process and presses return. The strange thing is that the "1+1\n" does not display in the Python buffer. The returned value does display. Storing the point before and after the output, it is then easy to retrieve Python's output. Here is a partial snippet:
;; Run inferior Python process (assumes you have Python properly installed)
(setq PyProc (run-python))
;; Send a carriage return. There is some issue when the process first starts
(process-send-string PyProc "\n")
;; Store the point prior to sending a command
(setq PyPM (with-current-buffer "*Python*"
(point)
)
)
;; Send a command to Python
(process-send-string PyProc "1+1;\n")
;; Get the result of the computation
(setq Result (with-current-buffer "*Python*"
(buffer-substring-no-properties PyPM
(- (point) 5)
)
)
)
Evaluation of Result gives you
Result -> "2"
You can terminate the process using
(process-send-eof PyProc)
Still don't know why I cannot simply use start-process.
In my first answer above, I resorted to
(setq PyProc (run-python))
to run Python as an inferior process since I had not been able to launch Python using the function start-process. Per the GNU Emacs documentation, there can be lots of issues with DOS programs because of the way they handle the stdin and stdout. However, python provides a "-i" command line flag that solves the problem on windows.
All you have to do is:
(setq PyProc (start-process "python" "*Python*" "python" "-i"))
(process-send-string PyProc "1+1;\n")
If you switch to buffer Python, you will see "2" as the result of the computation. If you want to see this work nicely, simply spit your window vertically by using C-x 3. Switch to Python on the right and run your code from the left buffer.
When you are ready to kill the Python process, just
(process-send-eof PyProc)
Kill you buffer to clean up. Hope this helps those trying to control processes on the Windows platform.
I written a simple shell in python and compiled it with nuitka.
My shell as some simple commands, such as "say string", "braille string", "stop" etc.
This program uses python accessible_output package to communicate with screen reader in windows.
Ok, this works well froma a normal shell, or executing it from windows.
Now, I would like run this program from within emacs, such as normal shell in emacs.
I tried some functions, "start-process", "shell-command", but I can't write commands.
My program displays a prompt, like python interpreter, where I can put my commands.
Elisp is able to run python shells, mysql shells, but I'm unable to run my own shell.
Help!
Emacs has a number of different ways to interact with external program. From your text, I suspect you need to look at comint in the emacs manual and the elisp reference manual. Comint is the low level general shell in a buffer functionality (it is what shell mode uses).
Reading between the lines of your post, I would also suggest you have a look at emacspeak. and speechd.el, both of which are both packages which add speech to emacs. Speechd.el is bare bones and uses speech-dispatcher while emacspeak is very feature rich. The emacspeak package uses a Tcl script which communicates with hardware or software speech servers. It also has a mac version written in python which communicates with the OSX accessiblity (voiceOver) subsystem. Looking at how these packages work will likely give you good examples on how to make yours do what you want.
Take a look at how it's done in the nodejs-repl https://github.com/abicky/nodejs-repl.el/blob/develop/nodejs-repl.el (see line 308)
In python-mode.el, the part in question reads
(with-current-buffer
(apply #'make-comint-in-buffer executable py-buffer-name executable nil (split-string-and-unquote args))
See docstring of make-comint-in-buffer for details.
What about just launching your script from inside an emacs shell buffer?
M-x shell RET /path/to/my/script RET
I'm new to Emacs and I'm trying to set up my python environment. So far I've learned that using "python-mode.el" in a python buffer C-c C-c loads the contents of the current buffer into an interactive python shell, apparently using what which python yields. In my case that is python 3.3.3. But since I need to get a python 2.7 shell, I'm trying to get Emacs to spawn such a shell on C-c C-c. Unfortunatly I can't figure out, how to do this. Setting py-shell-name to what which python2.7 yields (i.e. /usr/bin/python2.7) does not work. How can get Emacs to do this, or how can I trace back what Emacs executes when I hit C-c C-c?
python-mode.el, execute a python buffer using python2:
M-x py-execute-buffer-python2
or put this in .emacs file:
(custom-set-variables
'(py-force-py-shell-name-p t)
'(py-shell-name "python2"))
python-mode.el checks py-force-py-shell-name-p variable when executing py-execute-buffer(bound to C-c C-c key), and if this variable is set to true("t"), then use python interpreter name saved in py-shell-name.
Alternatively, this customization can be done in M-x customize, Programming>Languages>Python Mode, search there for "Py Force Py Shell" and "Py Shell Name" lines.
It will add this customization code to your .emacs file.
Emacs help(describe function):
C-h f py-execute-buffer TAB
You can send selected region in a python buffer to any interpreter:
C-u 3 M-x py-execute-region
Emacs will prompt every time for a python interpreter name you want to use.
The prefix numerical argument may be any number except 1 or 4, otherwise it will use a default interpreter without prompt.
To execute a buffer in different python interpreters you can select whole buffer by C-x h and then use this prefixed command.
I don't use python, but from the source to python-mode, I think you should look into customizing the variable python-python-command - It seems to default to the first path command matching "python"; perhaps you can supply it with a custom path?
I changed two days ago to Emacs 23, which lately gave me a lot of headache, especially, as I have two Python versions installed, the older 2.7 and 3. As I generally want to start the python 3 interpreter, it would be nice if I could tell Emacs in some way to use python 3 instead of 2.7.
Besides, I could not find a module which helps to highlight python3 syntax. I am currently using python-mode.el for highlighting.
Also, if somebody had a good tip for which module would be best to show the pydoc, I would be very thankful.
Thanks in advance!
If you're using python-mode.el, you can specify the binary to be executed as an inferior process by setting the py-python-command variable, i.e.:
(setq py-python-command "python3")
Naturally, you'll need to provide the name of the binary as it exists on your system in place of "python3", if it differs. In python.el, the analogous variable to set is python-python-command.
As far as using pydoc, there are a few possibilities. First, you can simply execute help() inside the Python inferior process. If you do choose that option, you might find it useful to add the following code to your .emacs file:
(setenv "PAGER" "cat")
This is necessary because interactive pagers (e.g., less, more, most, etc.) don't work particularly well inside inferior process buffers. Second, you can install a Texinfo package containing the documentation and use Emacs's info browser (q.v., Python Programming in Emacs). Finally, if you opt to use python.el, it includes an interactive function called python-describe-symbol that can lookup pydoc help on demand (and I suspect python-mode.el should have something similar). If you search around a bit, I'm sure you can find other methods and/or third-party packages as well.
[RET] = enter or return key.
M = Meta or Alt key.
If you feel like letting Emacs do the heavy lifting for you, go through the Emacs settings dialogue to have Emacs set it automatically.
M-x customize-variable [RET] python-shell-interpreter [RET]
A new buffer should appear where you can customize the python interpreter you are using.
The field to the right of Python Shell Interpreter: will be the current interpreter your are using. In my case it was python so I changed it to python3. Then move the curer to select the Apply and Save button above and hit [RET] to make Emacs respect the new setting. Alternatively you can click the button with the mouse.
Next time you open the Emacs python interpreter, it will be using the new python you set from the setting dialogue.
After I did the following thing, I got the behavior that: when editing a Python file in emacs, C-c C-p creates a new buffer with a Python3 interpreter.
Add to your .emacs file the following:
(defcustom python-shell-interpreter "python3"
"Default Python interpreter for shell."
:type 'string
:group 'python)
The reason I tried this, was because I found
(defcustom python-shell-interpreter "python"
"Default Python interpreter for shell."
:type 'string
:group 'python)
in python.el. And I hypothesized (i) that the "python" could be substituted with "python3" and (ii) that this would override the definition in python.el.
It is likely that there are reasons why this is inelegant or heavy-handed or otherwise bad. I am an emacs newb.
start an python-interpreter
M-x python RET
(the default interpreter)
M-x pythonVERSION
where VERSION means any installed version
Inspired by #serv-inc's answer you can also put
(setq python-shell-interpreter "python3")
into your ./.emacs.d/init.el or where you store your configuration.
It's December 2020, I'm using EMACS 25, otherwise unconfigured for python, and I found that:
(setq python-shell-interpreter "python3")
added to my .emacs file did the business and led to a reasonably intuitive way of working with python3. C-c C-c sends my file to the interpreter (or asks me to start a new interpreter, and tells me how).
Of course, now the python3 interpreter runs for any python file, so python 2 programs don't work.
I am imagining a better world where emacs looks at the #!/usr/bin/env python2 line at the top of my file and figures out which interpreter to start by some kind of insane high-tech magic.
Note python-mode.el knows a hierarchy how to detect the version needed
a shebang precedes setting of py-shell-name
while py-execute-THING-PYTHONVERSION
would precede also shebang for the command being
see menu PyExec
I just downloaded GNU emacs23.4, and I already have python3.2 installed in Windows7.
I have been using Python IDLE to edit python files.
The problem is that I can edit python files with Emacs but I do not know how to run python interpreter in Emacs. When i click on "switch to interpreter", then it says "Searching for program: no such file or directory, python"
Someone says i need to make some change on .emacs file, but i do not know where to look for.
And I am very unexperienced and just started to learn programming. I am not familiar with commonly used terminologies. I have been searching for solutions but most of the articles i find on the Internet only confuse me.
so the questions are:
how do i run python interpreter in Emacs?
are there different kind of python interpreter? if so, why do they have different interpreters for one language?
Place this in your .emacs file to set the location of your python interpreter:
(setq python-shell-interpreter "path\to\your\python3.2")
Emacs comes with good manuals and an info mode to help read them. To learn more about .emacs you can use:
M-: (info "(Emacs)Init file") RET.
C-c C-z can do this. It is the key-binding for the command python-switch-to-python
In emacs 25.3.1 I use this to open up a python shell:
M-x run-python
After first adding this to my .emacs file:
(setq python-shell-interpreter "/usr/local/bin/python3")
IF you have python installed, try M-x python-shell
(press and hold ALT while pressing x, then type python-shell, then press enter)
There are different language implementations if that is what you are asking (see a list of them here).
In emacs 24.5.1 with spacemacs 105 (develop branch) and the Python layer enabled ("layer" is a spacemacs concept; see their documentation), I find python-shell-switch-to-shell opens an IPython buffer. I tested macropy.console in such a buffer and it works great.
You probably need to have Python in your windows PATH environment variable. Can you start the interpreter just by typing python in the command window?
I don't have anything special in my emacs.el, but the start interpreter command works just fine.
Also, I recommend reading this blog post, as it contains many useful tips and packages worth installing if using Emacs as a Python IDE.
To simply open an interpreter, you can also use M-x python. If that does not work, try M-x python and hit TAB, which will list more options via auto-completion. One of them should work k if you have python installed.
If you are inclined, it may be worthwhile to check out the Emacs python modes as well - http://www.emacswiki.org/emacs/?action=browse;oldid=PythonMode;id=PythonProgrammingInEmacs
In emacs 24.2 there is python-switch-to-python