Consider mydir/spam/module.py
import os
def rprint():
print(os.path.dirname(os.path.abspath(__file__)))
print(__file__)
if __name__=='__main__':
rprint()
$ python3 module.py
/home/ricky/d/lab/w3c/24.Python/01.init.py/mydir/spam
module.py
import spam.module
spam.module.rprint()
A file mydir/caller2.ok.py will be ok
from spam import module
module.rprint()
But a file mydir/caller3.error.py gives an error
import spam
spam.rprint()
$ python3 caller3.fail.py
Traceback (most recent call last):
File "caller3.fail.py", line 2, in <module>
spam.rprint()
AttributeError: module 'spam' has no attribute 'rprint'
Failed to experiment __init__.py
End
沒有留言:
張貼留言