Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > import subprocess in python

Reply
Thread Tools

import subprocess in python

 
 
Kuhl
Guest
Posts: n/a
 
      11-16-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:4
[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:4
[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.
 
Reply With Quote
 
 
 
 
sturlamolden
Guest
Posts: n/a
 
      11-16-2009
On 16 Nov, 13:50, Kuhl <(E-Mail Removed)> wrote:

> Python 2.2.3 (#1, Feb *2 2005, 12:22:4


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


Your Python version is too old.

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      11-16-2009
On Mon, Nov 16, 2009 at 4:50 AM, Kuhl <(E-Mail Removed)> 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:4
> [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:4
> [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
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      11-16-2009
On Mon, 16 Nov 2009 04:58:00 -0800, sturlamolden wrote:

> On 16 Nov, 13:50, Kuhl <(E-Mail Removed)> wrote:
>
>> Python 2.2.3 (#1, Feb *2 2005, 12:22:4

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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
to use import java.lang.* or import java.lang.Math or none at all? JPractitioner Java 13 02-24-2006 08:48 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
XML Schema question - does "import" import elements? Vitali Gontsharuk XML 2 08-25-2005 07:33 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments