[英]FileNotFoundError when using python -m pytest vs. pytest
I recently changed the IDE I am using to VSCode. 我最近将正在使用的IDE更改为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. 在移动IDE之前,我也没有意识到这是一个问题。
I have a directory structure like this: 我有这样的目录结构:
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
But, when VSCode launches, it spits out an error saying it can't find an expected file: 但是,当VSCode启动时,它会吐出一个错误,指出找不到所需的文件:
E FileNotFoundError: [Errno 2] No such file or directory: 'my_app/expected_results/expected_available_items.yml'
After some investigation, I figured out that this is because when VSCode launches it issues the following command: 经过调查,我发现这是因为VSCode启动时会发出以下命令:
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')
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
? 我需要修改什么,以便当我发出pytest
命令时测试可以继续运行,并且在我(显然是我的IDE)发出python -m pytest
?
I hope it's safe to assume that VSCode is launching this from the root of my_app
like I am? 我希望可以安全地假设VSCode像我一样从my_app
的根目录启动此代码?
Probably not enough information to answer this for you straight out, but lets try some things: 可能没有足够的信息来直接为您解答,但请尝试一些操作:
print(os.getcwd())
print(EXPECTED_RESULTS_BASE.absolute())
which
to see if you're actually calling different things. 由于您使用的是VENV和错误是调用pytest用不同的命令的结果,请尝试使用which
,看看是否你实际上调用不同的东西。 Both before and after activating your venv: 在激活您的venv之前和之后: which pytest
which python
python -m pytest
will call the pytest module installed with the version of python you've just called. python -m pytest
将调用与您刚刚调用的python版本一起安装的pytest模块。 If python
calls a different version than you're getting from pytest
inside your venv, then that could be the problem. 如果python
调用的版本与您从pytest
内部的pytest获取的版本不同,则可能是问题所在。
You should be able to check which python version pytest is calling by looking at the hashbang at the top of the file 您应该能够通过查看文件顶部的hashbang来检查pytest正在调用哪个python版本
head -1 $(which pytest)
On my system, macOS with anaconda Python installed, I get the following from those commands 在我的系统上,安装了anaconda Python的macOS,我从这些命令中获得了以下内容
$ which pytest
/Users/Shannon/anaconda/bin/pytest
$ which python
/Users/Shannon/anaconda/bin/python
$ head -1 $(which pytest)
#!/Users/Shannon/anaconda/bin/python
This tells me that pytest
is calling the same version of python I get when I call python
. 这告诉我pytest
调用的是我调用python
时得到的相同版本的python
。 As such, pytest
and python -m pytest
should result in the same thing for me, inside my default environment. 因此,在我的默认环境中, pytest
和python -m pytest
应该对我产生相同的结果。
Are you sure VSCode is loading your venv correctly before running the test? 在运行测试之前,您确定VSCode是否正确加载了venv吗?
Assuming you have a virtual environment selected for your Python interpreter in VS Code: 假设您在VS Code中为Python解释器选择了一个虚拟环境:
python -m pip install pytest
运行python -m pip install pytest
That will install pytest into the virtual environment which is why python -m
fails (pytest globally on your PATH is installed in some global Python install which a virtual environment won't have access to). 这会将pytest安装到虚拟环境中,这就是python -m
失败的原因(在虚拟环境无法访问的某些全局Python安装中,将PATH上的pytest全局安装)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.