Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

python - importlib does not show neither error nor does call the script

It was just three weeks, I started learning python and speeding up so fast to pick many things. Honestly I jumped into the ocean without knowing swimming.

Unfortunately, I got stuck at one point for many hours and could not find the issue after referring many answers in stackoverflow.

Let me explain,

I have the following project folder structure along with python files,

|-transform
   |---mask
   |-----config/
              application_config.json
   |-----logs
   |-----mask/
             mask.py
             constants.py
             maskconfigparser.py
   |-----tests/
              test_string_parser.py

test_string_parser.py should call mask.py in the mask folder so that it executes by reading configuration in the config folder.

Here is the code in test_string_parser.py

import unittest
from pathlib import Path
import importlib.util
import sys

sys.path.insert(1, '/transform/mask/')

class TestStringMasking(unittest.TestCase):
def test_string_masking(self):
    spec = importlib.util.spec_from_file_location(
        "mask", 
        "/transform/mask/mask/mask.py")
    mask_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mask_module)
if __name__ == '__main__':
    unittest.main() 
                                 

I call the tests from linux script file run_tests.sh as shown below.

export PYTHONPATH="${PYTHONPATH}:/transform/mask/mask/"
python -m unittest discover -v   

when I run the sh script, the mask.py was never executed and I could not find what was the issue while there is no error.

Please help and advice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...