How do I find out what file an import is using?

Dave Angel d at davea.name
Wed May 9 14:15:17 EDT 2012


On 05/09/2012 01:52 PM, Rob Richardson wrote:
> I am trying to work with a Python script someone else wrote.  The script includes the line
> 	from Level3Utils import *
>
> I need to look at the functions that are included in that import.  In an effort to identify exactly which file is being used, I renamed the Level3Utils.py and Level3Utils.pyc files in the same folder as the script I'm working on.  The import command in PythonWin executed without error.  I looked for a file named Level3Utils.py in my Python tree (d:/Python25, in my case).  None were there.  I then commented out the import line and stepped through the code.  It ran without error!  The class that should have come from Level3Utils executed successfully without being imported!
>
> How do I find out where the class definition actually is?
>
> (There is no PYTHONPATH environmental variable defined on my machine.)
>
> Thanks very much!
>
> RobR
First, if you want to see the import path, just display    sys.path

  import sys
  print sys.path

Next, it's bad practice to use the form:    from  nnnn import *    
because it's then hard to see what (if anything) you actually imported
from there.  And you can easily hide your own globals, or conversely
hide some imports with a new global you might define.   If removing the
import doesn't stop the code from running, you probably aren't using
anything from it, and should leave the line out.

However, it is frequently useful to find where a module is coming from,
and what symbols it defines.

I'm using wxversion module for an example, because it's not very big. 
And I'm doing it interactively, while you probably want to print these
values from your code.


>>> dir(wxversion)
['AlreadyImportedError', 'UPDATE_URL', 'VersionError', '_EM_DEBUG',
'__builtins__', '__doc__', '__file__', '__name__', '__package__',
'_find_default', '_find_installed', '_get_best_match', '_pattern',
'_selected', '_wxPackageInfo', 'checkInstalled', 'ensureMinimal',
'fnmatch', 'getInstalled', 'glob', 'os', 're', 'select', 'sys']
>>> wxversion.__file__
'/usr/lib/python2.7/dist-packages/wxversion.pyc'

In other words, try
   print Level3Utils.__file__



-- 

DaveA




More information about the Python-list mailing list