Import without executing module

python at python at
Mon Feb 2 13:51:10 CET 2009

If the output is coming from a print command, couldn't the OP
temporarily redirect STDIO to a file to prevent the output from being


----- Original message -----
From: "Stephen Hansen" <apt.shansen at>
To: "Ray" <raykyoto at>
Cc: python-list at
Date: Sun, 1 Feb 2009 23:19:09 -0800
Subject: Re: Import without executing module

On Sun, Feb 1, 2009 at 11:05 PM, Ray <raykyoto at> wrote:
> 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.

Unfortunately, that's not possible, I believe. All the top level
commands in a particular Python script are executed: that's how the
functions get created.


More information about the Python-list mailing list