Dropbox API 不在 python 脚本 运行 上工作,但在交互模式下工作

Dropbox API not working on python script run but is working on interactive mode

一个简单的检查 Dropbox API 是否工作,我创建了下面的 dropbox.py 脚本

import dropbox

dbx = dropbox.Dropbox('MY_TOKEN')

dbx.users_get_current_account()

运行 它在使用终端的正常脚本模式下,我必须使用下面的命令。

username$ python3 dropbox.py

此 returns 以下错误:

Traceback (most recent call last):
  File "dropbox.py", line 1, in <module>
    import dropbox

使用以下命令的交互模式时工作正常

username$ python3
Python 3.6.4 (default, Jan  6 2018, 11:51:15) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dropbox
>>> dbx = dropbox.Dropbox('MY_TOKEN')
>>> dbx.users_get_current_account()
FullAccount displayed here successfully

为什么交互模式与脚本模式存在差异?如何让脚本模式工作?

问题可能出在文件 dropbox.py 和模块之间的名称离合器上。

当 运行 dropbox.Dropbox、python 尝试从您的文件(也被视为模块)创建 Dropbox class 的实例时,并且你没有。

这种导入顺序背后的动机是能够 "override" 使用您自己的预安装模块。

TL;DR:重命名您的文件应该会有帮助。