使用 python -m pytest 与 pytest 时出现 FileNotFoundError [英] FileNotFoundError when using python -m pytest vs. pytest

查看:193
本文介绍了使用 python -m pytest 与 pytest 时出现 FileNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我使用的 IDE 更改为 VSCode.在大多数情况下,我喜欢它,但有一个我似乎无法解决的特殊问题.我也没有意识到这是一个问题,直到我移动了 IDE.

I recently changed the IDE I am using to VSCode. For the most part I like it, but there is one particular problem that I can't seem to resolve. I didn't realize it was a problem either, until I moved IDEs.

我有这样的目录结构:

my_app
├── env
│   ├── bin
│   ├── include
│   ├── lib
│   ├── lib64 -> lib
│   ├── pyvenv.cfg
│   └── share
├── my_app
│   ├── expected_results
│   ├── __init__.py
│   ├── test_data
│   └── tests
├── pytest.ini
├── README.rst
├── setup.cfg
└── setup.py

当我启动我的虚拟环境时,我坐在这个目录结构的根目录下.

When I launch my virtual environment I am sitting at the root of this directory structure.

我通过发出此命令(或提供其他选项)来运行我的测试.这目前有效:

I run my tests by issuing this command (or providing additional options). This currently works:

pytest

但是,当 VSCode 启动时,它吐出一个错误,说它找不到预期的文件:

But, when VSCode launches, it spits out an error saying it can't find an expected file:

E   FileNotFoundError: [Errno 2] No such file or directory: 'my_app/expected_results/expected_available_items.yml'

经过一番调查,我发现这是因为当 VSCode 启动时,它会发出以下命令:

After some investigation, I figured out that this is because when VSCode launches it issues the following command:

python -m pytest

我通过这样做来设置该路径:

I am setting that path by doing this:

import pathlib
EXPECTED_RESULTS_BASE = pathlib.Path("my_app/expected_results")
expected_results = EXPECTED_RESULTS_BASE.joinpath('expected_available_items.yml')

我需要修改什么,以便我的测试在我发出 pytest 命令时继续运行,并且在我(或我的 IDE,显然)发出 python -m 时运行pytest?

What do I need to modify so that my tests will continue to operate when I just issue a pytest command AND will operate if I (or my IDE, apparently) issues python -m pytest?

我希望可以安全地假设 VSCode 像我一样从 my_app 的根目录启动它?

I hope it's safe to assume that VSCode is launching this from the root of my_app like I am?

推荐答案

可能没有足够的信息直接为您回答这个问题,但让我们尝试一些事情:

Probably not enough information to answer this for you straight out, but lets try some things:

  • 在您的测试代码中,在出现错误的行上方插入一些这样的行,看看它们是否打印出您所期望的
print(os.getcwd())
print(EXPECTED_RESULTS_BASE.absolute())

  • 由于您使用的是 venv 并且错误是使用不同的命令调用 pytest 的结果,请尝试使用 which 来查看您是否实际上调用了不同的东西.激活 venv 之前和之后:
    • Since you're using a venv and the error is a result of calling pytest with a different command, try using which to see if you're actually calling different things. Both before and after activating your venv:
    • which pytest
      which python
      

      python -m pytest 将调用与您刚刚调用的 python 版本一起安装的 pytest 模块.如果 python 调用的版本与您从 venv 中的 pytest 获得的版本不同,那么这可能是问题所在.

      python -m pytest will call the pytest module installed with the version of python you've just called. If python calls a different version than you're getting from pytest inside your venv, then that could be the problem.

      您应该能够通过查看文件顶部的 hashbang 来检查 pytest 正在调用哪个 python 版本

      You should be able to check which python version pytest is calling by looking at the hashbang at the top of the file

      head -1 $(which pytest)
      

      在我的系统上,macOS 安装了 anaconda Python,我从这些命令中得到以下内容

      On my system, macOS with anaconda Python installed, I get the following from those commands

      $ which pytest
      /Users/Shannon/anaconda/bin/pytest
      
      $ which python
      /Users/Shannon/anaconda/bin/python
      
      $ head -1 $(which pytest)
      #!/Users/Shannon/anaconda/bin/python
      

      这告诉我 pytest 正在调用与我调用 python 时获得的相同版本的 python.因此,在我的默认环境中,pytestpython -m pytest 应该对我产生相同的结果.

      This tells me that pytest is calling the same version of python I get when I call python. As such, pytest and python -m pytest should result in the same thing for me, inside my default environment.

      在运行测试之前,您确定 VSCode 正确加载了您的 venv 吗?

      Are you sure VSCode is loading your venv correctly before running the test?

      这篇关于使用 python -m pytest 与 pytest 时出现 FileNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆