简体   繁体   中英

How do you invoke a function in a module which is in a package?

I have a project structure of:

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

inside of mymodule.py I have the following function:

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

If i call python from inside the root directory I'd like to do the following:

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

Is there anyway to invoke the hello function?

jonrsharpe comment is right. When you reference a package you are referring to the __init__.py module and this module doesn't contain any reference to mymodule . That's why you cannot do app.mymodule in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM