1

To describe my questions concisely, please have a look at examples below:

  1. Module os have a function getcwd() which returns current working directory. But there are no details about os.getcwd() in /usr/lib/python2.7/os.py file. Where is the implementation of the function?

  2. os.path is also a module in python , but in /usr/lib/python2.7 directory, there is no file named os.path. So when you import os.path in your python script, which file is imported?

Thank all your helps...

Björn Pollex
77.2k30 gold badges206 silver badges290 bronze badges
asked Mar 11, 2011 at 12:44

4 Answers 4

3

1 . The getcwd() functions is implemented in C look here.

2 . os.path is defined in the module os by dynamically detecting the os type and importing the correspondent library and set in it using : sys.modules['os.path'] = path

answered Mar 11, 2011 at 12:52
Sign up to request clarification or add additional context in comments.

Comments

2

Modules do not have to be python scripts. Using the C-API you can write modules in C or C++. You can compile them as dynamic libraries, so that the interpreter can load them dynamically, or you can recompile the interpreter and link the modules into it.

answered Mar 11, 2011 at 12:52

1 Comment

... and in Jython, modules are installed as .class files. I guess IronPython does something similar.
1

If you're on a POSIX system (Linux, Mac OS X), these lines in os.py bring in those bits:

from posix import *
import posixpath as path

And on Windows:

from nt import *
import ntpath as path

(Plus a couple more options for less popular systems)

Note that using from x import * is usually frowned on. This is kind of a special case.

answered Mar 11, 2011 at 12:54

Comments

1

The interactive python shell can be used to check where a module is loaded from, and to see if a method is built-in or python:

>>> import os
>>> os
<module 'os' from '/usr/lib/python2.6/os.pyc'>
>>> os.path
<module 'posixpath' from '/usr/lib/python2.6/posixpath.pyc'>
>>> os.getcwd
<built-in function getcwd>
>>> os.path.join
<function join at 0x87d1b1c>
>>>

os.path is loaded from posixpath.pyc, os.getcwd is built-in, os.path.join is a python method.

answered Mar 11, 2011 at 13:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.