How can module determine its own path?

K

kj

How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

TIA!

kynn
 
R

Robert Kern

How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Stef Mientki

Robert said:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

cheers,
Stef
 
R

Robert Kern

Robert said:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

Modules launched from execfile() using a properly initialized namespace dict do.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Stef Mientki

Robert said:
Robert said:
On 2009-10-30 12:19 PM, kj wrote:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

Modules launched from execfile() using a properly initialized
namespace dict do.
interesting,
but how do I configure a "properly initialized namespace dict" (other
than my current namespace) ?

thanks,
Stef Mientki
 
R

Robert Kern

Stef said:
Robert said:
Robert Kern wrote:
On 2009-10-30 12:19 PM, kj wrote:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__

but for modules launched with execfile, __file__ doesn't exists.

Modules launched from execfile() using a properly initialized
namespace dict do.
interesting,
but how do I configure a "properly initialized namespace dict" (other
than my current namespace) ?

filename = '...'
ns = dict(
__file__=filename,
__name__='whatever_you_need_it_to_be',
__builtins__=__builtins__,
)
execfile(filename, ns)


If you need to access variables in the current namespace:

filename = '...'
global_ns = globals().copy()
global_ns['__file__'] = filename
global_ns['__name__'] = 'whatever_you_need_it_to_be'
local_ns = locals().copy()
execfile(filename, global_ns, local_ns)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Steven D'Aprano

Robert said:
How can a module determine the path of the file that defines it? (Note
that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

If you execute a file with execfile, it isn't a module. It's a string
read from a file being execute, which is not the same thing.

Hint: in Python 3, execfile is gone (thank goodness!). To get the same
result, you use exec(file('myfile').read()).
 
S

Steven D'Aprano

Stef Mientki wrote: ....

filename = '...'
ns = dict(
__file__=filename,
__name__='whatever_you_need_it_to_be', __builtins__=__builtins__,
)
execfile(filename, ns)


Or better still, don't use execfile at all, and just use the import
system the way it's supposed to be used. If you find yourself doing by
hand what the Python compiler normally does for you, it's a good sign
that you're probably doing it wrong.
 
D

Dave Angel

Stef said:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

cheers,
Stef
The way I read the docs, execfile() doesn't create a module, so this is
irrelevant. Effectively it adds to the current module, or to whatever
the global() and local() dictionary define.

I haven't experimented with it (as the doc says, it's used rarely), so
if I'm wrong, please correct me.

DaveA
 
G

Gabriel Genellina

Robert said:
On 2009-10-30 12:19 PM, kj wrote:
How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)

__file__
but for modules launched with execfile, __file__ doesn't exists.

Remember that execfile just executes the file contents, it does not create
a new module nor interacts with the module management.
"modules launched with execfile" has no meaning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top