[英]How to know if I run python from Textmate/emacs?
I use TextMate to debug python script, as I like the feature of using 'Command-R' for running python from TextMate, and I learned that emacs provide similar feature. 我使用TextMate调试python脚本,因为我喜欢使用'Command-R'从TextMate运行python的功能,并且我了解到emacs提供了类似的功能。
I need to know if the python is run from command line or from TextMate/emacs. 我需要知道python是从命令行运行还是从TextMate / emacs运行。 How can I do that?
我怎样才能做到这一点?
I use TextMate for python coding/debugging, and it's pretty useful. 我使用TextMate进行python编码/调试,这非常有用。 But, sometimes I need to run the test using command line.
但是,有时我需要使用命令行来运行测试。 I normally turn on debugging/logging mode with TextMate, and off with command line mode.
我通常使用TextMate打开调试/记录模式,并使用命令行模式关闭。 This is the reason I asked the question.
这就是我问这个问题的原因。 Also, I plan to use emacs for python debugging, so I wanted to ask the case for emacs.
另外,我计划使用emacs进行python调试,因此我想询问emacs的情况。
I got an answer in the case with emacs, and I happen to solve this issue with TextMate. 对于emacs,我得到了答案,而我碰巧用TextMate解决了这个问题。
I guess the shell variable from TextMate disappear when I'm done using it. 我猜想当我使用完TextMate的shell变量后,它就会消失。
For Emacs: If python is run as an inferior process, then the environment variable INSIDE_EMACS
will be set. 对于Emacs:如果python作为劣等程序运行,则将设置环境变量
INSIDE_EMACS
。
From docs: 从文档:
Emacs sets the environment variable INSIDE_EMACS in the subshell to a comma-separated list including the Emacs version.
Emacs将子外壳程序中的环境变量INSIDE_EMACS设置为以逗号分隔的列表,其中包括Emacs版本。 Programs can check this variable to determine whether they are running inside an Emacs subshell.
程序可以检查此变量以确定它们是否在Emacs子外壳中运行。
sys.argv
will tell you how Python was invoked. sys.argv
将告诉您如何调用Python。 I don't know about TextMate, but when I tell Emacs to eval buffer, its value is ['-c']
. 我不了解TextMate,但是当我告诉Emacs使用eval缓冲区时,其值为
['-c']
。 That means it's executing a specified command, according to the man page. 根据手册页,这意味着它正在执行指定的命令。 If Python's run directly from the command line with no parameters,
sys.argv
will be []
. 如果Python在不带参数的情况下直接从命令行运行,则
sys.argv
将为[]
。 If you run a python script, it will have the script name and whatever arguments you pass it. 如果运行python脚本,它将具有脚本名称以及您传递的任何参数。 You might want to set up your python-mode in Emacs and whatever the equivalent in TextMate is to put something special like
-t
in the command line. 您可能要在Emacs中设置python-mode,而TextMate中的等效选项是在命令行中添加
-t
之类的特殊内容。
That's pretty hackish though. 不过,这确实有点骇人听闻。 Maybe there's a better way.
也许有更好的方法。
From the docs for sys.path
: 从
sys.path
的文档中:
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.
在程序启动时初始化后,该列表的第一项path [0]是包含用于调用Python解释器的脚本的目录。 If the script directory is not available (eg if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string , which directs Python to search modules in the current directory first.
如果脚本目录不可用(例如, 如果解释器是交互式调用的,或者从标准输入中读取了脚本),则path [0]是空字符串 ,它指示Python首先在当前目录中搜索模块。 Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.
请注意,由于PYTHONPATH的结果,在插入条目之前插入了脚本目录。
So 所以
if sys.path[0]:
# python was run interactively
else:
# python is running a script.
Or, for example, from the IPython prompt (inside Emacs): 或者,例如,从IPython提示符(在Emacs内部):
In [65]: sys.path
Out[65]:
['', <-------------------- first entry is empty string
'/usr/bin',
'/usr/local/lib/python2.6/dist-packages/scikits.statsmodels-0.2.0-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages/pyinterval-1.0b21-py2.6-linux-i686.egg',
... ]
Use Command-R to run the script directly 使用Command-R直接运行脚本
Use Shift-Command-R to run the script from terminal. 使用Shift-Command-R从终端运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.