My current understanding (based on these answers: one, two, three; and Python documentation) of how import in Python works is (just in case it matters: all the code snippets are tested on Python 3.6.1):
Say we have a module mod, which has submodules sub and sub1; sub, in turn, has a function func; then we can (given that mod installed in current environment, of course):
import mod
mod.sub.func()
mod.sub1
# or
import mod.sub
mod.sub.func()
mod.sub1 # will result in "NameError: name 'mod' is not defined"
# or
from mod.sub import func
func()
mod.sub.func() # will result in "NameError: name 'mod' is not defined"
mod.sub1 # will result in "NameError: name 'mod' is not defined"
Recently, while playing with werkzeug.security.generate_password_hash and werkzeug.security.check_password_hash, in Python console, I have noticed that:
import werkzeug
werkzeug.security.generate_password_hash('some_password', method='pbkdf2:sha512', salt_length=25)
results in AttributeError: module 'werkzeug' has no attribute 'security'.
Though, the following works fine:
from werkzeug import security
security.generate_password_hash('some_password', method='pbkdf2:sha512', salt_length=25)
this (of course) too:
import werkzeug.security
werkzeug.security.generate_password_hash('some_password', method='pbkdf2:sha512', salt_length=25)
as well as this:
from werkzeug.security import generate_password_hash
generate_password_hash('some_password', method='pbkdf2:sha512', salt_length=25)
and, a bit surprisingly (at least for me), this one:
import werkzeug
from werkzeug import security
werkzeug.security.generate_password_hash('some_password', method='pbkdf2:sha512', salt_length=25)
My questions are:
- Am I wrong (or lacking details) in some of my notions, concerning how
importworks in Python? - Why
import werkzeugwon't give me access towerkzeug.security? My understanding is — it should importwerkzeug, along with all of it's submodules/attributes. - Why
import werkzeug+from werkzeug import securityallows access towerkzeug.security? My understanding: it should bind two separate names (with no connections between them), as follows:werkzeugtoimport werkzeug(i.e.werkzeugmodule) andsecuritytofrom werkzeug import security(i.e.securitysubmodule ofwerkzeugmodule.