views:

57

answers:

2

Hello, all.

I am inspecting the JSON module of python 3.1, and am currently in /Lib/json/scanner.py. At the top of the file is the following line:

from _json import make_scanner as c_make_scanner

There are five .py files in the module's directory: __init__ (two leading and trailing underscores, it's formatting as bold), decoder, encoder, scanner and tool. There is no file called "json".

My question is: when doing the import, where exactly is "make_scanner" coming from?

Yes, I am very new to Python!

+5  A: 

It's coming from a C-compiled _json.pyd (or _json.so, etc, etc, depending on the platform) that lives elsewhere on the sys.path. You can always find out where that is in your specific Python installation by importing the module yourself and looking at its __file__, e.g.:

>>> import _json
>>> _json.__file__
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_json.so'

As you see, in my installation of Python 2.6, _json comes from the lib-dynload subdirectory of lib/python2.6, and the extension used on this platform is .so.

Alex Martelli
+1  A: 

It may be coming from a file, or it may be built-in. On Windows, it appears to be built-in.

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _json
>>> _json.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

and there is no _json.pyd or _json.dll in the offing.

If you want to see the source, having a binary file on your machine or not is irrelevant -- you'll need the SVN browser.

John Machin
I am receiving the same "'module' object has no attribute '__file__'" message. Does this mean that there is no way to see make_scanner?
GlenCrawford