2021年7月14日 星期三

Python __init__.py Failed to experiment

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



A file mydir/caller1.ok.py will be ok

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


沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...