[Tutor] updating Unix config file

Dave Angel davea at ieee.org
Wed Oct 21 01:16:49 CEST 2009



Matt Herzog wrote:
> On Tue, Oct 20, 2009 at 02:49:53PM +0000, Tiago Saboga wrote:
>   
>> On Tue, Oct 20, 2009 at 2:44 PM, Matt Herzog <msh at blisses.org> wrote:
>>     
>>> Yes, thanks. What failed was the invocation of PIPE. Apparently I had to explicitly import PIPE from subprocess or python had no clue as to what PIPE was!
>>>
>>> Dare I say, "wtf?" since when fo I have to specify such? Shouldn't importing the subprocess module make all its methods available? I can't remember having to do this before.
>>>       
>> It is really like any other module. If you just import subprocess, you
>> can access all its methods and attributes by spelling it out:
>>
>> import subprocess
>> handler = subprocess.Popen(['cat'], stdout=subprocess.PIPE,
>> stdin=subprocess.PIPE)
>>
>> Or you can import just the names you will use in the global namespace:
>>
>> from subprocess import Popen, PIPE
>> handler = Popen(['cat'], stdout=PIPE, stdin=PIPE)
>>
>> HTH,
>>     
>
> It does help. I never knew this. I don't remember seeing this quirk before.
>
> Thanks.
>
>   

I don't know how much you've used Python, but you were already using 
os.popen().  It's the same thing.  In order to reference a symbol 
defined in another module, you either have to qualify that symbol with 
the module name (like the "os." prefix), or use the fancier from xx  
import  yy statement.

How about if I elaborate a bit:

When you do
import   mymodule

you've added exactly one symbol to your "global" namespace, the 
symbol    mymodule.

If you do
from  mymodule import symbol1, symbol2, symbol3

you do the equivalent of three assignment statements, adding three 
symbols to your global namespace.

symbol1 = mymodule.symbol1
symbol2 = mymodule.symbol2
symbol3 = mymodule.symbol3


HTH
DaveA



More information about the Tutor mailing list