Skip to main content
 首页 » 编程设计

python之pytest导入错误

2025年05月04日10txw1958

我正在尝试导入当前目录中的文件,但我得到的是这样的

 module_not_in_the_pythonpath__ 

对于我在该文件中定义的所有变量。

我已经添加了路径

      C:/Project/main_test_folder 

在python路径中

我的目录结构是这样的

 main_test_folder 
       test_folder1             //working fine here 
              __init__.py 
              Constants.py 
              Some test file Constant.py will be imported. 
       test_folder2            //working fine here 
              __init__.py 
              Constants.py 
              //Some test file Constant.py will be imported. 
       test_folder3              //for this folder alone its not working 
              __init__.py 
              Constants.py 
              Common.py          //Constant.py is imported in this(not working) 
              Testcase.py 
              //Some more test file ,Constant.py,Common.py will be imported in each file. 

通用.py

     from constants import * 
     class Payload(object): 
 
 
         def __init__(self, params): 
            ''' 
            Constructor 
            ''' 
         @staticmethod 
         def createPayLoad(Name,model):  //NAME and MODEL Defined in constants.py 
              PAYLOAD_PROFILE={ 
                 NAME:              Name, 
                 MODEL:                     model, 
 
                 } 
 
            return PAYLOAD 

测试用例.py

     from common import *        //this line is not working 
     from constants import * 

错误:

   test_folder3\test_01_rename.py:15: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  @staticmethod def    
   createPayLoad(Name, model): PAYLOAD={ > NAME: Name,MODEL: model} E NameError: global  
   name 'NAME' is not defined test_folder3\common .py:34: NameError</failure> 

请您参考如下方法:

使用 import * see 是不好的做法
如果您在模块的 __init__.py see 中设置 __ALL__ 变量,则可以这样做