ImportError: No module named twisted.persisted.sty

2020-08-26 04:21发布

问题:

From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.

I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).

From the import of scrapy on the first line, I get this error when running the program: ImportError: No module named twisted.persisted.styles

The whole script, if it's helpful, points to this:

Traceback (most recent call last):
  File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
    import scrapy
  File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
    from . import _monkeypatches
  File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
    import twisted.persisted.styles  # NOQA
ImportError: No module named twisted.persisted.styles

Any suggestions on how to tackle this problem?

回答1:

Just try to force the update of twisted :

pip install twisted --upgrade

That works for me with python3.4 and Scrapy==1.1.0rc1



回答2:

Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:

  1. The easiest way to install a python package is through pip.
  2. If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
  3. If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
  4. If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.

note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!



回答3:

It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:

Installing Twisted through pip broken on one server



回答4:

Had this exact thing on FreeBSD. Solution (as root/sudo):

chmod -R go+rX /usr/local/lib/python2.7/site-packages

Some directory permissions weren't set up right on install.