Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Unit test fail with not being able to find global module

From the root of my project I am running the standard python unit-testing command.

python3 -m unittest

I end up with

doc = yaml.safe_load(f)
NameError: name 'yaml' is not defined

Here's a blueprint of the test code:

import unittest
import yaml
from validators.utils import get_settings_key
# from validators.validators import validate_assumed_role


class TestValidators(unittest.TestCase):
    
    def setUp(self):
        with open("boilerplate_aws.yaml") as f:
            self.doc = yaml.safe_load(f)
            self.infrastructure = self.doc["parameters"]["infrastructure"]
            self.inventory_role = self.infrastructure["assumed_role_arn"]
 
    def test_role_validator(self):
        settings_role = get_settings_key("assumed_role_arn")

        # self.assertTrue(validate_assumed_role(self.inventory_role, settings_role))
# 

if __name__ == '__main__':
    unittest.main()

I consider the yaml module as a global one coming by default in Python. Just wondering if I am missing something or it's some project messed up structure ? Just for safety check, I looked up the package and it's here locally. This is under Ubuntu. No venvs, or anything like that.

Thanks.


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

1 Answer

0 votes
by (71.8m points)

Try running the following:

sudo easy_install pip
sudo python -m pip install pyyaml

If that doesn't work try installing a specific version like this:

pip install pyyaml==X.XX

These possible solutions were found on this github issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...