运行测试时,Python导入src模块 [英] Python import src modules when running tests

查看:211
本文介绍了运行测试时,Python导入src模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源文件位于src下,而我的测试文件位于受测试下.当我想运行测试文件时,例如说python myTest.py,我收到一个导入错误:没有名为ASourceModule.py的模块".

My source files are located under src and my test files are located under tests. When I want to run a test file, say python myTest.py, I get an import error: "No module named ASourceModule.py".

如何从运行测试所需的源中导入所有模块?

How do I import all the modules from source needed to run my tests?

推荐答案

您需要将该目录添加到路径:

You need to add that directory to the path:

import sys
sys.path.append('../src')

如果您经常使用它,可以将其放入模块中.

Maybe put this into a module if you are using it a lot.

这篇关于运行测试时,Python导入src模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆