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


沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...