在Windows 10 CMD中使用命令后(命令提示符)C:\ Users \ my.name \ PycharmProjects \ PROJECTNAME \ Tests> python -m unittest login.py
我遇到一个错误:
ImportError: Failed to import test module: login
Traceback (most recent call last):
File "C:\Users\my.name\AppData\Local\Programs\Python\Python37-32\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "C:\Users\my.name\PycharmProjects\PROJECTNAME\Tests\login.py", line 4, in <module>
from Pages.loginPage import LoginPage
ModuleNotFoundError: No module named 'Pages'
我的代码结构:
PROJECTNAME
|-- Drivers
|-- Pages
| |-- loginPage.py
|-- Tests
| |-- login.py
login.py:
import unittest
from selenium import webdriver
from Pages.loginPage import LoginPage
import HtmlTestRunner
class LoginTest(unittest.TestCase):
C:\Users\my.name\PycharmProjects\PROJECTNAME>python -m unittest Tests.login
命令运行正常,但是为什么
C:\Users\my.name\PycharmProjects\PROJECTNAME\Tests>python -m unittest login.py
无法正常工作并引发错误吗?
答案 0 :(得分:1)
尝试从C:\Users\my.name\PycharmProjects\PROJECTNAME
到
C:\Users\my.name\PycharmProjects\PROJECTNAME\>python -m unittest Test\login.py
如果不起作用,请尝试相对导入,例如
from ..Pages.loginPage import LoginPage
并从PROJECTNAME
目录运行