module path?

A

akonsu

hello,

is there a way to determine the file location of a loaded module?
assuming it is not built in.

import settings
print settings

produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>

i would like to get to this path somehow other than by parsing the
string representation of the module. is there a property on
types.ModuleType? i could not find anything...

thanks
konstantin
 
S

Steven D'Aprano

hello,

is there a way to determine the file location of a loaded module?
assuming it is not built in.

import settings
print settings

produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>

i would like to get to this path somehow other than by parsing the
string representation of the module. is there a property on
types.ModuleType? i could not find anything...

Did you look at dir(settings) for a list of method and attribute names?

Look at module.__file__. But be careful because some modules are "built
in", that is, they don't actually exist as a separate file and are part
of the Python compiler. E.g.:
Traceback (most recent call last):
<module 'sys' (built-in)>
 
A

akonsu

Did you look at dir(settings) for a list of method and attribute names?

Look at module.__file__. But be careful because some modules are "built
in", that is, they don't actually exist as a separate file and are part
of the Python compiler. E.g.:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'>>> sys

<module 'sys' (built-in)>

thanks! i did not know about dir() method.
konstantin
 
D

Dave Angel

akonsu said:
hello,

is there a way to determine the file location of a loaded module?
assuming it is not built in.

import settings
print settings

produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>

i would like to get to this path somehow other than by parsing the
string representation of the module. is there a property on
types.ModuleType? i could not find anything...

thanks
konstantin
The property is called __file__

So in this case, filename = settings.__file__
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top