stefan.stt
stefan.stt

Reputation: 2627

Python importing module when calling the script by another script

I have the following python structure directory structure

In login_logout_test.py I use the following imports and everything works fine when I run this script (all the modules are imported properly)

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

But when this script (login_logout_test.py) is called by CommandRunner.py this error occurs:

No module named 'common_test_functions'

Upvotes: 3

Views: 1477

Answers (1)

stefan.stt
stefan.stt

Reputation: 2627

Actually I have contrived a solution to my own problem:

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

Also there is a file, that holds necessary for the script parameters. Here is the code to have this file path in both cases (running this script by itself or calling it by another script):

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)

If someone has a better one, please post it.

Thank you in advance.

Stefan

Upvotes: 2

Related Questions