import subprocess in python

K

Kuhl

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.Traceback (most recent call last):

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.Traceback (most recent call last):

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.
 
C

Chris Rebert

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.Traceback (most recent call last):

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.Traceback (most recent call last):
 File "<stdin>", line 1, in ?
NameError: name 'pyfunc' is not defined
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
 
N

Nobody

Your Python version is too old.

Your Python version is *way* too old.

If you're lucky, people who use features of Python 2.5 or 2.6 will mention
that you need Python 2.5 or 2.6, but having at least Python 2.4 is
generally taken for granted.
 

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,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top