import precedence

Chris Liechti cliechti at gmx.net
Tue Jun 25 14:43:29 EDT 2002


"Mark McEahern" <mark at mceahern.com> wrote in
news:mailman.1025011774.28523.python-list at python.org: 

> Suppose I have a package like this:
> 
>   spam/
>     __init__.py
>     setup.py
>     foo.py
> 
> For cygwin, setup.py creates:
> 
>   foo.dll
> 
> For linux2, setup.py creates:
> 
>   foo.so
> 
> But for win32, setup.py is basically a no-op because I can do what I
> need to do in foo.py using pythoncom.
> 
> Here's the question:
> 
> Can I just assume that:
> 
>   from spam import foo
> 
> will only import the foo.py module if the foo.dll and foo.so are not
> found--that is, if the import statement is executed on win32 (where
> those extensions won't exist)?
> 
> I looked at the documentation for import and it's not clear to me that
> it addresses this issue.  Perhaps because noone in their right mind
> would consider this approach?  ;-)

why don't you add an underline to the C extension, it's pretty common to do 
that. then in foo.py you can do:

try:
    	from _foo import *
except ImportError:
    	#get python implementation...

you could also choose the correct import by looking at os.name.
example from pyserial:

if os.name == 'nt': #sys.platform == 'win32':
    from serialwin32 import *
elif os.name == 'posix':
    from serialposix import *
elif os.name == 'java':
    from serialjava import *
else:
    raise "Sorry no implementation for your platform available."

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list