import subprocess in python

Discussion in 'Python' started by Kuhl, Nov 16, 2009.

  1. Kuhl

    Kuhl Guest

    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.
    Kuhl, Nov 16, 2009
    #1
    1. Advertising

  2. Kuhl

    sturlamolden Guest

    On 16 Nov, 13:50, Kuhl <> wrote:

    > Python 2.2.3 (#1, Feb  2 2005, 12:22:48)


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


    Your Python version is too old.
    sturlamolden, Nov 16, 2009
    #2
    1. Advertising

  3. Kuhl

    Chris Rebert Guest

    On Mon, Nov 16, 2009 at 4:50 AM, Kuhl <> 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
    Chris Rebert, Nov 16, 2009
    #3
  4. Kuhl

    Nobody Guest

    On Mon, 16 Nov 2009 04:58:00 -0800, sturlamolden wrote:

    > On 16 Nov, 13:50, Kuhl <> wrote:
    >
    >> Python 2.2.3 (#1, Feb  2 2005, 12:22:48)

    >
    >> 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.
    Nobody, Nov 16, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. JPractitioner
    Replies:
    13
    Views:
    20,118
    Roedy Green
    Feb 24, 2006
  3. Vitali Gontsharuk
    Replies:
    2
    Views:
    554
    Vitali Gontsharuk
    Aug 25, 2005
  4. Artur M. Piwko

    import/from import question

    Artur M. Piwko, Jun 29, 2003, in forum: Python
    Replies:
    1
    Views:
    824
    Peter Hansen
    Jul 2, 2003
  5. hiral
    Replies:
    2
    Views:
    582
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page