python中添加模块儿路径的代码
1.首先添加环境变量
vim ~/.bash_profile #添加python环境变量如下两行代码: PYTHONPATH="$PYTHONPATH:./" export PYTHONPATH
2.python脚本中代码
# -*- coding: utf-8 -*- import os import sys import yaml # 引入子模块路径 #获取当前脚本路径,此处‘/../’上一层目录。如果模块儿在本级目录就不需要了。 basedir = os.path.abspath(os.path.dirname(__file__)) ADDITIONAL_PACKAGE_PATH = [ '/../libs/package_A', ] for p in ADDITIONAL_PACKAGE_PATH: package_abs_path = basedir + p if package_abs_path not in sys.path: sys.path.append(package_abs_path)
3.调用模块儿时
from package_A import ClassA, ClassB