about importing modules

This is the place for queries that don't fit in any of the other categories.

about importing modules

Postby pynub » Mon Sep 16, 2013 2:39 pm

Hi All

I am working an a small python script and I have at the top at line

Code: Select all
import os.path



But I am looking something up and I see it has just
Code: Select all
import os


at the top.

If I import os, will this import os.path also. Is import os.path just a subset of the os module?

Why would I ever choose just to import os.path rather than just import.os?

So can I safely just change import os.path to import os ?


Thanks
pynub
 
Posts: 6
Joined: Sun Sep 15, 2013 9:04 pm

Re: about importing modules

Postby stranac » Mon Sep 16, 2013 2:52 pm

When you import a package, only the top level of it is imported(its __init__.py).
So in general case, you won't be able to say import package and use the package.module module inside it.
Also, if you import package.module, you won't be able to use the stuff from the package namespace.


os and os.path are a bit of a special case.
It lookes like os is a package, and os.path is a module within it.

But that's not the case.
os actually just does some magic that makes it seem like os.path is a part of it.
It's actually a platform-specific module that gets imported inside os, and magic-ed the shit out of.

Whether you import os or os.path, both will be available to you.

However, I would recommend using import os if you're using stuff from the os namespace, and import os.path if you're using stuff from os.path namespace.
You know - explicitness, clarity and stuff like that.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1133
Joined: Thu Feb 07, 2013 3:42 pm

Re: about importing modules

Postby pynub » Tue Sep 17, 2013 10:41 am

ok - this is going to be a dumb question. How do I know if i'm using stuff from what namespace?
pynub
 
Posts: 6
Joined: Sun Sep 15, 2013 9:04 pm

Re: about importing modules

Postby stranac » Tue Sep 17, 2013 12:22 pm

If you use it like 'something.whatever', it's in the 'something' namespace.
If you use 'another.stupid.example.thing', it's in the 'another.stupid.example' namespace.
...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1133
Joined: Thu Feb 07, 2013 3:42 pm

Re: about importing modules

Postby pynub » Tue Sep 17, 2013 1:34 pm

OK thanks. And http://docs.python.org is where is check what each thing does e.g. http://docs.python.org/2/library/os.path.html
pynub
 
Posts: 6
Joined: Sun Sep 15, 2013 9:04 pm

Re: about importing modules

Postby Marbelous » Tue Sep 17, 2013 2:09 pm

Yes, but don't forget that manual is on your PC as well if you installed python in a normal way. You'll also find the interactive PYDOC system in the python menu. You can ALSO just use the built-in help, e.g.:
Code: Select all
 help(os.path)

OR, you can simply print a module's docstring directly:
Code: Select all
print os.path.__doc__

Python is quite a helpful snake. ;)
Marbelous
 
Posts: 136
Joined: Fri May 31, 2013 8:12 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 4 guests