Emacs: pass arguments to inferior Python shell during buffer evaluation

2024/10/7 7:54:35

recently I started using Emacs as a Python IDE, and it not quite intuitive... The problem I am struggling with right now is how to pass command line arguments to the inferior python shell when the buffer is evaluated with C-c C-c. Thanks for help.

Answer

This doesn't appear to be easily possible; the inferior process managed by the python.el module is designed to persist across many invocations of python-send-buffer (and friends). One solution I've found is to write your own function that sets sys.argv programmatically from within the inferior process:

(defun python-send-buffer-with-my-args (args)(interactive "sPython arguments: ")(let ((source-buffer (current-buffer)))(with-temp-buffer(insert "import sys; sys.argv = '''" args "'''.split()\n")(insert-buffer-substring source-buffer)(python-send-buffer))))

Execute this function in your *scratch* buffer and/or save it in your .emacs file, then, if you want, bind it to a convenient key sequence. C-c C-a doesn't seem to be used by python-mode, so perhaps:

(global-set-key "\C-c\C-a" 'python-send-buffer-with-my-args)

The command will prompt you for arguments to use, then copy your source buffer into a temporary buffer, prepending it with a code snippet that sets sys.argv to the list of arguments you supplied, and finally will call python-send-buffer.

The above code will just naively split the string you type on whitespace, so if you need to supply arguments that have whitespace in them, you'll need a more sophisticated algorithm.

https://en.xdnf.cn/q/70236.html

Related Q&A

How to edit a wheel package (.whl)?

I have a python wheel package, when extracted I find some python code, Id like to edit this code and re-generate the same .whl package again and test it to see the edits .. How do I do that?

Choosing order of bars in Bokeh bar chart

As part of trying to learn to use Bokeh I am trying to make a simple bar chart. I am passing the labels in a certain order (days of the week) and Bokeh seems to be sorting them alphabetically. How ca…

buildout - using different python version

i have set up buildout project (django to be specific) that has to run in old machine, it works fine in my local system with python 2.7. In production server it runs python 2.5 and i want to configure…

Receive an error from lingnutls/Hogweed when importing CV2

Ive never seen an error like this and dont know where to start. I installed opencv with conda install opencvand am running Ubuntu Linux 18.04 using a conda environment named fpn. How should I even appr…

Understanding django admin readonly_fields

I created some code to differentiate between two usergroups in Django admin, resulting in showing all fields readonly or only some of them, which are set directly in the ModelAdmin class.At first here …

The seaborn styles shipped by Matplotlib are deprecated since 3.6

The seaborn styles shipped by Matplotlib are deprecated since 3.6, as they no longer correspond to the styles shipped by seaborn. However, they will remain available as seaborn-v0_8-<style>. Alte…

Python: Can the pydoc module output HTML docs with relative paths?

I am using the pydoc module to output documentation for some types which I have defined with the C API. The types I want to document dont exist until the interpreter has been embedded inside my C progr…

How to generate coverage report for http based integration tests?

I am writing integration tests for a project in which I am making HTTP calls and testing whether they were successful or not.Since I am not importing any module and not calling functions directly cover…

Does Webdriver support pagefactory for Python?

I was reading about page objects and design patterns on the Webdriver project site and came across pagefactory. It doesnt look like the Webdriver for Python API includes pagefactory. Is this true?

Truncated versus floored division in Python

To establish context, Im talking about integer arithmetic only, on large integers so going via floating point isnt an option, and using negative numbers so the difference between floored and truncated …