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


沒有留言:

張貼留言

202501 Debian USB LAN Card Bridge

 202501 Debian USB LAN Card Bridge ChatGPT Question I have a machine running debian 12 with a LAN port using a cable to connect to my office...