detector_卖弄.朋友
import ast
import os
import fpdf
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
import sys
def main(directory):
# Get stats for files in directory
stats_dict = get_stats(directory)
...
我在这个文件中有一堆导入。当我运行此文件时,(参数是目录的字符串路径),我收到以下错误
File "C:\Users\user\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/user/Desktop/project/src/detector_main.py", line 12
from ../tools.viz_generator import add_viz
我的项目结构如下所示:
我觉得这些输入不一致,可能更有条理。我只是写了一堆脚本并且很重要,但我觉得有一种方法可以将它变成一个更加一致的包,用户可以在任何地方运行导入问题。
有帮助吗?
听起来你正在寻找的是Python包。
https://www.pythoncentral.io/how-to-create-a-python-package/
使用Python包非常简单。你需要做的就是:
创建一个目录并为其提供包名称。把你的课程放进去。在所有目录中创建一个init.py文件!为了创建Python包,它非常容易。 init.py文件是必需的,因为使用此文件,Python将知道此目录是普通目录(或文件夹 - 无论您想要调用它)的Python包目录。无论如何,我们将在这个文件中编写一些import语句来从我们的全新包中导入类。
我做的是把我的包裹放进去
PYTHON_PATH \ LIB \站点包\ mypackage中
然后在__init__.py
from .function_scripts import *