Python can find a module…and then it can't

坚强是说给别人听的谎言 提交于 2019-12-17 21:36:35

问题


I am finally going full steam into Python, but for some reason I have an issue where Python can find a module in the interactive CLI and then it can't when I write a script. The module is specifically mysql.connector located in /Library/Python/2.7/site-packages . As you can see from the interactive CLI session, it imports mysql.connector just fine. Echoing the sys.path shows 'Library/Python/2.7/site-packages'

Here's a new CLI window (I'm on a Mac 10.10). Note: initially when I login I am popped into my home dir which of course is normal.

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']
>>>

So I merely changed to another directory...

cd /Users/wilkie/Projects/dataparse

and just like that... it can't find 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
>>>

I am officially stumped and have no idea why this is happening. Anyone ever encounter this?


回答1:


In the traceback, I see that you named your file mysql.py. Therefore, when you try to import something from mysql.py, the interpreter looks for it in your own script, not the "actual" module. Instead of casting a wider net, it immediately gives up. To fix this, rename your script to something that doesn't mask any module names.




回答2:


You have a mysql something or other either in your home directory, or in your new directory. It might be a .py file or perhaps the actual package (in the wrong place ;). Fix that and your problem should go away.



来源:https://stackoverflow.com/questions/31443428/python-can-find-a-module-and-then-it-cant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!