[Pythonmac-SIG] Import ldap not behaving as expected

Bob Ippolito bob at redivi.com
Tue Oct 11 16:18:39 CEST 2005


On Oct 10, 2005, at 9:29 AM, U n d e r a c h i e v e r wrote:

> Hi all
>
> Doubtless there is a very simple solution to my problem ...
>
> I've installed python2.4 with a handful of supporting modules  
> (well, one,
> ldap) from darwinports.
>
> The following works fine provided I type it in interactively in a  
> python
> session, but not as a script:-
>
> $ cat bin/testldapimport.py
> #!/opt/local/bin/python
> import ldap
> import os
> import sys
>
> def goodbye(exitmessage, exitcode):
>     print exitmessage
>     sys.exit(exitcode)
>
> goodbye("quiting",0)
>
> $
> $
> $ /opt/local/bin/python
> Python 2.4.1 (#1, Oct 10 2005, 11:15:09)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> #!/opt/local/bin/python
>>>>
> ... import ldap
>
>>>> import os
>>>> import sys
>>>>
>>>> def goodbye(exitmessage, exitcode):
>>>>
> ...     print exitmessage
> ...     sys.exit(exitcode)
> ...
>
>>>> goodbye("quiting",0)
>>>>
> quiting
>
> But running the same coede as a script generates an error:-
>
> $
> $ /opt/local/bin/python bin/testldapimport.py
> Traceback (most recent call last):
>   File "bin/testldapimport.py", line 2, in ?
>     import ldap
>   File "/Users/glaw/bin/ldap.py", line 23, in ?
>     except ldap.LDAPError, error_message:
> AttributeError: 'module' object has no attribute 'LDAPError'
>
> Cluestick? I'm lost as to what could be going wrong here...

You have a "ldap.py" getting in the way of the real LDAP module you  
want to use.  It's because it's in the bin directory with the script,  
if you were in the bin directory when you ran it from the interpreter  
then you'd get the same error.

-bob



More information about the Pythonmac-SIG mailing list