简体   繁体   中英

ImportError: No module named toripchanger, how to launch scrapy with right python version

Python 3 is not my default version. I want to use it, because one the package I want to use toripchanger is only avaible under Python3.

So my pip3 version is:

C:\Users\Truc>pip3 -V
pip 18.1 from c:\python\python37\lib\site-packages\pip (python 3.7)

I use a scrapy.spider that I used to run in python 2.7 version and it works perfectly, but know I am using 3.7 in oder to use toripchanger I precised in my files this at the top of it:

#!"C:\Python37\python.exe"
# -*- coding: utf-8 -*-

the code above concerns the spider's file itself, the middlewares.py , the settings.py , the items.py , and pipelines.py .

In the middlewares.py I added some lines:

...
from scrapy.conf import settings
from toripchanger import TorIpChanger
...
ip_changer = TorIpChanger(reuse_threshold=3)

class ProxyMiddleware(object):
    _requests_count = 0

    def process_request(self, request, spider):
        self._requests_count += 1
        if self._requests_count > 10:
            self._requests_count = 0 
            ip_changer.get_new_ip()

        request.meta['proxy'] = settings.get('HTTP_PROXY')
        spider.log('Proxy : %s' % request.meta['proxy'])

The issue when I run my spider scrapy crawl spider is I obtain this error:

File "C:\Users\Truc\...\middlewares.py", line 13, 
in <module> from toripchanger import TorIpChanger
ImportError: No module named toripchanger

You may see the whole lines of error at the end of this topic

The issue for me is I tried what it is provided in that topic so I did pip3 install setuptools even pip3 install --upgrade setuptools , I did pip3 install Twisted-18.9.0-cp37-cp37m-win32.whl as adviced in this topic because it was necessary to install scrapy in my python 3.7 version.

As I said, the whole lines of error, where I see c:\\python27 while I precised #!"C:\\Python37\\python.exe" in my files

[in] scrapy crawl spider
[out]

2019-02-18 14:27:11 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot:I_AM_A_POLITE_ROBOT)

2019-02-18 14:27:11 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0, libxml2 2.9.5, 
cssselect 1.0.3, parsel 1.4.0, 
w3lib 1.19.0, Twisted 17.9.0, 
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)], 
pyOpenSSL 17.5.0 (OpenSSL 1.1.0g  2 Nov 2017), 
cryptography 2.1.4, Platform Windows-7-6.1.7601-SP1
2019-02-18 14:27:11 [scrapy.crawler] INFO: Overridden settings
{'NEWSPIDER_MODULE': 'folder.spiders', 
'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['folder.spiders'],
'RETRY_TIMES': 5, 'BOT_NAME': 'I_AM_A_POLITE_ROBOT',
'RETRY_HTTP_CODES': [401, 403, 404, 408, 500, 502, 503, 504],
'AUTOTHROTTLE_ENABLED': True,'DOWNLOAD_DELAY': 2}

2019-02-18 14:27:11 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.throttle.AutoThrottle']

2019-02-18 14:27:11 [py.warnings] WARNING: 
c:\python27\lib\site-packages\scrapy\utils\deprecate.py:
156:ScrapyDeprecationWarning: 
`scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware` 
class is deprecated, use
`scrapy.downloadermiddlewares.useragent.UserAgentMiddleware`
instead ScrapyDeprecationWarning)
Unhandled error in Deferred:

2019-02-18 14:27:11 [twisted] CRITICAL: Unhandled error in Deferred:

2019-02-18 14:27:11 [twisted] CRITICAL:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\twisted\internet\defer.py", line 1386, in
_inlineCallbacks
result = g.send(result)
File "c:\python27\lib\site-packages\scrapy\crawler.py", line 98, in crawl
six.reraise(*exc_info)
File "c:\python27\lib\site-packages\scrapy\crawler.py", line 80, in crawl
self.engine = self._create_engine()
File "c:\python27\lib\site-packages\scrapy\crawler.py", line 105, in _create_engine
return ExecutionEngine(self, lambda _: self.stop())
File "c:\python27\lib\site-packages\scrapy\core\engine.py", line 69, in __init__self.downloader = downloader_cls(crawler)
File "c:\python27\lib\site-packages\scrapy\core\downloader\__init__.py", line88, in __init__self.middleware = DownloaderMiddlewareManager.from_crawler(crawler)
File "c:\python27\lib\site-packages\scrapy\middleware.py", line 58, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "c:\python27\lib\site-packages\scrapy\middleware.py", line 34, in from_settings
mwcls = load_object(clspath)
File "c:\python27\lib\site-packages\scrapy\utils\misc.py", line 44, in load_object
mod = import_module(module)
File "c:\python27\lib\importlib\__init__.py", line 37, in import_module__import__(name)
File "C:\Users\...\middlewares.py", line 13, in <module>
from toripchanger import TorIpChanger
ImportError: No module named toripchanger

好的,以防万一您在3.7之后安装了Python 2.7,Windows默认情况下会将最新安装的Python版本视为默认版本。

Windows will always use the default python.

https://docs.python.org/3/faq/windows.html#how-do-i-make-python-scripts-executable

To use some other python version without changing the Windows default, you must do something like this (the exact path will vary per system setup):

C:\Program Files\[your python path]\python.exe "myfile.py"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM