0

从终端运行 Python 代码时出现模块未找到错误。

我的项目结构:

Project
../py_configs
  __init__.py
  conf.py
../src
  check.py

自动将模块路径添加到 PATH:

py_configs/__init__.py

import os
import sys

path = os.path.join((os.path.dirname(__file__))).replace('\\', '/')
sys.path.extend([path.replace("/","\\"),path])

src/check.py

from py_configs import conf

错误

Terminal - src directorty: python -m check
Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Continuum\anaconda3\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\admin\AppData\Local\Continuum\anaconda3\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\admin\PycharmProjects\ExperimentProject\src\check.py", line 1, in <module>
    from py_configs import conf
ModuleNotFoundError: No module named 'py_configs'
4

0 回答 0