import subprocess in python

Chris Rebert clp2 at rebertia.com
Mon Nov 16 08:04:02 EST 2009


On Mon, Nov 16, 2009 at 4:50 AM, Kuhl <chen_zhitao at yahoo.com> wrote:
<snip>
> found in my system, so I have to use python instead. However, "import
> subprocess" still failed, see below.
>
> # which python
> /usr/bin/python
> # which ipython
> ipython: Command not found.
> # python
> Python 2.2.3 (#1, Feb  2 2005, 12:22:48)
> [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import subprocess
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> ImportError: No module named subprocess
>>>>

The `subprocess` modules was added in Python 2.4. You're using Python
2.2.3; you need to update.

> should start to search for  it. Then I tried to key in a function
> file  python_func_00.py  by myself.
>
> def pyfunc():
>        print "Hello function"
>
>
> # python
> Python 2.2.3 (#1, Feb  2 2005, 12:22:48)
> [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import python_func_00
>>>> pyfunc()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> NameError: name 'pyfunc' is not defined
<snip>
> What's the mistake that I am making? How to solve it?

The `import foo` statement only adds the name of the module itself to
your namespace, so you need to refer to pyfunc by way of the module
name. To wit:

import python_func_00
python_func_00.pyfunc()

Alternatively, you can specify a set of names to import from the
module namespace into your own using the `from foo import bar` syntax:

from python_func_00 import pyfunc
pyfunc()


Regarding ipython, it's a third-party package that is not part of
Python itself and must be installed separately. Exactly how you do so
will obviously depend on which distro you're using.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list