Import without executing module
raykyoto at gmail.com
Mon Feb 2 02:05:50 EST 2009
I'm quite new to python as I've only just started to learn about it a
few days ago... I am trying to do something and after reading about
it, I'm still not sure whether or not it can be done.
Basically, someone has created a python script and I would like to
make use of his functions. I would prefer to not modify his file so
what I would like to do is just write my script and import parts that
are needed. i.e., I would like to separate my changes from his as
best as I can. However, that other module has both functions (def's,
which I would like to use) and top-level commands which I don't need
and in fact, prints errors when I import it since it was meant to be
run as a top-level module and not imported in. i.e., its expecting
arguments to be supplied.
For example, suppose I have a module test.py:
from test2 import foo
print "test says hello"
print "foo is being executed"
print "test2 says hello"
and I would like to run "python test.py" without "test2 says hello"
appearing. I figured "import test2" would not work, so I thought that
maybe "from" would only take the functions listed, but no. In the
end, it is just importing all of "test2" and then renaming it: foo =
This isn't a very serious problem as I obviously have the source and
can just copy the functions and attribute the original author (that's
ok). But, it would be nice if I can make use of those functions
without touching that file [test2.py in the above example].
Is this possible?
More information about the Python-list