用另一个脚本调用脚本时的Python导入模块

时间:2017-02-27 08:35:29

标签: python path

我有以下python structure directory structure

login_logout_test.py我使用以下导入,当我运行此脚本时,一切正常(所有模块都已正确导入)

import sys, os
parentPath = os.path.abspath("..")
if parentPath not in sys.path:
    sys.path.insert(0, parentPath)
import common_test_functions as cts
from functions import login_logout_functions as llf

但是当login_logout_test.py调用此脚本(CommandRunner.py)时会发生此错误:

  

没有名为' common_test_functions'

的模块

1 个答案:

答案 0 :(得分:0)

实际上我已经设法解决了我自己的问题:

import sys, os
from selenium import webdriver
# adding path to common_test_functions to the sys.path
# so this module could be invoked
parentPath = os.path.abspath("..\\..")
if parentPath not in sys.path:
        sys.path.append(parentPath)
from functions import login_logout_functions as llf
from tests import common_test_functions as cts

还有一个文件,它对脚本参数是必需的。以下是在两种情况下都有此文件路径的代码(单独运行此脚本或通过其他脚本调用它):

parameters_directory_path = os.path.dirname(os.path.realpath(__file__))
parameters_file_name = "login_logout_test_parameters.tsv"
parameters_file_path = os.path.join(parameters_file_path, parameters_file_name)

如果有人有更好的,请发布。

提前谢谢。

的Stefan