Rus Python script interactively

G

Gnarlodious

In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

-- Gnarlie
http://Gnarlodious.com/
 
D

Diez B. Roggisch

Gnarlodious said:
In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

If it's in the path, you can just import it. That will of course only be
possible once per ipython-session.

Diez
 
L

Lie Ryan

Gnarlodious said:
In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

-- Gnarlie
http://Gnarlodious.com/

IMHO, starting a new terminal and runs the script from there.
Or quitting/pausing the current interactive session and run it like
normal python scripts.

Alternatively, you may also want to look at `execfile` built-in function.
 
P

Peter Otten

Gnarlodious said:
In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

After a quick glance over ipython's %quickref:

In [1]: !mkdir -p path/to

In [2]: !echo 'print "works!"' > path/to/scriptname.py

In [3]: %run path/to/scriptname.py
works!
 
G

Gnarlodious

Thanks for all the suggestions! The last solution is the one I was
ooking for, I am really starting to like iPython. Learning all kinds
of new tricks!.

-- Gnarlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top