惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
D
Docker
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
H
Help Net Security
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
博客园 - 司徒正美
C
Check Point Blog
B
Blog
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
F
Fortinet All Blogs
美团技术团队
D
DataBreaches.Net

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Python system PATH issues When We Use Pytest
2025-05-14 · via jdhao's digital space

When I try to run pytest locally for a Python project, I noticed that the import in those test scripts may fail if I run pytest in different directories. The import error usually means that your actual code modules is not in the python path, so they can not be imported successfully by your test scripts.

This has something to do with how pytest set up the python path when you run it.

change the python path#

You can add other path to the sys.path by setting the pythonpath option. Multiple config file formats are supported. Here is how to do it in pyproject.toml:

[tool.pytest.ini_options]
pythonpath = ["/path/to/project/root"]

After you set it up correctly, the import error should be gone.

Note that you can not change sys.path with --rootdir option, the --rootdir is for other things like __pytest_cache directory. This is clearly documented in pytest official doc

using python -m pytest#

This will add the current directory to sys.path, that is why the test scripts under unit_tests folder can import modules in the parent directory.

contest.py file#

Create an empty conftest.py in the parent directory also works. See also https://github.com/pytest-dev/pytest/issues/4699 This may be a hacky way, but it should work.

should we add __init__.py to tests directory?#

Suppose the tests folder is in the same level as the source code, and there is no __init__.py inside tests folder,

.
├── app/
│   ├── foo/
│   │   └── foo.py
│   ├── baz.py
│   └── bar/
└── tests/
    ├── unit_tests/
    └── test_foo.py

Pytest treat the test module under tests directory as standalone modules. When pytest run the test cases in a test module, it will insert the parent directory of this test module to sys.path. For example, when it runs test_foo.py, <project-root>/tests is inserted to sys.path. For it runs tests/unit_tests/test_bar.py, <project-root>/tests/unit_tests will be inserted to sys.path.

In this case, pytest use the base name of the test module as the module name (you can check this in the sys.modules table). So you can not have test modules with the same name under tests directory, even if they are in different directory.

If you have __init__.py like below, pytest will treat these as packages, and find the project root accordingly.

└── tests/
    ├── __init__.py
    ├── unit_tests/
    │   └── __init__.py
    └── test_foo.py

In this particular case, the project root will be the directory containing both tests and app directory. The full module name for test_foo.py will be tests.test_foo instead.

This is also explained in the official doc on standalone test modules.

References#