简体   繁体   English

有什么办法查看python从哪里导入模块吗?

[英]Any way to see where python is importing a module from?

So I installed the facebook module, realized it was the wrong one, used pip to uninstall and then installed facebook-sdk. 因此,我安装了facebook模块,意识到它是错误的模块,使用pip进行卸载,然后安装了facebook-sdk。 Here is my code: 这是我的代码:

import facebook

token = '[token]'

graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")

friend_list = [friend['name'] for friend in friends['data']]

print friend_list

and get 并得到

Traceback (most recent call last):
  File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
    import facebook
  File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
    graph = facebook.GraphAPI(token)
AttributeError: 'module' object has no attribute 'GraphAPI'

When looking this up, EVERY result says to uninstall facebook and facebook-sdk and reinstall facebook-sdk. 查找时,每个结果都提示卸载facebook和facebook-sdk,然后重新安装facebook-sdk。 And I have, many many times. 而且我有很多次。 I searched /python27/ for facebook afterwards to make sure the files were gone. 之后,我在/ python27 /中搜索了facebook,以确保文件不存在。

Is there any way on a windows machine to trace back where I am importing "facebook" from? Windows计算机上是否有任何方法可以追溯到我从中导入“ facebook”的位置?

Module objects have a __file__ attribute, and the object representation also includes the file: 模块对象具有__file__属性,并且对象表示形式还包括文件:

print facebook
print facebook.__file__

In your case, you are importing your own script ; 就您而言,您正在导入自己的脚本 you named it facebook as well and are masking the installed module: 您也将其命名为facebook并掩盖了已安装的模块:

  File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
    import facebook
  File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
    graph = facebook.GraphAPI(token)

Note the filename in the first line, then the fact that the same file is used for that import. 请注意第一行中的文件名,然后注意该导入使用相同文件的事实。 Python stores the main script as __main__ , so importing the script itself results in another module being created for the actual filename. Python将主脚本存储为__main__ ,因此导入脚本本身会导致为实际文件名创建另一个模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM