python自定义import 路径(当包位置不在pythonpath默认路径下时,会出现no module named xxx.自定义import路径可解决该问题
现暂有方法三种,均可生效
1:临时修改导入路径(重启后失效)
import sys
sys.path.append('/usr/local/path/to/xxx')
2:临时设置环境变量(重启后失效):
export PYTHONPATH=/usr/local/path/to/xxx
source ~/.bashrc # 使环境变量立即生效
3:永久增加import路径:
echo '/usr/local/path/to/xxx' > /usr/lib/python2.7/dist-packages/xxx_.pth
echo '/usr/local/path/to/xxx' > /usr/lib/python2.7/site-packages/xxx_.pth
若配置虚拟环境,请以python虚拟环境中的dist-packages/site-packages路径代替,此处以默认的python2.7路径为例