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
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
#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')
这种方式每个文件都需要添加,也很麻烦
3. 添加PYTHONPATH环境变量(推荐)
export PYTHONPATH=/Users/wangtaiyang/PycharmProjects/cloud_platform_script/monitor_yarn
pycharm中运行代码,它自动就为你设置好了项目的路径到sys.path中,而我们的项目往往要到服务器运行,而在用python命令执行时,项目的路径需要我们自己配置