运行 pytest on vagrant ubuntu vm with windows 作为主机。导入文件不匹配
Run pytest on vagrant ubuntu vm with windows as host. Import file mismatch
我想这是一个非常具体的情况,但我会提出这个问题,因为我没有找到相关的答案并且可能对其他人有用。
情况是这样的:
- Windows 是主机 OS.
有一个 Ubuntu VirtualBox(用 vagrant 创建)。
我的 django 项目文件夹在 windows 和 ubuntu 之间共享。
我在 ubuntu 和 ubuntu 上安装了 pytest(通过 pytest-django)
我的 virtualenv windows.
- 如果我 运行 来自 windows virtualenv 的
pytest
命令测试 运行
还好
- 但是在 ubuntu 虚拟机终端中我不能只 运行
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:\virtualEnvs\my_env\project_src\my_django_app\tests.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
我想这是一个非常具体的情况,但我会提出这个问题,因为我没有找到相关的答案并且可能对其他人有用。
情况是这样的:
- Windows 是主机 OS.
有一个 Ubuntu VirtualBox(用 vagrant 创建)。
我的 django 项目文件夹在 windows 和 ubuntu 之间共享。
我在 ubuntu 和 ubuntu 上安装了 pytest(通过 pytest-django) 我的 virtualenv windows.
- 如果我 运行 来自 windows virtualenv 的
pytest
命令测试 运行 还好 - 但是在 ubuntu 虚拟机终端中我不能只 运行
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:\virtualEnvs\my_env\project_src\my_django_app\tests.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