如何在包中的模块中调用函数?

2024-03-29 09:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目结构如下:

root_dir/
└── app/
    ├── __init__.py
    ├── mymodule.py

在mymodule.py中,我具有以下功能:

def hello():
    print('hello world')

如果我从根目录中调用python,我希望执行以下操作:

>>> import app
>>> app.mymodule.hello()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'app' has no attribute 'mymodule'

是否仍有调用hello函数的方法


Tags: 项目py功能apphelloworldinitdef