Python专区

python导包错误 ImportError: cannot i

2018-12-16  本文已影响0人  码仙丶
  1. 开发工具pycharm
  2. 运行方式
  1. 项目目录结构


    cloud_platform
  2. 代码
import os
import sys

from test import a
a.test()
  1. 问题
Traceback (most recent call last):
  File "monitor_yarn/aa.py", line 1, in <module>
    from test import a
ImportError: cannot import name a
import os
import sys

for i in sys.path:
    print i
from test import a
a.test()

#输出
/Users/wangtaiyang/PycharmProjects/cloud_platform_script/monitor_yarn
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/yarn_api_client-0.3.1-py2.7.egg
/Users/wangtaiyang/PycharmProjects/cloud_platform_script/monitor_yarn
/Users/wangtaiyang/PycharmProjects/cloud_platform_script
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/yarn_api_client-0.3.1-py2.7.egg
/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend
test
  1. 解决方法
#sys.py文件中,sys.path就是一个列表
path = [
    '/Applications/PyCharm.app/Contents/helpers',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
    '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
]

#在自己代码中向path列表中添加要导入的包的路径
os.sys.path.append('/Users/wangtaiyang/PycharmProjects/cloud_platform_script/test')

这种方式每个文件都需要添加,也很麻烦

  1. 添加PYTHONPATH环境变量(推荐)
export PYTHONPATH=/Users/wangtaiyang/PycharmProjects/cloud_platform_script/monitor_yarn

总结

pycharm中运行代码,它自动就为你设置好了项目的路径到sys.path中,而我们的项目往往要到服务器运行,而在用python命令执行时,项目的路径需要我们自己配置

上一篇下一篇

猜你喜欢

热点阅读