How do I run twisted from the console?

2019-06-16 19:10发布

问题:

I'm using Python 3 with Anaconda on Windows 7. I installed Twisted with conda install twisted, and now I'm trying to run twisted (or twistd?) from the console, but I get this error

'twisted' is not recognized as an internal or external command, operable program or batch file.

which makes me think a directory is missing from the path, as in this question. Anaconda is installed in C:\Anaconda3, but even in C:\Anaconda3\Lib\site-packages\twisted, there isn't a twisted.py or twistd.py file.

Am I doing something wrong, or am I looking in the wrong place for the file(s)?

Is this an issue because Twisted isn't officially ported to Python 3 yet?

回答1:

twistd runs twisted applications (though you can run a script with Twisted code in it like any other Python file) and should be in the bin directory inside your Anaconda installation directory, so if you can get conda, you can get twistd as well.

twisted is the library you use to write code that uses Twisted, so you can't run that from the command line.

Here is the status of Twisted on Python3 https://twistedmatrix.com/trac/milestone/Python-3.x

And here is the particular ticket about twistd not being available on Python3 yet https://twistedmatrix.com/trac/ticket/7497



回答2:

Don't confuse "Twisted" with "twistd". When you use "twistd", you are running the program with Python. "twistd" is a Python program that, among other things, can load an application from a .tac file (as you're doing here).

The "Twisted Command Prompt" is a Twisted installer-provided convenience to help out people on Windows. All it is doing is setting %PATH% to include the directory containing the "twistd" program. You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path.

(From How do you you run a Twisted application via Python (instead of via Twisted)?)

Run:

set PATH=%PATH%;C:\path\to\twistd.py

Where in C:\path\to\twistd.py you insert the path to the twistd.py file.



回答3:

Twisted is a Python library. To use it, you could import it e.g., here's a web server from twisted home page:

#!/usr/bin/env python
from twisted.web import server, resource
from twisted.internet import reactor, endpoints

class Counter(resource.Resource):
    isLeaf = True
    numberRequests = 0

    def render_GET(self, request):
        self.numberRequests += 1
        request.setHeader("content-type", "text/plain")
        return "I am request #" + str(self.numberRequests) + "\n"

endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter()))
reactor.run()

Save it to a file e.g., counter_server.py and run: py counter_server.py. You could visit http://localhost:8080/ to make sure it works (it doesn't with twisted-15.2.1 version on Python 3). Twisted is ported only partially to Python 3 (the graph is based on the data from a year ago).

twistd is a Python program that uses twisted Python package (note: e). It is not ported to Python 3 yet (pip install twisted installs it on Python 2 but it doesn't install it on Python 3).