Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module attributes
#2
It's the way they have build the package.
botocore is a folder that contain session.py and a blank __init__.py.
botocore/
 |-- __init__.py
 |-- session.py
# session.py
def egg():
    print('i am foo')
Usage:
λ ptpython
>>> import botocore

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

>>> # Have to use 
>>> import botocore.session

>>> botocore.session.egg()
i am foo
There is a fix for this bye modifying the blank import in __init__.py .
# __init__.py
from .session import egg
Usage:
λ ptpython
>>> import botocore

>>> botocore.session.egg()
i am foo

>>> # Now will also work without session,also we can call egg() function in session.py directly 
>>> botocore.egg()
i am foo
When build a package have to think of stuff like this.
The last version is for me much more user friendly.

Can of course have in doc that most use import botocore.session.bar.somthing,
i do not like import like this Dodgy
The way around this when build own package is to modify __init__.py(not all in the subfolders but one in main package folder) or also look at __all__ to severe stuff easier to users.
Reply


Messages In This Thread
module attributes - by Skaperen - Jul-14-2018, 12:53 AM
RE: module attributes - by snippsat - Jul-14-2018, 02:01 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020