【问题标题】:Github testing can't find fileGithub 测试找不到文件
【发布时间】:2023-03-25 05:30:02
【问题描述】:

我有一个“模型”文件夹,里面有用于单元测试的不同模型。 文件层次结构如下

- test
  - models
    - nn_class.sav
    - nn_reg.sav
  - test_model

我运行 test_model,里面有单元测试。当我读取文件nn_class.sav 时,它通过提供路径"./models" 工作得非常好。而当我尝试在 Github 上运行相同的测试时,它会抛出 File not found 的错误。 我必须改用它,所以它可以在两种环境中找到文件。

model_name = 'nn_class.sav'
    if os.path.isdir('./models'):
        path = './models/'
    else:
        path = 'test/models/'

有没有一种优雅的方式来处理这个问题。有人向我推荐了固定装置,我不知道他们是否会有所帮助。

【问题讨论】:

    标签: python github pytest filenotfoundexception file-not-found


    【解决方案1】:

    不是一种巧妙的方法,最好避免此类技巧,但尝试这样做

    import sys
    sys.path.insert(0, '.') 
    

    在您的test_model 文件的开头,它将使用root 作为案例的运行路径。然后您可以使用根的相对路径。或任何文件夹,只需将. 更改为您需要的路径即可。

    在这里查看 cmets 的回复为什么最好避免这种技巧:https://stackoverflow.com/a/448279/2957102

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2019-09-29
      • 1970-01-01
      • 2017-08-03
      • 2018-01-01
      • 2014-09-17
      相关资源
      最近更新 更多