Python venv中如何引入mysql 模块

发布于 2022-09-11 23:46:11 字数 3201 浏览 14 评论 0

现象

使用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中却可以执行。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2022-09-18 23:46:11

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文