How to import only one module in a package when the package __init__.py has already imports the modules?

Robert Kern robert.kern at gmail.com
Sun Nov 1 20:47:18 CET 2009


Peng Yu wrote:
> On Sat, Oct 31, 2009 at 11:43 PM, Steven D'Aprano
> <steve at remove-this-cybersource.com.au> wrote:
>> On Sat, 31 Oct 2009 22:54:47 -0500, Peng Yu wrote:
>>
>>> So python would not be able to accommodate my preference one
>>> class/function per file?
>> Of course it does! You can do that RIGHT NOW -- just put one class per
>> file.
>>
>>> I.e., I have to use something like 'from spam
>>> import spam' or 'spam.spam()',
>> How else do you expect to use the class if you don't import it?
>>
>>
>>> or accept that the filename is not the
>>> same as the class/function name.
>> So let me see...
>>
>> You don't want to write "import spam; spam.spam()"
>> You don't want to write "from spam import spam; spam()"
>> You don't want to write "from Spam import spam; spam()"
>> You don't want to write "from spam import Spam; Spam()"
>>
>> What exactly do you want?
> 
> When I define class spam in file spam.py, I want to call it by
> 
> import spam
> spam()
> 
> If spam.py is in dir/, then I want to call it by
> 
> import dir.spam
> 
> dir.spam()

That's just not how Python imports work.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list