[英]importing the right module but getting an error in python
import sys
from subprocess import run, PIPE
import shlex
from src.detector_main import detect_main
def main():
# print command line arguments
for arg in sys.argv[1:]:
print(arg)
if __name__ == "__main__":
# main()
print(sys.argv)
这是我的主要模块。 如果您from src.detector_main import detect_main
,则应该从src/detector_main.py
导入detect_main
。
在我的detector_main.py
,我有一堆进口商品,
import ast
import os
import fpdf
import sys
from Detector.class_coupling_detector import detect_class_cohesion
from Detector.cyclomatic_complexity_detector import detect_cyclomatic_complexity
from Detector.long_lambda_detector import detect_long_lambda
from Detector.long_list_comp_detector import detect_long_list_comp
from Detector.pylint_output_detector import detect_pylint_output
from Detector.shotgun_surgery_detector import detect_shotgun_surgery
from Detector.useless_exception_detector import detect_useless_exception
# from tools.viz_generator import add_viz
def detect_main(directory):
# Get stats for files in directory
stats_dict = get_stats(directory)
....
运行我的主模块会给我这个错误:
File "pyscent.py", line 5, in <module>
from src.detector_main import detect_main
File "C:\Users\user\Desktop\proj\src\detector_main.py", line 5, in <module>
from Detector.class_coupling_detector import detect_class_cohesion
ModuleNotFoundError: No module named 'Detector'
我没有得到这个,因为我正在遵循确切的路径。
我不明白这一点,因为我遵循正确的道路。
在您的例子导入Detector.class_coupling_detector
文件模块,该模块在同一个目录探测器 ,但你的CWD 是不是 src目录。
因此,您应该from src.Detector...
使用绝对导入from src.Detector...
或者from src.Detector...
相对导入from .Detector...
以下是有关这两种导入方式之间差异的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.