Python venv中如何引入mysql 模块

现象

使用Python3.6 安装了 Scrapy,然后进去 venv/ 后执行 scrapy crawl spiderName,结果一直报错

    import mysql.connector
ModuleNotFoundError: No module named 'mysql'

分析

查看使用的Python是 3.7的,但实际上 venv的Python版本是 3.6

2019-11-27 00:19:58 [twisted] CRITICAL: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/usr/local/lib/python3.7/site-packages/scrapy/crawler.py", line 86, in crawl
    self.engine = self._create_engine()
  File "/usr/local/lib/python3.7/site-packages/scrapy/crawler.py", line 111, in _create_engine
    return ExecutionEngine(self, lambda _: self.stop())
  File "/usr/local/lib/python3.7/site-packages/scrapy/core/engine.py", line 70, in __init__
    self.scraper = Scraper(crawler)
  File "/usr/local/lib/python3.7/site-packages/scrapy/core/scraper.py", line 71, in __init__
    self.itemproc = itemproc_cls.from_crawler(crawler)
  File "/usr/local/lib/python3.7/site-packages/scrapy/middleware.py", line 53, in from_crawler
    return cls.from_settings(crawler.settings, crawler)
  File "/usr/local/lib/python3.7/site-packages/scrapy/middleware.py", line 34, in from_settings
    mwcls = load_object(clspath)
  File "/usr/local/lib/python3.7/site-packages/scrapy/utils/misc.py", line 46, in load_object
    mod = import_module(module)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/sul/projects/crawl2/crawler/crawler/pipelines/save.py", line 3, in <module>
    import mysql.connector
ModuleNotFoundError: No module named 'mysql'
(venv) ben:crawler sul$ python -V
Python 3.6.1

实际上是有的

(venv) ben:crawler sul$ pip freeze
mysql-connector-python==8.0.18
protobuf==3.11.0
six==1.13.0
(venv) ben:crawler sul$ 

问题

(venv) ben:crawler soul$ scrapy version -v 
Scrapy       : 1.7.3
lxml         : 4.4.1.0
libxml2      : 2.9.9
cssselect    : 1.1.0
parsel       : 1.5.2
w3lib        : 1.21.0
Twisted      : 19.7.0
Python       : 3.7.2 (default, Feb 12 2019, 08:16:11) - [Clang 9.0.0 (clang-900.0.39.2)]
pyOpenSSL    : 19.0.0 (OpenSSL 1.1.1c  28 May 2019)
cryptography : 2.7
Platform     : Darwin-18.7.0-x86_64-i386-64bit
(venv) ben:crawler soul$ python -V
Python 3.6.1

为何在venv 里面,Python版本是 3.6,但执行 scrapy crawl spiderName 却用的是全局的 Python版本?导致现在无法执行scrapy crawl,但直接写mysql 链接文件,在 venv中却可以执行。

阅读 2.5k
1 个回答

scrapy安装的环境和你的venv不一致,你可以which scrapy然后cat看下shebang里的解释器用的哪个就知道

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进