本文介绍了ModuleNotFoundError: 没有名为“pytest"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在虚拟环境中安装pytest模块后,使用python代码调用运行提示找到pytest模块.

我在虚拟环境之外安装了 pytest 模块.用python正常调用就可以了.

导入pytestdef test_main():断言 5!=5如果 __name__ == "__main__":pytest.main()

错误如下:

[运行] python -u "d:\MyPytest\test_sample.py"回溯(最近一次调用最后一次):文件d:\MyPytest\test_sample.py",第 1 行,在导入pytestModuleNotFoundError: 没有名为pytest"的模块[完成] 在 0.185 秒内退出代码=1

解决方案

TLDR: 我怀疑您在系统级别 python 中安装了 pytestsite-packages 所以当你尝试在你的 virtualenv 中运行 pytest 时,它会抛出一个 ModuleNotFoundError 因为它没有安装这个依赖项你的 virtualenv.虚拟环境为您提供了一个沙盒环境,因此您可以为您的项目试验潜在的 python 库,但它们是自包含的,无法访问您的系统级 Python 第三方库.>

通常,当 import 语句无法成功导入指定模块时,会引发 ImportError.如果问题是由于无效或不正确的路径,这将引发 ModuleNotFoundError.

根据您的问题,不清楚您安装 pytest 的位置,因为您说您在 virtualenv 中安装了它,然后您说您安装了它在您的系统级 python 站点包上的 virtualenv 之外.所以我将给出我的想法,让 pytest 中工作>virtualenv,因为这可能是您想要的:

Virtualenv 很好,因为它们为您提供了一个沙盒环境来使用 python 库,可以避免弄乱您的系统级 python 配置.现在 ModuleNotFoundError 在您的 virtualenv 中抛出,因为它找不到您尝试运行的测试的 pytest 模块.也许您可以尝试激活您的 virtualenv 并在此 virtualenv 中重新安装 pytest 并查看如果此操作方案解决了您的问题:

激活您的虚拟环境:

# Posix 系统源/路径/到/ENV/bin/activate# 窗户\path\to\env\Scripts\activate

在您的 virtualenv 中安装 pytest:

注意:在安装 pytest 之前,您应该会看到括号中列出的 virtualenv 名称.对于此示例,假设您创建了一个名为:env

的虚拟环境

(env) pip install pytest

现在 pytest 将在您的 virtualenv 中可用.有关更多信息,请查看 virtualenv文档.我还建议查看 virtualenvwrapper,,它很好地包裹了 virtualenv用于激活/停用 virtualenvs 的更方便的命令.

希望有帮助!

After installing the pytest module in a virtual environment, I used the python code to call and run the prompt to find the pytest module.

I installed the pytest module outside the virtual environment. I can call it normally with python.

import pytest


def test_main():
    assert 5!=5

if __name__ == "__main__":
    pytest.main()

The error is as follows:

解决方案

TLDR: I suspect you installed pytest within your system level python site-packages so when you try to run pytest, within your virtualenv, it's throwing a ModuleNotFoundError since it doesn't have this dependency installed within your virtualenv. Virtual environments give you a sandboxed environment so you can experiment with potential python libraries for your project, but they're self contained and don't have access to your system level python third-party libraries.

Typically an ImportError is raised when an import statement has trouble successfully importing the specified module. If the problem is due to an invalid or incorrect path, this will raise a ModuleNotFoundError.

From your question it isn't clear where you installed pytest since you said you installed it within your virtualenv then you said you installed it outside your virtualenv on your System level python site-packages.. So I will give my thoughts for getting pytest to work within a virtualenv, since this is probably what you want:

Virtualenv are nice because they give you a sandboxed environment to play around with python libraries, safe from messing up your system level python configurations. Now the ModuleNotFoundError is thrown within your virtualenv because it can't find the pytest module for the test you're trying to run. Maybe you could try activating your virtualenv and re-installing pytest within this virtualenv and seeing if this course of action resolves your issue:

Activate your virtualenv:

# Posix systems
source /path/to/ENV/bin/activate

# Windows
\path\to\env\Scripts\activate

Install pytest within your virtualenv:

Note: you should see your virtualenv's name listed in parenthesizes before installing pytest. For this example, suppose you created a virtual environment named: env

(env) pip install pytest

Now pytest will be available to you within your virtualenv. For more information checkout virtualenv's documentation. I would also suggest looking into virtualenvwrapper, which nicely wraps around virtualenv for more convenient commands to activate/deactivate virtualenvs.

Hopefully that helps!

这篇关于ModuleNotFoundError: 没有名为“pytest"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:15