Aquamacs doesn't run python file on C-c C-c - python

When I run C-c C-c on a .py file, Aquamacs returns
Output file descriptor of python3 is closed
On the other hand, the Python shell works just fine.
How can I fix it such that I can run Python files?

It seems Aquamacs' default Python interpreter is pyhton.el and not python-mode.el .
Installing python-mode.el from here will do the trick.

Related

Python IDLE how do I get from editor to shell

If I'm in the Python IDLE editor and the shell is not open, is there some way to open the shell without running a program? I expect it's something simple that I just can't find.
Thanks
For Python 3.8 its just Run -> Python Shell if I am understanding your question correctly
For windows:
Win+R to open run window
cmd to open, well, the command line
python to run python. Make sure you've added the python.exe file to PATH

M-x run-python to get a Ipython shell in emacs [duplicate]

I'm trying to run Python code for testing and debugging using Emacs. How should I debug and run code in *.py files ? I tried using the M-x compile commands . Using M-x compile, I get a compilation buffer that crashes (It says Python is compiling, but then nothing happens).
If you are using emacs24 this should be the default (in emacs23 you need python.el, not python-mode.el):
In a python buffer:
C-c C-z : open a python shell
C-c C-c : run the content of the buffer in the opened python shell
C-c C-r : run the selected region in the python shell
default python shell is "python", if you need to use ipython you can use this conf in your .emacs
(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args "--colors=Linux --profile=default"
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
python-shell-completion-setup-code
"from IPython.core.completerlib import module_completion"
python-shell-completion-module-string-code
"';'.join(module_completion('''%s'''))\n"
python-shell-completion-string-code
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
provided that you have ipython installed in your system of course :)
ipython>=5 has a auto-complete feature which breaks the emacs sub-shell, you can fix this by changing this line
python-shell-interpreter-args "--colors=Linux --profile=default"
and add --simple-prompt.
It will allow you to see ipython correctly but for some reason I did not get yet the auto-completion in emacs is not as effective as it used to be.
How do you run Python code using Emacs?
I'm running Emacs 26, vanilla dev version (self compiled from source cloned from Savannah).
(Note that in emacs docs, we usually see, for example, Ctrl-c denoted as C-c)
In Python mode (which I usually enter by using C-x C-f to "find" a (possibly new) file ending in .py), you can start a Python shell with and then execute your buffer's
if __name__ == '__main__': with:
C-c C-p (which executes run-python to create a shell with Inferior Python major mode, Shell-Compile minor mode)
C-u C-c C-c (which executes python-shell-send-buffer with a prefix argument)
We require the prefix argument to send the if __name__ == '__main__': block to the inferior Python shell.
We can see all of the Ctrl-c commands with Ctrl-c ?
C-c C-c python-shell-send-buffer
C-c C-d python-describe-at-point
C-c C-f python-eldoc-at-point
C-c C-j imenu
C-c C-l python-shell-send-file
C-c C-p run-python
C-c C-r python-shell-send-region
C-c C-s python-shell-send-string
C-c C-t Prefix Command
C-c C-v python-check
C-c C-z python-shell-switch-to-shell
C-c < python-indent-shift-left
C-c > python-indent-shift-right
C-c C-t c python-skeleton-class
C-c C-t d python-skeleton-def
C-c C-t f python-skeleton-for
C-c C-t i python-skeleton-if
C-c C-t m python-skeleton-import
C-c C-t t python-skeleton-try
C-c C-t w python-skeleton-while
Inspecting the help for python-shell-send-buffer (by clicking it), we see:
python-shell-send-buffer is an interactive compiled Lisp function in
‘python.el’.
(python-shell-send-buffer &optional SEND-MAIN MSG)
Send the entire buffer to inferior Python process.
When optional argument SEND-MAIN is non-nil, allow execution of
code inside blocks delimited by "if __name__== '__main__':".
When called interactively SEND-MAIN defaults to nil, unless it’s
called with prefix argument. When optional argument MSG is
non-nil, forces display of a user-friendly message if there’s no
process running; defaults to t when called interactively.
According to the docs C-u is a prefix argument - and seems to be the most generic one.
A workaround that lets us avoid using the prefix argument C-u is using parentheses:
if (__name__ == '__main__'):
main()
instead of the usual:
if __name__ == '__main__':
main()
and then C-c C-c by itself executes the main function.
In my opinion, M-! and M-& are underrated. Often you just want to start the current script you are working on, no need to complicate things.
Of course, you can use M-x compile, as long as you don't have to provide interactive input. If you do have to provide interactive input, M-x shell is your friend.
If you want to run stuff with one button press, check out F3 and F4 to record a keyboard macro and replay it (the macro can also be bound to a key, e.g. F5).
In each of these cases, there is no "magic" taking place. Emacs does not know how to "compile" or "run" python scripts. You have to provide/overwrite a sensible command line invocation, like:
Compile Command: ipython <yourscriptname>.py
The python subshell is cool where a REPL development style makes sense, but at least on Windows matplotlib, tkinter and other libraries that have to deal with the Windows Message Loop tend to block/hang upon displaying GUI elements.
Once you open your python file in Emacs, you will need to start the python process with:
M-x run-python or C-c C-p, which creates an inferior python shell buffer. This buffer will be created by a horizontal split, and the active buffer will be the one containing the python file.
Then you can do C-c C-c, which will send the current python buffer to the inferior python shell below. This is where you will see the output of your python file.
To switch to and from your python file buffer and the inferior python shell buffer, you can do C-x o.
If you accidentally close one of the buffers, you can switch between buffers with C-x C-<left_arrow> and C-x C-<right_arrow> and perform operations on the python buffer like the ones mentioned by Aaron Hall.
NOTE:
Running GNU Emacs 26.2 without any extensions.
Did not define if (__name__ == '__main__'): block

Is is possible to send code from text editor to existing ipython console

I am starting to learn python with sublime and ipython. They are cool tools and but I want a way to connect them.
I normally have a sublime and a IPython console open. Is there any command that I can run in sublime just send:
runfile('~\someExample.py', wdir='~\myDir')
to the running IPython console?
Thanks!
I edit in geany and use, in ipthon:
run myfile
to load and run myfile.py. ls and cd and pwd are available to check and change the directory.
I save the file from the editor, but I control the run from the ipython console.
If I just need to run the script, I could do a python myfile.py in the editor's terminal window, or maybe via the editor's execute shortcut. But the value in running the code in an existing ipython console is that I can interact with the newly loaded code and variables. I can examine variables, rerun functions with new values, etc.
There is a plugin to do this called SendCode; you can install it directly through the package manager.
As another answer mentioned, you can run some set of shell commands from within ipython, including execution of a file with run python_file.py, as well as other shell commands including cd, mkdir, rm, mv, etc.

Using python2.7 with Emacs 24.3 and python-mode.el

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?

Use of shebang in shell scripts

In Linux, we usually add a shebang in a script to invoke the respective interpreter. I tried the following example.
I wrote a shell script without a shebang and with executable permission. I was able to execute it using ./. But if I write a similar python program, without shebang, I am not able to execute it.
Why is this so? As far as my understanding, shebang is required to find the interpreter. So how does shell scripts work, but not a python script?
My assumption is that a script without a shebang is executed in the current environment, which at the command line is your default shell, e.g. /bin/bash.
shell scripts will only work if you are in the shell you targeted ... there is not python shell ... as such python will never work without explicity calling python (via shebang or command line)
By default the shell will try to execute the script. The #! notation came later
There’a subtle distinction here. If the target is a binary or begins with a #! shebang line, then the shell calls execv successfully. If the target is a text file without a shebang, then the call to execv will fail, and the shell is free to try launching it under /bin/sh or something else.
http://en.wikipedia.org/wiki/Shebang_(Unix)
Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.
Now when the #! is not found, every line is interpreted as native shell command. And hence if you write a bash script and run it under bash shell it will work. If you run the same bash script in say a tcsh shell it will not work without the initial #!/usr/bin/tcsh

Categories

Resources