how to import subprocess into my 'subprocess.py' file

Discussion in 'Python' started by hiral, May 5, 2010.

  1. hiral

    hiral Guest

    Hi,

    I am doing following in my 'subprocess.py' file...

    1 from __future__ import absolute_import
    2 from subprocess import *
    3 from subprocess import call as myCall
    4 from subprocess import Popen as myPopen
    5
    6 def getProperCmd(cmd):
    7 cmd += 'time' # this is just an example; in fact I am doing
    lots of processing on cmd
    8 return cmd
    9
    10
    11 def call(cmd, **kwargs):
    12 return myCall(getProperCmd(cmd), **kwargs)
    13
    14 def Popen(cmd, **kwargs):
    15 return myPopen(getProperCmd(cmd), **kwargs)

    When running this it gives following error...
    <error>
    Traceback (most recent call last):
    File "subprocess.py", line 2, in <module>
    from subprocess import *
    File "subprocess.py", line 3, in <module>
    from subprocess import call as myCall
    ImportError: cannot import name call
    </error>

    So how can I create a python file (with the same name as standard
    module name) with custom methods?

    Thank you in advance.
    -Hiral
     
    hiral, May 5, 2010
    #1
    1. Advertising

  2. On Wed, 05 May 2010 03:34:06 -0700, hiral wrote:

    > So how can I create a python file (with the same name as standard module
    > name) with custom methods?


    Don't do this. It will lead to nothing but trouble.

    Python doesn't support multiple modules with the same name. Unless you
    create a custom importer, a module called subprocess will shadow (hide)
    the original subprocess module.



    --
    Steven
     
    Steven D'Aprano, May 5, 2010
    #2
    1. Advertising

  3. hiral wrote:
    > Hi,
    >
    > I am doing following in my 'subprocess.py' file...
    >
    > 1 from __future__ import absolute_import
    > 2 from subprocess import *
    > 3 from subprocess import call as myCall
    > 4 from subprocess import Popen as myPopen
    > 5
    > 6 def getProperCmd(cmd):
    > 7 cmd += 'time' # this is just an example; in fact I am doing
    > lots of processing on cmd
    > 8 return cmd
    > 9
    > 10
    > 11 def call(cmd, **kwargs):
    > 12 return myCall(getProperCmd(cmd), **kwargs)
    > 13
    > 14 def Popen(cmd, **kwargs):
    > 15 return myPopen(getProperCmd(cmd), **kwargs)
    >
    > When running this it gives following error...
    > <error>
    > Traceback (most recent call last):
    > File "subprocess.py", line 2, in <module>
    > from subprocess import *
    > File "subprocess.py", line 3, in <module>
    > from subprocess import call as myCall
    > ImportError: cannot import name call
    > </error>
    >
    > So how can I create a python file (with the same name as standard
    > module name) with custom methods?
    >
    > Thank you in advance.
    > -Hiral
    >

    Just don't do that, find another name.
    Dont use * in import statement:
    "Namespaces are one honking great idea -- let's do more of those!"
    ("import this" in a shell for more rules)

    Using * in import statements removes namespaces.

    JM
     
    Jean-Michel Pichavant, May 5, 2010
    #3
    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:
    1,003
    Paul Opal
    Oct 11, 2004
  2. bienwell
    Replies:
    2
    Views:
    4,255
    bienwell
    Jun 17, 2006
  3. Kuhl

    import subprocess in python

    Kuhl, Nov 16, 2009, in forum: Python
    Replies:
    3
    Views:
    9,202
    Nobody
    Nov 16, 2009
  4. Brandon Harris

    Passing File Objects into Subprocess.Popen

    Brandon Harris, Nov 4, 2010, in forum: Python
    Replies:
    0
    Views:
    404
    Brandon Harris
    Nov 4, 2010
  5. Henri
    Replies:
    8
    Views:
    125
    Randy Webb
    Nov 19, 2004
Loading...

Share This Page