gpt4 book ai didi

python tox - 没有这样的文件或目录错误

转载 作者:行者123 更新时间:2023-11-30 22:35:11 26 4
gpt4 key购买 nike

我有一个项目,假设名为 'my_project'具有以下结构:

my_project
|
tox.ini
setup.py
src
|
client.py
server.py
config.json
# other files
tests.py

文件client.py中定义的类和server.py使用 config.json 中包含的信息和tests.py实现py.test测试文件夹 src 中的每个文件。在客户端/服务器文件中,我读取配置文件如下:

with open('./config.json') as infile:
self.parameters = json.load(infile)

接下来,我创建了一个 tox.ini 文件,如下所示:

[tox]
envlist = py27,cov

[testenv]
#commands = py.test --cov=src/client.py src/tests.py -v
commands = py.test -sv --doctest-modules src/__init__.py src/tests.py


[testenv:py27]
commands = coverage erase
coverage run --source=src -m pytest
coverage report -m
deps = pytest

但是当我运行tox时命令我收到错误:"No such file or directory: 'config.json'" 。如何设置正确的路径以确保 tox能找到所有需要的文件吗?

最佳答案

正在尝试打开'./config.json'总是让 Python 在进程的当前目录中查找;这就是文件路径的工作原理。如果你想打开config.json文件与 .py 位于同一目录中正在打开的文件,您需要包含包含 .py 的目录的路径文件:

with open(os.path.join(os.path.dirname(__file__), 'config.json')) as infile:

关于python tox - 没有这样的文件或目录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com