未找到设置文件参考

Settings file reference is not found

我正在努力将现有的 Python 2.7 项目迁移到 Python 3.9。我在 Python 3.

中遇到与目录结构相关的问题

我当前的项目目录结构是:

├───project
│   ├───core
|       +--__init__.py
|       +--main.py
|       +--settings.py
│   ├───jobs
|       +--job.py

main.py:

import settings


class Main:
    def __init__(self, a=None, b=settings.B):
        self.a = a
        self.b = b

    def start(self):
        print(self.a, self.b)

job.py:

import sys
# sys.path.insert(0, '../core/')
from core.main import Main
from core import settings

main = Main(settings.A)
main.start()

使用Python 2.7 解释器时,此结构没有问题,但在Python 3.9 中执行job.py 时,我看到以下错误:

  File "project\core\main.py", line 1, in <module>
    import settings
ModuleNotFoundError: No module named 'settings'

通过取消注释 job.py 脚本第 2 行的代码可以解决此问题,但我想避免像那样对文件夹值进行硬编码。如果有人可以提供替代方法并解释为什么它在较新的 Python 版本中以这种方式运行,我将不胜感激。

由于这种用例的歧义,在 python3 中删除了绝对导入。 (这个解释的很好:Changes in import statement python3

您可以对这个用例使用相对导入 - https://docs.python.org/3/reference/import.html#package-relative-imports