python导包错误 ImportError: cannot i
2018-12-16 本文已影响0人
码仙丶
- 开发工具pycharm
- 运行方式
- pycharm右键运行
- python命令执行
-
项目目录结构
cloud_platform - 代码
import os
import sys
from test import a
a.test()
- 问题
Traceback (most recent call last):
File "monitor_yarn/aa.py", line 1, in <module>
from test import a
ImportError: cannot import name a
- 在pycharm中右键运行没问题,但是去服务器直接用python命令去直接执行就会报上面的错误
- 这是因为在用python命令执行时test包默认没有引入到sys.path中
- 继续看代码
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
- 看上面打印信息可以看到并没有test这个包的路径,而用pycharm右键运行项目时,打印:
/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
- 可以看到是有当前运行的文件的路径 And 这个项目的根路径
- 解决方法
- 三种解决方法
- 在/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages下添加一个xx.pth,xx是要导入的模块名,然后写上本地项目路径,这种方法在换一个环境用时太麻烦,不推荐
- 将包的路径添加到sys.path中
#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')
这种方式每个文件都需要添加,也很麻烦
- 添加PYTHONPATH环境变量(推荐)
export PYTHONPATH=/Users/wangtaiyang/PycharmProjects/cloud_platform_script/monitor_yarn
- 这种方式也是我最喜欢的方式,换了执行环境,只需配置一条环境变量而已,比起上面两种方式要方便的多
总结
pycharm中运行代码,它自动就为你设置好了项目的路径到sys.path中,而我们的项目往往要到服务器运行,而在用python命令执行时,项目的路径需要我们自己配置