By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,640 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,640 IT Pros & Developers. It's quick & easy.

importing part of a module without executing the rest

P: n/a
file1.py
----------
a = 20
from abc import *
print "Should this be printed when 'a' is alone imported from this
module"

file2.py
----------
from file1 import a
print a

file2.py is used in a context where 'from abc import *' statement
doesn't make sense but it can make sense of (and requires) 'a'.
But it seems we can't import just a part of the module and expect the
rest to not be executed.
Executing python file2.py executes the 'from abc ...' and the print
statement following it in file1.py.

Can't I just import 'a' into this name scope without touching the rest
of the statements in the module (file1 in this case). If so, what the
rationale behind such logic in the module 'import' mechanism of
python?
Of course, the option of using if __name__ == '__main__' in file1.py
for statements following 'a = 20' is not useful in my case as I need
all statements in file1 to be exectued when imported by someone else
(say file3.py)

Don't I have any solution other than packaging those parts in file1.py
(a = 20 and the rest of the statements) into different modules?
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
On Jun 13, 5:52 pm, krishna.00...@gmail.com wrote:
file1.py
----------
a = 20
from abc import *
print "Should this be printed when 'a' is alone imported from this
module"

file2.py
----------
from file1 import a
print a

(snipped)

Of course, the option of using if __name__ == '__main__' in file1.py
for statements following 'a = 20' is not useful in my case as I need
all statements in file1 to be exectued when imported by someone else
(say file3.py)
That's a bad code smell. In general, modules intended to be imported
should not have any side effects at global scope before the "if
__name__ == '__main__':" part. Put the "print ..." and all other
statements with side effects in one or more functions and let the
importing module call them explicitly if necessary.

George
Jun 27 '08 #2

P: n/a
kr***********@gmail.com writes:
file1.py
----------
a = 20
from abc import *
print "Should this be printed when 'a' is alone imported from this
module"

file2.py
----------
from file1 import a
print a

file2.py is used in a context where 'from abc import *' statement
doesn't make sense but it can make sense of (and requires) 'a'.
But it seems we can't import just a part of the module and expect the
rest to not be executed.
Executing python file2.py executes the 'from abc ...' and the print
statement following it in file1.py.
That's right. That's what 'import' is documented to do.

Import statements are executed in two steps: (1) find a module,
and initialize it if necessary; (2) define a name or names in the
local namespace …

… to ``initialize'' a Python-coded module meansto execute the
module's body.

<URL:http://docs.python.org/ref/import.html>
Can't I just import 'a' into this name scope without touching the
rest of the statements in the module (file1 in this case). If so,
what the rationale behind such logic in the module 'import'
mechanism of python?
The purpose of dividing code into separate modules is to have all the
code in those modules executed by an 'import' statement. If you want
some code to be executed under some circumstances and other code not
executed, they need to be separated somehow.
Don't I have any solution other than packaging those parts in
file1.py (a = 20 and the rest of the statements) into different
modules?
Let me turn that around: What are you actually trying to do, and how
does that conflict with separating code that seems (from your
description) conceptually separate anyway?

One possible answer could be to define a module-level function. This
would mean that at 'import' time, the 'def' statement is executed and
the function defined, but the code inside it is not executed until you
call the function at some later time. This also has the beneficial
effect of defining a specific interface for using that code.

Whether that's appropriate to your situation is impossible to say
without knowing what you are trying to do.

--
\ “Never do anything against conscience even if the state |
`\ demands it.” —Albert Einstein |
_o__) |
Ben Finney
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.