在python 3.7中无法将文件作为模块导入。

问题描述 投票:0回答:2
python -V
Python 3.7.6

which python
python: aliased to python3.7

which python3.7
/path/to/virtualenvfolder/bin/python3.7

文件夹结构

src
 |-- __init__.py
 |-- migrations
     |-- env.py

env.py

from config import DB, DB_HOST, DB_USER, DB_PASS, DB_PORT, DB_DRIVER

错误

File "src/migrations/env.py", line 13, in <module>
    from config import DB, DB_HOST, DB_USER, DB_PASS, DB_PORT, DB_DRIVER
ModuleNotFoundError: No module named 'config' 

但如果改成

from src.config import DB, DB_HOST, DB_USER, DB_PASS, DB_PORT, DB_DRIVER

-- __init__.py

from config import DB, DB_HOST, DB_USER, DB_PASS, DB_PORT, DB_DRIVER

它的工作原理。

python virtualenv
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.