运行OpenTok服务器代码(Python)时出错

时间:2017-06-19 07:31:49

标签: python opentok

我在运行flask run OpenTok服务器代码时遇到问题。我该如何根除错误?提前谢谢。

这是错误:

(opentokenv) ➜  opentok-server python -m flask run
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Library/Python/2.7/site-packages/flask/__main__.py", line 15, in <module>
    main(as_module=True)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 513, in main
    cli.main(args=args, prog_name=name)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 380, in main
    return AppGroup.main(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Library/Python/2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Library/Python/2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 423, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 152, in __init__
    self._load_unlocked()
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 176, in _load_unlocked
    self._app = rv = self.loader()
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 237, in load_app
    rv = locate_app(self.app_import_path)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 90, in locate_app
    __import__(module)
  File "/Users/azambaderi/Documents/Web_Projects/opentok-server/opentok.py", line 2, in <module>
    from opentok import OpenTok
ImportError: cannot import name OpenTok

这是Python代码:

from flask import Flask, render_template
from opentok import OpenTok
import os

try:
    api_key = os.environ['API_KEY']
    api_secret = os.environ['API_SECRET']
except Exception:
    raise Exception('You must define API_KEY and API_SECRET environment variables')

app = Flask(__name__)
opentok = OpenTok(api_key, api_secret)
session = opentok.create_session()

@app.route("/")
def hello():
    key = api_key
    session_id = session.session_id
    token = opentok.generate_token(session_id)
    return render_template('index.html', api_key=key, session_id=session_id, token=token)

if __name__ == "__main__":
    app.debug = True
    app.run()

1 个答案:

答案 0 :(得分:2)

我尝试了你的代码并且它对我起作用,直到我将文件名更改为opentok.py(从堆栈跟踪看起来它是你正在使用的名称)。

我认为这里最快的解决方案是将文件名从opentok.py更改为其他内容,因为它与库名称冲突。 (不要忘记删除opentok.pyc文件。)

如果您希望保留名称,则应this answer了解Absolute and Relative imports