ModuleNotFoundError:“没有名为'unittest.main'的模块python 3.7

时间:2019-12-02 05:12:51

标签: python python-3.x unit-testing

我有一个用Python编写的小型单元测试程序。在“导入单元测试”中有问题。这是错误消息(我在Pycharm和mac catalina os中运行它):

  

导入unittest文件   “ /usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/init.py”,   第64行,在       从.main导入TestProgram,主ModuleNotFoundError:没有名为“ unittest.main”的模块

以退出代码结束的过程

当我在Python3命令行中运行它时,它具有相同的错误。在Python(python2)中运行它没问题。 该程序于本周初在python3上运行。 这是我的程序:

import unittest
from selenium import webdriver
import random

class MyTestCase(unittest.TestCase):

    def numberToString(self, number):
        digitNames = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
        result = ""
        while number > 0:
            currentDigit = number % 10  # modulus operator gives remainder after integer division
            # in this case it is the rightmost digit
            result = digitNames[currentDigit] + ' ' + result
            number = number // 10  # integer division throws away the remainder
        return result

    def test_something(self):
        browser = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
        for i in range(1, 2):
            a = random.randint(1, 65535)
            b = random.randint(1, 65535)
            browser.get("http://www.math.com/students/calculators/source/basic.htm")
            aSpell = self.numberToString(a).split()
            bSpell = self.numberToString(b).split()
            for spell in aSpell:
                browser.find_element_by_name(spell).send_keys(" " + spell)
            browser.find_element_by_name("plus").send_keys(" +")
            for spell in bSpell:
                browser.find_element_by_name(spell).send_keys(" " + spell)
            browser.find_element_by_name("DoIt").send_keys(" =")

            self.assertEqual(eval(browser.find_element_by_name("Input").get_attribute("value")), a + b)
        browser.quit()

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

我猜可能是路径问题,但我不知道如何解决。我需要重新安装python3吗?我当前的版本是3.7。

3 个答案:

答案 0 :(得分:0)

这就是我得到的:

pip install unittest
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
ERROR: Could not find a version that satisfies the requirement unittest (from versions: none)
ERROR: No matching distribution found for unittest

当我运行python2.7(“ python Hello_world.py”而不是“ python3 hello_world.py”)时,没问题。

我有两个同名文件。我需要同时保留两者吗? /System/Volumes/Data/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/operator.py /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/operator.py

答案 1 :(得分:0)

这是我在Pycharm终端中输入“ pip install unittest”后的输出消息:

默认的交互式shell现在是zsh。 要将您的帐户更新为使用zsh,请运行chsh -s /bin/zsh。 有关更多详细信息,请访问https://support.apple.com/kb/HT208050。 (venv)Xiaohongs-MacBook-Air:pimTest.py1 xiaohongyu $ pip install unittest 收集单元测试   找不到满足单元测试要求的版本(来自版本:) 找不到与单元测试匹配的分布

答案 2 :(得分:0)

我有两个名称相同但位于不同目录中的文件: /System/Volumes/Data/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/operator.py /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/operator.py

我需要删除一个还是要保留两个?