PYTHONPATH -bash:export:`myproject / tornado_main.py':不是有效的标识符

时间:2014-07-15 18:14:23

标签: python django bash

我正在努力学习https://github.com/bdarnell/django-tornado-demo的django-tornado-demo。我需要运行这个可执行文件。以下是代码块:

#!/usr/bin/env python

# Run this with
# PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py
# Serves by default at
# http://localhost:8080/hello-tornado and
# http://localhost:8080/hello-django

from tornado.options import options, define, parse_command_line
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi

define('port', type=int, default=8080)

class HelloHandler(tornado.web.RequestHandler):
  def get(self):
    self.write('Hello from tornado')

def main():
  parse_command_line()
  wsgi_app = tornado.wsgi.WSGIContainer(
    django.core.handlers.wsgi.WSGIHandler())
  tornado_app = tornado.web.Application(
    [
      ('/hello-tornado', HelloHandler),
      ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
      ])
  server = tornado.httpserver.HTTPServer(tornado_app)
  server.listen(options.port)
  tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
  main()

当我尝试执行Python路径导出时:

  

export PYTHONPATH = DJANGO_SETTINGS_MODULE = myproject.settings   的myproject / tornado_main.py

我明白了:

  

-bash:export:`myproject / tornado_main.py':不是有效的标识符

如何使用顶部建议的PYTHONPATH运行此文件?感谢

1 个答案:

答案 0 :(得分:0)

不要使用export,只需按照CLI编写的方式直接运行它:

PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py

这将使用更改的环境变量运行tornado_main.py。您也可以这样做:

export PYTHONPATH=. 
export DJANGO_SETTINGS_MODULE=testsite.settings 
testsite/tornado_main.py

如果您希望对PYTHONPATHDJANGO_SETTINGS_MODULE的更改影响您从该终端启动的所有流程。

修改

dan@dantop2:~/code/django-tornado-demo$ ls
testsite
dan@dantop2:~/code/django-tornado-demo$ ls testsite/
__init__.py  manage.py  settings.py  tornado_main.py  urls.py  views.py
dan@dantop2:~/code/django-tornado-demo$ PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py
<server starts running>