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

推荐订阅源

A
About on SuperTechFans
G
Google Developers Blog
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
小众软件
小众软件
月光博客
月光博客
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
The Cloudflare Blog
博客园_首页
美团技术团队
大猫的无限游戏
大猫的无限游戏
B
Blog
IT之家
IT之家
Jina AI
Jina AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

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#