在 vagrant ubuntu vm 上运行 pytest,Windows 作为主机.导入文件不匹配



我想这是一个非常特殊的情况,但我会提出这个问题,因为我没有找到相关的答案,可能对其他人有用。

情况是这样的:

  • Windows是主机操作系统。
  • 有一个Ubuntu VirtualBox(用vagrant创建)。

  • 我的 django 项目文件夹在 Windows 和 ubuntu 之间共享。

  • 我在 ubuntu 和 我在Windows上的虚拟环境。

  • 如果我从 windows virtualenv 运行pytest命令,测试将运行 刚刚好
  • 但是在 ubuntu vm 终端中,我不能只运行pytest命令 因为我收到错误:程序"pytest"当前不是 安装。
  • 我可以用python -m pytest运行它.但在这种情况下,我得到import file mismatch错误:

=========================错误===

========================
_____________ ERROR collecting my_django_app/tests.py ____________
import file mismatch:
imported module 'my_django_app.tests' has this **__file__** attribute:
C:virtualEnvsmy_envproject_srcmy_django_apptests.py
which is not the same as the test file we want to collect:
/vagrant/projects/project_src/my_django_app/tests.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

信息非常明确,但我不知道如何克服这个问题。

我使用 python 2.7.9,没有 pycache 文件夹(也没有 ant .pyc编译的文件)。

我只需要删除缓存的编译python文件。这些文件位于 django 项目文件夹内的__pycache__文件夹中。每个 django 应用程序也有一个pycache文件夹。

如果在 Windows 主机中运行 pytest,则缓存的编译文件包含特定于 windows 的路径,并尝试从那里导入模块。因此,必须删除缓存的文件。然后,您可以使用以下命令在 ubuntu VM 中运行 pytest:python -m pytest

您可以通过运行带有 -B 选项的 python 解释器来避免创建编译文件:python -B pytest

最新更新