PDA

View Full Version : Importing modules in Python?



RingZzer0
11-24-2011, 05:13 AM
Hi,

I am new to python coding and aware of following ways of importing modules (sys in my case):

1. import sys
All methods/functions of sys would get imported and the way of accessing them would be - sys.version

2. from sys import *
All methods/functions of sys would get imported and the way of accessing them would be - version

3. from sys import version
Just the method 'version' would get imported and the way of accessing it would be - version

Further, I think the best way to avoid collisions is to use the way 1 so that method/function could be associated with module name (sys.version) and collision could be avoided with other local or user methods.

Is my perception clear about importing and using them? Or are there more ways of doing it?

Thanks

abhaythehero
11-24-2011, 02:12 PM
Is my perception clear about importing and using them? Or are there more ways of doing it?

Thanks

Yes you are on right track and all of the points you mentioned are correct :)

RingZzer0
11-25-2011, 12:02 AM
Hey thanks for endorsing mate.

RingZzer0
12-12-2011, 10:57 AM
Excerpt from Dive into Python -

-------------------------------------------------------

When should you use from module import?
* If you will be accessing attributes and methods often and don't want to type the module name over and over, use from module import.
* If you want to selectively import some attributes and methods but not others, use from module import.
* If the module contains attributes or functions whti the same name as onces in your module, you must use import module to avoid name conflicts.

Use from module import * sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes dubugging and refactoring more difficult.

-------------------------------------------------------