1

i'm try to run twistd web with Klein and got a lot of issues. Even if try run it from an example - got the same results. Source

from klein import Klein
app = Klein()

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

resource = app.resource

Then run it (in this example, the file above is saved as twistdPlugin.py:

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

Errors: 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'

How to be with this?

1 Answers1

1

You can solve this 1 of 2 ways. First way would be to add the directory with your source code to the PYTHONPATH environment variable. This is the easiest method.

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

Or you could make a virtualenv and install your twistdPlugin module there. This way you won't have to mess with environment variables yourself.

The tl;dr of the matter is that twistd used to source the current directory in Python 2 Twisted < 16.4 but it doesn't do that anymore with Python 3 the lastest Twisted. In other words, twistd expects all modules to be importable. Despite being a nuisance at times, this design makes twistd apps portable.

notorious.no
  • 4,919
  • 3
  • 20
  • 34
  • Actually not related to Python 3 at all. On neither Python 2 nor Python 3 does the most recent release of Twisted's `twistd` load plugins found at ${PWD}. The change was introduced in 16.4 (as an unintentional, unannounced side-effect of unrelated maintenance), I believe. See https://twistedmatrix.com/pipermail/twisted-python/2016-September/030780.html – Jean-Paul Calderone Jun 16 '18 at 14:17
  • Thanks, maybe you know better wait to run Klein on server as web app? Local it's working good and i try run on server like do with Flask with Apache2 but for Klein it's not working. – Максим Дихтярь Jun 16 '18 at 15:02
  • How's your Apache setup? Flask is usually run via a WSGI server which Klein doesn't need and won't work with. I've run Klein behind both Apache and Nginx using a proxy with no issues. – notorious.no Jun 16 '18 at 16:08