views:

139

answers:

4

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.

I need to know if the python is run from command line or from TextMate/emacs. How can I do that?

ADDED

I use TextMate for python coding/debugging, and it's pretty useful. 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. 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.

I got an answer in the case with emacs, and I happen to solve this issue with TextMate.

  1. Set variables in Preferences -> Advanced -> Shell Variables, and I found that TM_ORGANIZATION_NAME is already there to be used. So, I'll just use this variable.
  2. Use this variable, if os.environ['TM_ORGANIZATION_NAME']: return True

I guess the shell variable from TextMate disappear when I'm done using it.

+1  A: 

sys.argv will tell you how Python was invoked. I don't know about TextMate, but when I tell Emacs to eval buffer, its value is ['-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 []. If you run a python script, it will have the script name and whatever arguments you pass it. 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.

That's pretty hackish though. Maybe there's a better way.

Nathon
+3  A: 

For Emacs: If python is run as an inferior process, then the environment variable INSIDE_EMACS will be set.

From docs:

Emacs sets the environment variable INSIDE_EMACS in the subshell to a comma-separated list including the Emacs version. Programs can check this variable to determine whether they are running inside an Emacs subshell.

Trey Jackson
A: 

Use Command-R to run the script directly

Use Shift-Command-R to run the script from terminal.

pyfunc
+1  A: 

From the docs for 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. If the script directory is not available (e.g. 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. Notice that the script directory is inserted before the entries inserted as a result of 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):

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',
  ... ]
unutbu