自定义导入Python3的问题

自定义导入Python3的问题,python,module,Python,Module,我正在尝试在Python3中导入我自己的文件。我的目录如下所示: /path/folder/__init__.py /path/folder/custom_module2.py /path/folder/custom_module.py /path/launcher.py 初始化: 自定义模块: import custom_module2 def custom_function: custom_module2.custom_function() print('world')

我正在尝试在Python3中导入我自己的文件。我的目录如下所示:

/path/folder/__init__.py
/path/folder/custom_module2.py
/path/folder/custom_module.py
/path/launcher.py
初始化:

自定义模块:

import custom_module2
def custom_function:
    custom_module2.custom_function()
    print('world')
自定义模块2:

def custom_function2:
    print('hello')
发射器:

import folder
custom_function()

它说没有名为custom_module的模块实际上,如果您只放置一个名为
\uuu init\uuuuy.py
文件的空文件,python会将目录视为一个包,您可以使用点符号导入它

__init__.py
custom_module2.py
custom_module.py
然后从launcher.py

from folder.custom_module import custom_function
custom_function()
init.py文件是使Python将目录视为包含包所必需的;这样做是为了防止具有公共名称(如字符串)的目录无意中隐藏稍后在模块搜索路径上出现的有效模块。在最简单的情况下,init.py可以只是一个空文件,但它也可以执行包的初始化代码,或者设置all变量,后面将介绍


来源:

这不是定义函数的方式
def custom\u函数:
这意味着运行脚本
launcher.py
仅在同一目录中执行时有效。不太理想。嗯,你到底是什么意思?你的解决方案在这方面有什么不同?试试你的方法,看看。除非您从中包含脚本的同一目录执行,否则此操作也将失败,并出现
ImportError
。我正在从不同目录启动
launcher.py
,看起来不错,还是我搞错了?
from folder.custom_module import custom_function
custom_function()