【发布时间】:2021-03-13 23:49:35
【问题描述】:
我有一个基本设置如下的 python 项目:
imptest.py
utils/something.py
utils/other.py
以下是脚本中的内容:
imptest.py
>#!./venv/bin/python
import utils.something as something
import utils.other as other
def main():
"""
Main function.
"""
something.do_something()
other.do_other()
if __name__ == "__main__":
main()
something.py
>#import other
def do_something():
print("I am doing something")
def main():
"""
Main function
"""
do_something()
#other.do_other()
if __name__ == "__main__":
main()
其他.py
>def do_other():
print("do other thing!")
def main():
"""
Main function
"""
do_other()
if __name__ == "__main__":
main()
imptest.py 是主文件,它偶尔会运行并调用 utils 函数。
如您所见,我在“something.py”中注释掉了一些行,我正在导入“其他”模块进行测试。
但是当我想测试something.py中的某些功能时,我必须运行文件something.py并取消对导入行的注释。
这感觉有点笨拙。
如果我离开了
import other
取消注释并运行 imptest.py,我得到这个错误:
Traceback (most recent call last):
File "imptest.py", line 5, in <module>
import utils.something as something
File "...../projects/imptest/utils/something.py", line 3, in <module>
import other
ModuleNotFoundError: No module named 'other'
有什么更好的方法?
【问题讨论】:
-
测试你的 sn-ps。这些扩展会搞砸事情
-
您介意发布minimal, reproducible example 而不是描述假设案例吗?
-
“一切正常。”:在这种情况下,粘贴您的实际代码
-
嘿,我添加了更多上下文/代码。请看看这是否更有用。
标签: python python-3.x modulenotfounderror