1

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

0x14
  • 13
  • 1
  • 5

1 Answers1

0

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']
Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • `time` module has `__file__` attribute as any other imported module (if `__file__` has meaning for it) – jfs Mar 09 '14 at 18:10
  • @J.F.Sebastian maybe in Python 3, but not 2.7: `>>> import time >>> time.__file__ Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute '__file__'` – Russia Must Remove Putin Mar 09 '14 at 18:27
  • it probably means that `time` is builtin into python executable in your installation: `'time' in sys.builtin_module_names`. The location is the location of `python` executable itself in this case. – jfs Mar 09 '14 at 18:37
  • So to elaborate on this: i did `import imp` followed by `imp.find_module('time')` and it returned this: `(None, 'time', ('', '', 6))` now, the second item in the list `'time'` is where the location of the file on the disk would be, if it was a file somewhere in the c:\python directory, but it listing itself makes me think its just located within python itself, comes under the Standard Library perhaps? – 0x14 Mar 09 '14 at 22:04
  • @0x14 Yes, time is in the Standard Library, but so is e.g. `collections`, which is a Python package. Let me add a bit regarding your motivating reason as stated in the question. – Russia Must Remove Putin Mar 09 '14 at 22:25
  • @0x14 you should make edits to your question rather than this answer. edit text can be found [here](http://stackoverflow.com/review/suggested-edits/4302467) and [here](http://stackoverflow.com/review/suggested-edits/4302495) if you need to copy and paste. – OGHaza Mar 10 '14 at 12:26
  • I was just in the process of doing so! Done. Also i got the formatting/markdown to work for me. – 0x14 Mar 10 '14 at 12:29