Some more information on finding python, its modules and whether they are built-in, modules of the standard distribution or those in the site-packages folder
References:
29.12. inspect — Inspect live objects — Python 3.4.4 documentation look at the table on Types and Members
http://stackoverflow.com/questions/247770/retrieving-python-module-path just one of hundreds of examples.
| Type | Attribute | Description |
|---|
| module | __doc__ | documentation string |
| __file__ | filename (missing for built-in modules) |
| Tips | Example |
|---|
A standard installation compatible with ArcMap and arcpy is assumed. These: - dir(module_name)
- help(module_name)
are your friends. Look for a __file__ property in the function/property list. | The os module does... <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> os
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dir<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> snip <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'__doc__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'__file__'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'__loader__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'__name__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> snip <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">]</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> os<SPAN class="punctuation token">.</SPAN>__file__
<SPAN class="string token">'C:\\Python34\\lib\\os.py'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> The time module doesn't <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> time
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dir<SPAN class="punctuation token">(</SPAN>time<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> snip <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'__doc__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'__file__'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'__loader__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'__name__'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> snip <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">]</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> time<SPAN class="punctuation token">.</SPAN>__file__ <SPAN class="comment token"># I will try anyway</SPAN>
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"<interactive input>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
AttributeError<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'module'</SPAN> object has no attribute <SPAN class="string token">'__file__'</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
Modules in the C:\PythonXX\lib\ folder will have a __file__ property. | <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># from ... C:\Python34\Lib ... folder</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> collections
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> collections<SPAN class="punctuation token">.</SPAN>__file__
<SPAN class="string token">'C:\\Python34\\lib\\collections\\__init__.py'</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
| Modules that aren't part of the standard distribution and have to be installed via pip or other means, get put in the site-packages folder and they will have a __file__ | How about arcpy? <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> arcpy<SPAN class="punctuation token">.</SPAN>__file__
<SPAN class="string token">'C:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy\\arcpy\\__init__.py'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> And my fav... <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> np<SPAN class="punctuation token">.</SPAN>__file__
<SPAN class="string token">'C:\\Python34\\lib\\site-packages\\numpy\\__init__.py'</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
| You can use the inspect module as well... all the cool programmers do ... | Numpy was previously imported as np. Notice the results are the same as the np.__file__ approach. <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> inspect
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> inspect<SPAN class="punctuation token">.</SPAN>getfile<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">)</SPAN>
<SPAN class="string token">'C:\\Python34\\lib\\site-packages\\numpy\\__init__.py'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> Lets try time. <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> inspect<SPAN class="punctuation token">.</SPAN>getfile<SPAN class="punctuation token">(</SPAN>time<SPAN class="punctuation token">)</SPAN>
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"<interactive input>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
File <SPAN class="string token">"C:\Python34\lib\inspect.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">518</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> getfile
<SPAN class="keyword token">raise</SPAN> TypeError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{!r} is a built-in module'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>object<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
TypeError<SPAN class="punctuation token">:</SPAN> <SPAN class="operator token"><</SPAN>module <SPAN class="string token">'time'</SPAN> <SPAN class="punctuation token">(</SPAN>built<SPAN class="operator token">-</SPAN><SPAN class="keyword token">in</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">is</SPAN> a built<SPAN class="operator token">-</SPAN><SPAN class="keyword token">in</SPAN> module
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>No... it is a built-in. But... you can another way... <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> time
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> inspect<SPAN class="punctuation token">.</SPAN>getmodule<SPAN class="punctuation token">(</SPAN>time<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token"><</SPAN>module <SPAN class="string token">'time'</SPAN> <SPAN class="punctuation token">(</SPAN>built<SPAN class="operator token">-</SPAN><SPAN class="keyword token">in</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |