Python 模块导入路径详解(待完善)

常见问题

使用命令行运行某个 .py 文件找不到 root 目录

比如,引入 project root 目录下的模块

from latent_rationale.beer.vocabulary import Vocabulary
from latent_rationale.beer.models.model_helpers import build_model

报错

(tf1) F:\科研\hard-kuma\interpretable_predictions-master\latent_rationale\beer>python predict.py
Traceback (most recent call last):
  File "predict.py", line 5, in <module>
    from latent_rationale.beer.vocabulary import Vocabulary
ModuleNotFoundError: No module named 'latent_rationale'

解决方法:
将 root 目录导入该文件

import sys
sys.path.append('F:\\科研\\hard-kuma\\interpretable_predictions-master')
posted @ 2021-10-19 15:07  墨莲玦  阅读(145)  评论(0)    收藏  举报