【发布时间】:2015-10-05 06:18:11
【问题描述】:
我终于开始全力投入使用 Python,但由于某种原因,我遇到了一个问题,即 Python 可以在交互式 CLI 中找到一个模块,然后在我编写脚本时却找不到。该模块特别是 mysql.connector 位于 /Library/Python/2.7/site-packages 。从交互式 CLI 会话中可以看到,它导入 mysql.connector 就好了。回显 sys.path 显示 'Library/Python/2.7/site-packages'
这是一个新的 CLI 窗口(我使用的是 Mac 10.10)。注意:最初当我登录时,我会弹出到我的主目录,这当然是正常的。
wilkie:~ wilkie$ which python
/usr/bin/python
wilkie:~ wilkie$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import mysql.connector
>>> sys.path
['', '/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg', '/Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
>>>
所以我只是换了一个目录...
cd /Users/wilkie/Projects/dataparse
就这样......它找不到 mysql.connector
wilkie:dataparse wilkie$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg', '/Library/Python/2.7/site-packages/cx_Oracle-5.2-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mysql.py", line 1, in <module>
import mysql.connector
ImportError: No module named connector
>>>
我被官方难住了,不知道为什么会这样。有人遇到过吗?
【问题讨论】:
-
你是怎么安装的?
import mysql;mysql.__file__输出什么? -
您的
dataparse目录中是否有一个名为mysql.py的模块? -
请执行
cd /Users/wilkie/Projects/dataparse,然后执行ls命令,让我们知道输出是什么。 -
@JaneWIlkie,来自非工作目录?确保仅
import mysql -
@JaneWIlkie,删除任何 .pyc 文件,否则您将再次遇到同样的问题
标签: python python-2.7 mysql-python python-module