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

H

hiral

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
 
S

Steven D'Aprano

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

Jean-Michel Pichavant

hiral said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top