Stef Mientki | 1 Jun 2009 23:28
Picon

how to get the path of a module (myself) ?

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?

thanks,
Stef Mientki
--

-- 
http://mail.python.org/mailman/listinfo/python-list

MRAB | 2 Jun 2009 00:37

Re: how to get the path of a module (myself) ?

Stef Mientki wrote:
> hello,
> 
> I've pictures stored in a path relative to my python source code.
> To get a picture, I need to know what path I'm on in each python module.
> I thought __file__ would do the job,
> but apparently I didn't read the documentation carefully enough,
> because file is the path to the module that called my module.
> 
> Any ways to get the path of "myself" ?
> 
I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py
--

-- 
http://mail.python.org/mailman/listinfo/python-list
(Continue reading)

Stef Mientki | 2 Jun 2009 01:46
Picon

Re: how to get the path of a module (myself) ?

MRAB wrote:
> Stef Mientki wrote:
>> hello,
>>
>> I've pictures stored in a path relative to my python source code.
>> To get a picture, I need to know what path I'm on in each python module.
>> I thought __file__ would do the job,
>> but apparently I didn't read the documentation carefully enough,
>> because file is the path to the module that called my module.
>>
>> Any ways to get the path of "myself" ?
>>
> I'm not sure what you mean. I just did a quick test.
>
> # File: C:\Quick test\child.py
> print "name is %s" % __name__
> print "file is %s" % __file__
>
> # File: C:\Quick test\parent.py
> import child
>
> print "name is %s" % __name__
> print "file is %s" % __file__
>
> # Output:
> name is child
> file is C:\Quick test\child.py
> name is __main__
> file is C:\Quick test\parent.py
Yes, that's what I (and many others) thought,
(Continue reading)

MRAB | 2 Jun 2009 02:14

Re: how to get the path of a module (myself) ?

Stef Mientki wrote:
> MRAB wrote:
>> Stef Mientki wrote:
>>> hello,
>>>
>>> I've pictures stored in a path relative to my python source code.
>>> To get a picture, I need to know what path I'm on in each python module.
>>> I thought __file__ would do the job,
>>> but apparently I didn't read the documentation carefully enough,
>>> because file is the path to the module that called my module.
>>>
>>> Any ways to get the path of "myself" ?
>>>
>> I'm not sure what you mean. I just did a quick test.
>>
>> # File: C:\Quick test\child.py
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # File: C:\Quick test\parent.py
>> import child
>>
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # Output:
>> name is child
>> file is C:\Quick test\child.py
>> name is __main__
>> file is C:\Quick test\parent.py
(Continue reading)

Dave Angel | 2 Jun 2009 02:10
Picon

Re: how to get the path of a module (myself) ?

Stef Mientki wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">MRAB wrote:
>> Stef Mientki wrote:
>>> hello,
>>>
>>> I've pictures stored in a path relative to my python source code.
>>> To get a picture, I need to know what path I'm on in each python 
>>> module.
>>> I thought __file__ would do the job,
>>> but apparently I didn't read the documentation carefully enough,
>>> because file is the path to the module that called my module.
>>>
>>> Any ways to get the path of "myself" ?
>>>
>> I'm not sure what you mean. I just did a quick test.
>>
>> # File: C:\Quick test\child.py
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # File: C:\Quick test\parent.py
>> import child
>>
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # Output:
>> name is child
>> file is C:\Quick test\child.py
>> name is __main__
(Continue reading)

David Lyon | 2 Jun 2009 00:23

Re: how to get the path of a module (myself) ?

On Mon, 01 Jun 2009 23:28:16 +0200, Stef Mientki <stef.mientki <at> gmail.com>
wrote:
> hello,
> 
> I've pictures stored in a path relative to my python source code.
> To get a picture, I need to know what path I'm on in each python module.
> I thought __file__ would do the job,
> but apparently I didn't read the documentation carefully enough,
> because file is the path to the module that called my module.
> 
> Any ways to get the path of "myself" ?

This ain't the official way... but the hackers way.....

Check site.path (import site)...

If your module got loaded, and it's own succinct directory or .egg, then
it will have been added to site.path. 

You might have to parse the values in site.path but we're only talking
a few lines of code because you already know the package name.

If not, there's another way through pkg_utils...

Regards

David

--

-- 
http://mail.python.org/mailman/listinfo/python-list
(Continue reading)

Stef Mientki | 2 Jun 2009 01:46
Picon

Re: how to get the path of a module (myself) ?

Thanks David,
but ....

David Lyon wrote:
> On Mon, 01 Jun 2009 23:28:16 +0200, Stef Mientki <stef.mientki <at> gmail.com>
> wrote:
>   
>> hello,
>>
>> I've pictures stored in a path relative to my python source code.
>> To get a picture, I need to know what path I'm on in each python module.
>> I thought __file__ would do the job,
>> but apparently I didn't read the documentation carefully enough,
>> because file is the path to the module that called my module.
>>
>> Any ways to get the path of "myself" ?
>>     
>
> This ain't the official way... but the hackers way.....
>
> Check site.path (import site)...
>   
always return None in my case
> If your module got loaded, and it's own succinct directory or .egg, then
> it will have been added to site.path. 
>
> You might have to parse the values in site.path but we're only talking
> a few lines of code because you already know the package name.
>
> If not, there's another way through pkg_utils...
(Continue reading)

jh | 15 Jun 2009 17:33
Picon
Favicon

Re: how to get the path of a module (myself) ?

Stef Mientki <stef.mientki <at> gmail.com> writes:
> I don't seem to have pkg_utils,
> only pkgutil, which doesn't have an apropiate function.
> 
> But I found another way, don't know if that's reliable:
> import inspect
> print inspect.currentframe().f_code.co_filenameE

Eeek.

> head -999 foo/*
==> foo/__init__.py <==
import pkg_resources
print pkg_resources.resource_string(__name__, "bar")

==> foo/bar <==
bar

> python -c "import foo"
bar

Make a "def load_resource" from that print line and you're done.

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Kevin Yuan | 20 Dec 2005 13:15
Picon

How to get the path of current running python script?

I tried the following
 
>>>> getFilePath = lambda name: os.path.normpath('%s\\%s' % (sys.modules[name].prefix, sys.modules[name].__name__))
>>>> getFilePath('__main__')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 1, in <lambda>
AttributeError: 'module' object has no attribute 'prefix'
>>>>

Can you help me?

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Gmane