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

查看:30
本文介绍了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天全站免登陆