473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

I just wanna know about os.path module..

I've just started digging into how python works..
I found that other mudules are clearly declared like one file per a
module..

But the only os.path doesn't have their own file..
ye I know is has actually depending on os like in my case posixpath..

What I'd love to know is..
when I call import os.path..
how happened under the hood?

I'm currently using python 2.4 in linux machine..
I'd appreciate it

Jul 21 '05 #1
6 1823


kimes wrote:
I've just started digging into how python works..
I found that other mudules are clearly declared like one file per a
module..

But the only os.path doesn't have their own file..
ye I know is has actually depending on os like in my case posixpath..

What I'd love to know is..
when I call import os.path..
how happened under the hood?

I'm currently using python 2.4 in linux machine..
I'd appreciate it


See line 5 of os.py (comment)
- os.path is one of the modules posixpath, ntpath, or macpath
It says it sets os.path depending on the platform

and does so in line 132
sys.modules['os.path'] = path

In your case, os.path should be implemented in posixpath.py
See line 48 to see where it came from

In short, "use the source, Luke" :-)
especially when Python is exceptionally readable.

Jul 21 '05 #2
kimes wrote:
But the only os.path doesn't have their own file..
ye I know is has actually depending on os like in my case posixpath..

What I'd love to know is..
when I call import os.path..
how happened under the hood?


At first os - module, or package, it doesn't matter here - is imported.
In its code, it detects the proper path module and imports it. The module
cache is then manipulated and the name 'path' is bound to posixpath, too:

sys.modules["os.path"] = posixpath
path = posixpath

The second stage of the import then reduces to just a lookup in the cache
instead of a search for the inexistent .../os/path.py in the filesystem.

Both the module attribute and the cache update are necessary when you want
to pull off similar tricks. Here is an example of the odd behaviour that
results from them being out of sync:
import os
os.path = 42
from os import path # binds the path attribute of module os
path 42 import os.path
os.path 42 old_globals = set(globals().keys())
from os.path import * # looks up os.path module as found in the cache
set(globals().keys()) - old_globals

set(['pardir', 'sameopenfile', 'exists', 'sep', 'splitext', 'basename',
'walk', 'expandvars', 'old_globals', 'expanduser', 'getmtime', 'defpath',
'dirname', 'isfile', 'supports_unicode_filenames', 'pathsep', 'getsize',
'samestat', 'split', 'devnull', 'islink', 'curdir', 'samefile', 'realpath',
'commonprefix', 'abspath', 'normcase', 'getatime', 'isdir', 'join',
'altsep', 'getctime', 'isabs', 'normpath', 'ismount', 'splitdrive',
'extsep'])

Peter

Jul 21 '05 #3
look at 'path' module too

http://www.jorendorff.com/articles/python/path/

Jul 21 '05 #4
Thanks for all you guys help..

But Peter,
You said 'At first os - module, or package, it doesn't matter here - is
imported.'

I'm still confused about that..
When I just call import os.path without calling import os..
In that case, you mean 'import os' is called implicitly?
Why? and How?

how python knows it should call import when we call import os?
Please make me clear.. :)
Thanks..

Jul 21 '05 #5
On Sunday 17 July 2005 12:47 pm, kimes wrote:
Thanks for all you guys help..
I'm still confused about that..
When I just call import os.path without calling import os..
In that case, you mean 'import os' is called implicitly?
Why? and How?

how python knows it should call import when we call import os?
Please make me clear.. :)


All you have to do is

import os

Python "knows" to import "os.path" because "os" tells it to. That is
to say, somewhere in "os" is code (conceptually) like:

import path

The os.path module is a sub-module of os. What makes it
particularly interesting is only that it is not always the *same*
module -- the particular os.path module that is loaded is
determined by your architecture. On a POSIX system (including
Unix and Linux) it will actually be an alias for "posixpath", but
it will be something else on Windows and Macintosh systems.

OTOH, the same thing will happen if you say:

import os.path

since it must import "os" in order to get to "path". I don't
think that Python is guaranteed to load the entire contents
of such dotted imports, although it appears to do so for the
case of "os" (I believe it depends on the code in the module).

Occasionally you will find packages that require you to load
subpackages explicitly:

import mypackage
import mypackage.subpackage

but not with "os".

Oh, and what Peter Otten meant by
"module, or package, it doesn't matter here"
is that the two terms are roughly equivalent, although there
is an internal distinction -- a "module" is a single Python source
file, whereas a "package" is a directory of source files which are
set up to load like a single module. For the purposes of this
discussion, the two terms are basically interchangeable (we
really ought to have a single term that can mean either -- I usually
speak loosely of "modules" regardless of which way they are
actually defined).

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 21 '05 #6
kimes wrote:
You said 'At first os - module, or package, it doesn't matter here - is
imported.'
With a file mop.py in your path

import mop

creates a module instance and executes mop.py to fill it with content --
classes, functions, whatever python objects you can think of -- and puts
that module instance into the sys.modules cache. With a file
mop/__init__.py, the process is the same except that the code to fill the
module is taken from the __init__.py file. Differences only appear when you
try to import submodules.
I'm still confused about that..
When I just call import os.path without calling import os..
In that case, you mean 'import os' is called implicitly?
Yes. Try it yourself with a package/submodule where __init__.py and
submodule.py just contain a print statement. I find such an experimental
approach often more convenient than haunting the docs.
Why? and How?
Because it's a sensible assumption that a submodule depends on its parent
package? Because it makes access of submodules as easy as attribute access
for any other object? Because it makes conditional imports easy (see
Terry's post)? Because it's the simplest approach that just works?
how python knows it should call import when we call import os?
Please make me clear.. :)


Conceptually, when you have a module alpha.beta.gamma you need just a bit of
string processing. "alpha.beta.gamma".split() -- aah, I have to import
alpha, then alpha.beta, then alpha.beta.gamma... read the source for the
strategy that is actually taken if you really care.

Peter

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Ferg | last post by:
I need a little help here. I'm developing some introductory material on Python for non-programmers. The first draft includes this statement. Is this correct? ...
4
by: Lars Yencken | last post by:
Hello, I'm working on a project where my python modules are using persistent files in the same directory. As an example, we're using rpy, so a piece of python code might read: from rpy import...
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
1
by: orit | last post by:
I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days"...
4
by: Martin M. | last post by:
Hi, I have the following question: How can an imported module see/find the path to itself? Background: From my main script I import a module which needs a file (AppleScript) located in the...
0
by: Berlin Brown | last post by:
I am trying to run some basic unit tests, but I can't get the paths setup in python/cygwin to pick up my modules. This code works fine in linux and I installed python through cygwin not as part...
8
by: tow | last post by:
I have a python script (part of a django application, if it makes any difference) which is exhibiting the following behaviour: import my_module # succeeds imp.find_module("my_module") # fails,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.