tornado.options.Error: Option already defined in

Go To StackoverFlow.com

1

I'm learning tornado now, and I think it's an excellent framework. Everything is well until I separate some methods from main.py.

For example:

In the main.py which is my server startup program. I use tornado.options.define to define some properties like this:

define("port", default=8888, help="run on the given port", type=int)
define("mysql_host", default="127.0.0.1:3306", help="blog database host")
define("mysql_database", default="forum", help="database name")
define("mysql_user", default="root", help="database user")
define("mysql_password", default="passwd", help="database password")

And there is a BaseHandler in it:

class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
    return self.application.db

def get_current_user(self):
    return self.get_secure_cookie("username")

And in another file, such as topic.py, every handler in the topic.py will extend BaseHandler in main.py, so I import BaseHandler like this:

from main import BaseHandler

Then the problem happen when I startup my program:

Traceback (most recent call last):
  File "/Users/abc/money/main.py", line 85, in <module>
    define("port", default=8888, help="run on the given port", type=int)
  File "/Library/Python/2.7/site-packages/tornado-2.2-py2.7.egg/tornado/options.py", line 93, in define
options[name].file_name)
tornado.options.Error: ('Option %r already defined in %s', 'port', '/Users/suyejun/Dropbox/money/main.py')

How can I avoid this kind of problem? Or is there another way to write the code?

2012-04-04 07:55
by goofansu
The only way I can replicate this is have a define() (for the port) in topic.py Are you sure you do not have define() in topic.py - Elvis D'Souza 2012-04-05 06:55
No,I have no define() in topic.py, there is just a from main import BaseHandler in it, and there is only define() in main.py. Now I just put BaseHandler in a lonely file, such as base.py, and no need to import main so far - goofansu 2012-04-05 23:01


3

You need to move BaseHandler to a different file that main.py. The problem you are having is that you are effectively loading up the code for main.py twice. Once when you start your application and again when you import the code into topic.py.

I would recommend making a seperate file called something like 'pagemanager.py' and put any shared classes, functions, code there. Then you are import those into both main.py and topic.py without causing issues with the code that is specific to the initialization of Tornado.

2012-04-06 12:52
by Drahkar
ok, i've done it. I move the BaseHandler to base.py and it goes well. Thank yo - goofansu 2012-04-06 15:56
Glad I could help - Drahkar 2012-04-06 16:40
Solved my problem!! Thank you - Erum Huang 2016-07-20 11:00
Ads