Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing an attribute
#5
This is how they have made the there package.
There are several way to make a package,they could have lifted sub-modules so all was available under import botocore.
To give a example.
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.
Can say the last version is more user friendly,if try to figure out a package without looking at doc.
But if they have in doc that most use import botocore.session,then it's kind of okay.

If i make package i try to lift sub-modules so all is available under main import,
this make import shorter and easier for user of package to figure out how import work.
Reply


Messages In This Thread
importing an attribute - by Skaperen - Apr-14-2019, 03:06 AM
RE: importing an attribute - by woooee - Apr-14-2019, 05:13 AM
RE: importing an attribute - by Skaperen - Apr-14-2019, 05:48 AM
RE: importing an attribute - by Gribouillis - Apr-14-2019, 06:32 AM
RE: importing an attribute - by snippsat - Apr-14-2019, 08:17 AM
RE: importing an attribute - by Gribouillis - Apr-14-2019, 10:48 AM
RE: importing an attribute - by Skaperen - Apr-14-2019, 01:55 PM
RE: importing an attribute - by snippsat - Apr-14-2019, 03:54 PM

Forum Jump:

User Panel Messages

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