Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

prevent 'import scrapy' from failing when twisted module is not available, also moved twisted 2.5.0 monkeypatch into a more specific module name

Pablo Hoffmanbf55a4708f66
diff --git a/scrapy/__init__.py b/scrapy/__init__.py
index bee059b0b..cb5e448b5 100644
--- a/scrapy/__init__.py
+++ b/scrapy/__init__.py
@@ -13,8 +13,7 @@ if sys.version_info < (2,5):
sys.exit(1)
# monkey patches to fix external library issues
-from scrapy.xlib.patches import apply_patches
-apply_patches()
+from scrapy.xlib import twisted_250_monkeypatches
# optional_features is a set containing Scrapy optional features
optional_features = set()
diff --git a/scrapy/xlib/patches.py b/scrapy/xlib/patches.py
index 6c9c20d96..1395161c8 100644
--- a/scrapy/xlib/patches.py
+++ b/scrapy/xlib/patches.py
@@ -1,12 +1,8 @@
"""
Monkey patches for supporting Twisted 2.5.0
-"""
-
-import twisted
-def apply_patches():
- if twisted.__version__ < '8.0.0':
- add_missing_blockingCallFromThread()
+NOTE: This module must not fail if twisted module is not available.
+"""
# This function comes bundled with Twisted 8.x and above
def add_missing_blockingCallFromThread():
@@ -42,3 +38,11 @@ def add_missing_blockingCallFromThread():
from twisted.internet import threads
threads.blockingCallFromThread = blockingCallFromThread
+
+try:
+ import twisted
+ if twisted.__version__ < '8.0.0':
+ add_missing_blockingCallFromThread()
+except ImportError:
+ pass
+