473,564 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1831


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().k eys())
from os.path import * # looks up os.path module as found in the cache
set(globals().k eys()) - old_globals

set(['pardir', 'sameopenfile', 'exists', 'sep', 'splitext', 'basename',
'walk', 'expandvars', 'old_globals', 'expanduser', 'getmtime', 'defpath',
'dirname', 'isfile', 'supports_unico de_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.subpa ckage

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 anansispacework s.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.gamm a you need just a bit of
string processing. "alpha.beta.gam ma".split() -- aah, I have to import
alpha, then alpha.beta, then alpha.beta.gamm a... 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
9701
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? ----------------------------------------------------------------- When loading modules, Python looks for modules in the following places in the following order:
4
1597
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 * rScript = 'myScript.r'
70
4041
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 Python standard library, and Reinhold Birkenfeld started a discussion on it in python-dev...
2
23368
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 using '/' as delimiters and get the last 'word'. The reason I'm not entirely happy with that method is that it is platform specific. I would prefer...
3
12366
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 part and a extension part. For example: '/Users/t/web/perl-python/I_Love_You.html' becomes
1
2929
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" source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx"> <module id="1" title="Introducing Windows Forms" location="D:\Disk-C\Documents and...
4
1681
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 same directory as the imported module. What I do not want is to tell the module the location of the AppleScript file by passing this information from...
0
1129
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 of the win32 install. DIR_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) PROJECT_HOME = os.path.join(DIR_PATH, '..', '..',...
8
1552
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, raising ImportError which is completely baffling me. According to sys.path, both should fail; the directory containing my_module is not in sys.path...
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.