pythonpython-2.7timeimportmodule

Python 2.7: "import time" Where is python importing this time module from?


When I'm importing something in Python, in this case import time, where is time being imported from? Where is the module located prior to import? I tried using dir() on the __builtin__ and other folders in my interpreter, but it's not there.

I have looked in the c:\python folder and a search turns up nothing for a file named time but nonetheless time.sleep(1) works just fine, so it is imported. Is it being automatically downloaded from the network?

I ultimately want to import some other modules for text coloring, but I need to know where this one came from first. Thanks.


Further to Aarons "motivations to your question" edit:

[u'', 'C:\\Program Files (x86)\\PyScripter\\Lib\\rpyc.zip', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', <--- This location 'C:\\Python27\\lib\\site-packages']

This is what i get. Im gonna guess from whats been said, by J.F.Sebastian, that the time module is in the above location marked (C:\Python27) (in the executable file), although my first guess would have been within a file within the lib folder.

I looked for collections and that is an independent file, so even though that too is the Standard Library, it exists in a file on the disk and i know where it is exactly. I would like to know this too for time (im guessing its in the .exe).

If i was to save my own modules in the above marked location (if not where), would i save it as examplemodule[no file extension], examplemodule.py?, and if saved with an extension would i have to import it with the extension i.e. import examplemodule.py? or not.

This is a question (the extension bit) which would probably be answered for me, if i simply could see the file it is on the disk, but being either compiled into the python executable or in the Libs directory i cannot. Thanks again


Solution

  • The 'time' module is written in C, and in Python 2 does not have a .__file__, like others such as collections are. In Python 3 (3.4 on my system):

    time.__file__
    

    returns

    '/home/user/cpython/build/lib.linux-x86_64-3.4-pydebug/time.cpython-34dm.so'
    

    I have cpython in my home directory, and I can get to the source code by looking in /Modules/timemodule.c

    Here's the module in Mercurial online: http://hg.python.org/cpython/file/3ae2cd85a908/Modules/timemodule.c


    The answers on this older question are out of date: How do I find the location of Python module sources?


    Let me address the motivation for the question, you're concerned about where to place your modules so you can import them. Their location has to be in sys.path, the first item of which is '' but semantically means your script directory.

    Here's my output from sys.path from my own build of Python 3.4:

    >>> import sys, pprint
    >>> pprint.pprint(sys.path)
    ['',
     '/usr/local/lib/python34.zip',
     '/home/user/cpython/Lib',
     '/home/user/cpython/Lib/plat-linux',
     '/home/user/cpython/build/lib.linux-x86_64-3.4-pydebug',
     '/home/user/.local/lib/python3.4/site-packages']