1
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)

This is my main module. If you see the from src.detector_main import detect_main, it is supposed to import detect_main from src/detector_main.py.

In my detector_main.py, I have a bunch of imports,

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)
    ....

Running my main module gives me this error:

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'

I don't get this because I am following the exact path.

enter image description here

I don't get this because I am following the right path.

0

1 Answer 1

2

In your example you import the Detector.class_coupling_detector module in file that is in the same directory as Detector but your cwd is not the src directory.

Due to this you should either use absolute import from src.Detector... or relative import from .Detector...

Here is some information about difference between this two ways to import

Sign up to request clarification or add additional context in comments.

3 Comments

using the relative import .Detector... fixed that issue, but now i am getting this error: ModuleNotFoundError: No module named '__main__.src'; '__main__' is not a package
You must add a __init__.py (possibly empty) at the root of each package, here in src/ and probably in Detector/
@Dawn17 you should add a __init__.py file to the root package(the directory above src). It can be empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.