无法导入名称SharePointSite

时间:2014-11-28 17:45:53

标签: python python-2.7

我在桌面和Windows 2008 R2服务器上都收到了相同的错误消息 -

这是代码 -

from sharepoint import SharePointSite, basic_auth_opener

server_url = "http://sharepoint/"
site_url = server_url + "path/to/page/Forms/AllItems.aspx"

opener = basic_auth_opener(server_url, "acct", "password")

site = SharePointSite(site_url, opener)

for sp_list in site.lists:
    print sp_list.id, sp_list.meta['Title']

运行时我收到以下错误 -

Traceback (most recent call last):
  File "C:\temp\sharepoint.py", line 1, in <module>
    from sharepoint import SharePointSite, basic_auth_opener
  File "C:\temp\sharepoint.py", line 1, in <module>
    from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite

发生了什么?包裹位于 -

C:\Python27\Lib\site-packages\sharepoint

我可以很好地导入其他包。例如,lxml工作正常。

from lxml import etree
没问题。

1 个答案:

答案 0 :(得分:5)

您将脚本命名为sharepoint.py并屏蔽了该库:

Traceback (most recent call last):
  File "C:\temp\sharepoint.py", line 1, in <module>
    from sharepoint import SharePointSite, basic_auth_opener
  File "C:\temp\sharepoint.py", line 1, in <module>
    from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite

查看traceback中的文件名,您可以看到脚本最终导入本身;当Python启动您的脚本时,它会将其加载为__main__,因此导入sharepoint会再次加载您自己的文件,此时它无法再次导入。

将脚本重命名为其他内容。