Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing an attribute
#1
when i do:
import botocore.session
my_session = botocore.session.get_session()
it works OK.

but if i do:
import botocore
my_session = botocore.session.get_session()
then it fails, saying that botocore has no attribute named session.

i am curious what is going on here. the reference manual (for 3.5) does not say what is going on although it does show a couple examples with attribute names on import statements, without explaining it.

how can i make my module do the same thing? why would i (or anyone) want to? or why should i not do this?

is there a way to get botocore.session if only botocore was imported, since the list of attributes does not have session in the latter case?

Output:
lt2a/forums /home/forums 1> py3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import botocore.session >>> 'session' in dir(botocore) True >>> lt2a/forums /home/forums 2> py3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import botocore >>> 'session' in dir(botocore) False >>> lt2a/forums /home/forums 3>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Is botocore.session a class, or function, or variable. No one has a crystal ball to tell them what you are trying to import.
Reply
#3
but what Python "mechanism" is involved that makes the attribute unavailable until it is imported. how do i make my own module do that?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Skaperen Wrote:what Python "mechanism" is involved that makes the attribute unavailable until it is imported.
This is what happens when 'session' is a submodule of a package 'botocore'. You only need a file structure like this
Output:
botocore/ __init__.py session.py
If there is no import session in __init__.py, then module botocore has no 'session' attribute but one can import botocore.session. You can also have a subpackage of course
Output:
botocore/ __init__.py session/ __init__.py
Reply
#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
#6
snippsat Wrote:Can say the last version is more user friendly,if try to figure out a package without looking at doc.
You can use pkgutil for this, the following code should work
import pkgutil
import botocore
print([t[1] for t in pkgutil.iter_modules(botocore.__path__)])
Actually, I already wrote a script for this task in this forum list_subpackages.py
With this you only need to call on the commandline
Output:
list_subpackages.py botocore
It also lists subsubpackages recursively.
Reply
#7
list_subpackages.py is interesting. i will have to run that on many things. i ran it on boto3, too. it missed some things that may just be mere attributes. it is showing me where things like botocore are amiss in documentation ... all they did with "botocore.session" was show it as an example (but at least one that worked).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Apr-14-2019, 10:48 AM)Gribouillis Wrote: Actually, I already wrote a script for this task in this forum list_subpackages.py
Nice script Thumbs Up
Now do i usually figure out how imports of package works reasonably fast,have used pdir2 sometime which give a nice overview.
ptpython which show Autocompletion of attributes without eg using Tab key.
Reply


Forum Jump:

User Panel Messages

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