如何修复“没有名为‘app_one’的模块" [英] How to fix "no module named 'app_one'"
本文介绍了如何修复“没有名为‘app_one’的模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有以下结构的 Python 包.
I have a Python package with the following structure.
>python_package # package root directory
>app_one # subpackage directory
>__init__.py
>views.py
>app_two # another subpackage directory
>__init__.py
>views.py
app_one/views.py
的代码:
def show():
print('do something')
app_two/views.py
的代码:
from app_one.views import show
show()
问题是,每当我尝试从终端运行 app_two
的 views.py
时,我都会收到错误
The problem is, whenever I try to run views.py
of app_two
from the terminal, I get an error
没有名为app_one"的模块
No module named 'app_one'
但是当我在 PyCharm IDE 中打开包 python_package
时,我没有遇到任何问题,一切正常.
But when I open the package python_package
in the PyCharm IDE, I'm getting no issue, everything works perfectly.
推荐答案
出现这个错误的原因是,app_one
文件的路径不在当前路径中,需要添加到路径使用 sys.path.append
试试:
This error occurs because, the path to the file app_one
is not in the current path, and you have to add it to the path using sys.path.append
Try :
import sys
sys.path.append('./app_one')
from views import show
show()
这篇关于如何修复“没有名为‘app_one’的模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文