运行py时,Python 2.7找不到模块。但是可以在终端找到模块

时间:2016-02-01 20:33:41

标签: python python-2.7 azure import

OSX,PyCharm,使用brew安装的python 2.7,只是尝试导入

from azure.mgmt.common import SubscriptionCloudCredentials
import azure.mgmt.compute
import azure.mgmt.network
import azure.mgmt.resource
import azure.mgmt.storage

收到错误:

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin/python2.7 XXX/pythonCode/p2.1/azure.py
Traceback (most recent call last):
  File "XXX/pythonCode/p2.1/azure.py", line 9, in <module>
    from azure.mgmt.common import SubscriptionCloudCredentials
  File "XXX/pythonCode/p2.1/azure.py", line 9, in <module>
    from azure.mgmt.common import SubscriptionCloudCredentials
ImportError: No module named mgmt.common

尝试:

  • 从源

    重新安装azure

    https://github.com/Azure/azure-sdk-for-python

  • 正在运行

    python -c "import site; print(site.getsitepackages())"

    我明白了 ['/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']

    其中/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages包含azure

  • 正在运行python -c "import sys; print(sys.path)"

    我得到了

    '', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages', '/usr/local/Cellar/numpy/1.10.4/libexec/nose/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages'
    

值得注意的是 正在运行

python -c "from azure.mgmt.common import SubscriptionCloudCredentials"

终端中的

根本没有错误。

有谁可以解释问题所在?

1 个答案:

答案 0 :(得分:3)

问题是您编写脚本的文件名为azure.py。导入此文件而不是您安装的模块azure,并且它没有必要的属性,从而导致此错误。

将代码所在的文件重命名为其他内容将解决此问题。