Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "from-import" exec the whole module?

OK, so I know that from-import is "exactly" the same as import, except that it's obviously not because namespaces are populated differently.

My question is primarily motivated because I have a utils module which has one or two functions that are used by every other module in my app, and I'm working on incorporating the standard library logging module, which as far as I can tell I need to do sorta like this:

import logging
logging.basicConfig(filename="/var/log")  # I want file logging

baselogger = logging.getLogger("mine")
#do some customizations to baselogger

and then to use it in a different module I would import logging again:

import logging
logger = logging.getlogger("mine")

# log stuff

But what I want to know is if I do a from utils import awesome_func will my logger definitely be set up, and will the logging module be set up the way I want?

This would apply to other generic set-ups as well.

like image 688
quodlibetor Avatar asked Jun 30 '26 23:06

quodlibetor


1 Answers

Looks like like the answer is yes:

$ echo 'print "test"
def f1():
    print "f1"

def f2():
    print "f2"

' > util.py

$ echo 'from util import f1
f1()
from util import f2
f2()
' > test.py

$ python test.py 
test
f1
f2

$ 
like image 76
Steve Losh Avatar answered Jul 02 '26 13:07

Steve Losh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!