twistd -n web不能使用Klein和Python3

时间:2018-06-16 13:02:56

标签: python-3.x twisted klein-mvc

我试图与Klein一起运行扭曲的网络并遇到很多问题。 即使尝试从一个例子运行它 - 获得相同的结果。 Source

from klein import Klein
app = Klein()

@app.route('/')
def hello(request):
    return "Hello, world!"

resource = app.resource

然后运行它(在这个例子中,上面的文件保存为twistdPlugin.py:

$ twistd -n web --class=twistdPlugin.resource

错误: sh-3.2# twistd -n web --class=twistdPlugin.resource Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/bin/twistd", line 11, in <module> load_entry_point('Twisted==17.9.0', 'console_scripts', 'twistd')() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/scripts/twistd.py", line 29, in run app.run(runApp, ServerOptions) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/application/app.py", line 657, in run config.parseOptions() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/application/app.py", line 624, in parseOptions usage.Options.parseOptions(self, options) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/python/usage.py", line 267, in parseOptions self.subOptions.parseOptions(rest) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/python/usage.py", line 255, in parseOptions self._dispatch[optMangled](optMangled, arg) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/python/usage.py", line 411, in <lambda> fn = lambda name, value, m=method: m(value) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/web/tap.py", line 121, in opt_class classObj = reflect.namedClass(className) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/python/reflect.py", line 173, in namedObject module = namedModule('.'.join(classSplit[:-1])) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/twisted/python/reflect.py", line 159, in namedModule topLevel = __import__(name) ModuleNotFoundError: No module named 'twistdPlugin'

如何使用它?

1 个答案:

答案 0 :(得分:0)

你可以解决这2种方法之一。第一种方法是将包含源代码的目录添加到PYTHONPATH环境变量中。这是最简单的方法。

PYTHONPATH=$(pwd) twistd -n web --class twistdPlugin.resource

或者你可以制作一个virtualenv并在那里安装你的twistdPlugin模块。这样你就不必自己搞乱环境变量了。

事情的tl; dr是用于在 Python 2 Twisted&lt;中获取当前目录的twistd。 16.4但是它不会再用 Python 3 执行那个最新的Twisted。换句话说,twistd期望所有模块都是可导入的。尽管有时令人讨厌,但这种设计使得扭曲的应用程序变得便携。