简体   繁体   English

未访问“pytest”/无法解析导入“pytest” - Pylance

[英]"pytest" is not accessed / Import "pytest" could not be resolved - Pylance

I haven't touched python in a long time and I forgot how much of a pain importing can be.我已经很久没有碰过 python 了,我忘记了导入会有多痛苦。

So I'm using pylance and pipenv as my shell.所以我使用 pylance 和 pipenv 作为我的 shell。 I have pytest installed in my local env, I can run pytest from the command line and test my code.我在本地环境中安装了 pytest,我可以从命令行运行 pytest 并测试我的代码。 However, I can't import it in the same file I'm running my tests from.但是,我无法将它导入到运行测试的同一个文件中。 Any idea what might be causing that problem?知道可能导致该问题的原因是什么吗?

Python version 3.9 Python 3.9版

I have my tests in a separate file right below root.我在根目录下的一个单独文件中进行了测试。 I don't have a init .py file in tests.我在测试中没有init .py 文件。 I've read that that can cause problems.我读过这可能会导致问题。

probably Pylance is having problems with the path for site-packages folder for your virtual environment and returns Import "pytest" could not be resolved , you need to set python path in this way:可能Pylance对您的虚拟环境site-packages文件夹的路径有问题并返回Import "pytest" could not be resolved ,您需要以这种方式设置python路径:

"python.pythonPath": "/my/project/path/.venv/bin/python"

I tried h.我试过h。 deville fletcher's answer and it didn't seem to work for me, but I could have messed up in there too.德维尔弗莱彻的回答,它似乎对我不起作用,但我也可能在那里搞砸了。 However, in the process, I remembered that I hadn't changed my interpreter.然而,在这个过程中,我记得我没有更换我的翻译。 Once I changed the interpreter to my pipenv shell that fixed my problem.一旦我将解释器更改为我的 pipenv shell 就解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM