views:

315

answers:

4

According to the official documentation, os.path is a module. Thus, what is the preferred way of importing it?

# Should I always import it explicitly?
import os.path

Or...

# Is importing os enough?
import os

Please DON'T answer "importing os works for me". I know, it works for me too right now (as of Python 2.6). What I want to know is any official recommendation about this issue. So, if you answer this question, please post your references.

+1  A: 

Interestingly enough, importing os.path will import all of os. try the following in the interactive prompt:

import os.path
dir(os)

The result will be the same as if you just imported os. This is because os.path will refer to a different module based on which operating system you have, so python will import os to determine which module to load for path.

reference

With some modules, saying import foo will not expose foo.bar, so I guess it really depends the design of the specific module.


In general, just importing the explicit modules you need should be marginally faster. On my machine:

import os.path: 7.54285810068e-06 seconds

import os: 9.21904878972e-06 seconds

These times are close enough to be fairly negligible. Your program may need to use other modules from os either now or at a later time, so usually it makes sense just to sacrifice the two microseconds and use import os to avoid this error at a later time. I usually side with just importing os as a whole, but can see why some would prefer import os.path to technically be more efficient and convey to readers of the code that that is the only part of the os module that will need to be used. It essentially boils down to a style question in my mind.

Matt Boehm
`from os import path` will make the calls to path even faster if speed is the issue.
Justin Peel
Being pythonic, explicit is better than implicit right?In reality I think it really is the judgment call by the user, whether the user is only going to use os.path or multiple modules within os. Maybe one method is more in line with your philosophy over the other?
Andrew Kou
Timing this is some of the prematurest premature optimization I've ever seen. This has never been *anyone's* bottleneck and the time here is immaterial to how someone should code.
Mike Graham
A: 

Couldn't find any definitive reference, but I see that the example code for os.walk uses os.path but only imports os

Chris Hulan
Hey, please change the link from #module-os to #os.walk, so the it will point directly to that function.
Denilson Sá
@Denilson: I edited the answer as per your suggestion, which was correct.
ΤΖΩΤΖΙΟΥ
+8  A: 

As per PEP-20 by Tim Peters, "Explicit is better than implicit" and "Readability counts". If all you need from the os module is under os.path, import os.path would be more explicit and let others know what you really care about.

Likewise, PEP-20 also says "Simple is better than complex", so if you also need stuff that resides under the more-general os umbrella, import os would be preferred.

Nick T
I don't see how `import os` is really about being "simple" in any meaningful way. Simple != short.
Mike Graham
I was more trying to point out that `import os` **and** a `import os.path` is daft if you e.g. need `os.getcwd()` and `os.path.isfile()`
Nick T
+5  A: 

os.path works in a funny way. It looks like os should be a package with a submodule path, but in reality os is a normal module that does magic with sys.modules to inject os.path. Here's what happens:

  • When Python starts up, it loads a bunch of modules into sys.modules. They aren't bound to any names in your script, but you can access the already-created modules when you import them in some way.

    • sys.modules is a dict in which modules are cached. When you import a module, if it already has been imported somewhere, it gets the instance stored in sys.modules.
  • os is among the modules that are loaded when Python starts up. It assigns its path attribute to an os-specific path module.

  • It injects sys.modules['os.path'] = path so that you're able to do "import os.path" as though it was a submodule.

I tend to think of os.path as a module I want to use rather than an thing in the os module, so even though it's not really a submodule of a package called os, I import it sort of like it is one and I always do import os.path. This is consistent with how os.path is documented.


Incidentally, this sort of structure leads to a lot of Python programmers' early confusion about modules and packages and code organization, I think. This is really for two reasons

  1. If you think of os as a package and know that you can do import os and have access to the submodule os.path, you may be surprised later when you can't do import twisted and automatically access twisted.spread without importing it. causes this.

    (This is because os.path is already in sys.modules, not because the name path was defined in the module, BTW.)

  2. It is confusing that os.name is a normal thing, a string, and os.path is a module. I always structure my packages with empty __init__.py files so that at the same level I always have one type of thing: a module/package or other stuff. Several big Python projects take this approach, which tends to make more structured code.

Mike Graham
Excellent, very informative answer! Congratulations! Even though it doesn't directly answer the question, it has lots of useful details. But could you please elaborate on "This is consistent with how os.path is documented"? Like Chris Hulan said, the os.walk() example imports just os instead of os.path.
Denilson Sá
@Denilson, It does include a direct answer: I always do `import os.path` myself and think that's a nicer way. By "This is consistent with how os.path is documented" I meant that it is given its own page in the documentation at http://docs.python.org/library/os.path.html .
Mike Graham