Azam
Azam

Reputation: 797

Error running OpenTok Server Code (Python)

I have problem running flask run for OpenTok server code. How can I eradicate the error? Thanks in advance.

This is the error:

(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

This is the Python code:

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()

Upvotes: 1

Views: 166

Answers (1)

maikthomas
maikthomas

Reputation: 436

I tried your code and it worked for me, until I changed the filename to opentok.py (from the stack trace it appears that is the name you are using).

I think the quickest solution here is to change your filename from opentok.py to something else as it conflicting with the library name. (Don't forget to remove the opentok.pyc file too).

If you wish to keep the name you should see this answer about Absolute and Relative imports

Upvotes: 2

Related Questions