我终于全力投入Python,但出于某种原因,我遇到了一个问题,即Python可以在交互式CLI中找到一个模块,然后在我编写脚本时就不能了。该模块特别是位于/Library/Python/2.7/site-packages中的mysql.connector。从交互式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
>>>
我正式难过,不知道为什么会这样。有没有人遇到过这个?
答案 0 :(得分:2)
您的主目录或新目录中有mysql
个或其他内容。它可能是.py
文件,也可能是实际的包(在错误的地方;)。解决这个问题,你的问题应该消失。
答案 1 :(得分:2)
在回溯中,我看到您将文件命名为mysql.py
。因此,当您尝试从mysql.py
导入某些内容时,解释器会在您自己的脚本中查找它,而不是“实际”模块。它不会投射更宽的网,而是立即放弃。要解决此问题,请将脚本重命名为不屏蔽任何模块名称的内容。