简体   繁体   English

Dropbox API无法在python脚本上运行,但正在以交互方式运行

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

A simple check if the Dropbox API works, I have below dropbox.py script created 简单检查一下Dropbox API是否有效,我在下面创建了dropbox.py脚本

import dropbox

dbx = dropbox.Dropbox('MY_TOKEN')

dbx.users_get_current_account()

Running it in normal script mode using terminal, I have to use below command. 使用终端在普通脚本模式下运行它,我必须使用以下命令。

username$ python3 dropbox.py

This returns below error: 返回以下错误:

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

It works okay when using the interactive mode with below command 通过以下命令使用交互模式时可以正常工作

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

Why is there a difference in interactive versus script mode? 交互模式和脚本模式为什么会有区别? How to get the script mode working? 如何使脚本模式工作?

Probably, the issue is name clutch between your file dropbox.py and module. 问题可能出在文件dropbox.py和模块之间。

When running dropbox.Dropbox , python tries to create instance of Dropbox class from your file (which is treated as module too), and you have no such. 当运行dropbox.Dropbox ,python会尝试从文件(也将其视为模块)创建Dropbox类的实例,而您还没有这样的实例。

The motivation behind this order of imports is ability to "override" pre-installed modules with your own. 导入顺序背后的动机是能够“覆盖”您自己的预装模块。

TL;DR: renaming your file should help. TL; DR:重命名文件应该会有所帮助。

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

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