import subprocess in python

Kuhl chen_zhitao at yahoo.com
Mon Nov 16 13:50:09 CET 2009


I am a Linux user beginning to learn Python now. Below is the first
Python script that I copied from the text book. It works, so it
confirmed that there is python installed in my system:

#!/usr/bin/env python

for a in [1, 2]:
        for b in ['a', 'b']:
                print a, b


But when I continue to learn Python, I come across with issue. The
text book instructed me to use ipython, but ipython command is not
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
>>>

So I guess that there should be a file named subprocess.py somewhere.
But there are too many files on the huge disk, I don't know where I
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
>>>
>>>

There's a binary file of 240 bytes created:    python_func_00.pyc
But as we can see above, the function pyfunc() does not work.

I guess that I should add the following statement at first line of
python_func_00.py, then redo the import:
#!/usr/bin/env python

But after adding this line and redoing the import, pyfunc() still
failed like above.

What's the mistake that I am making? How to solve it?

Thanks.



More information about the Python-list mailing list