Import module' or 'from module import?

The difference between import module and from module import foo is subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide on which you prefer.

The difference between import module and from module import foo is subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide on which you prefer import module Pros: less maintenance of your import statement code (don't have to update import statement if you start using another item from the module) Cons: typing module.

Foo in your code can be tedious and redundant from module import foo Pros: Don't have to type as much to use foo Cons: Have to update your import statement every time you use a new item from the module Either method is acceptable, but don't use from module import *. For any reasonable large set of code, if you import * your will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it east to get to the point where you think you don't use the import any more but its extremely difficult to be sure.

2 +1 for talking about 'from module import *' – dwc Apr 2 '09 at 20:01 +1 for discouraging usage of "from module import *", it just clutters the namespace. – Christian Witts Apr 3 '09 at 6:49 4 cluttering the namespace is not the most problematic part of "import *", it's the reduction in readability: Any name conflicts will show themselves in (unit) testing. But all the names you use from the imported module will be bare, with nary a hint were they come from.

I absolutely loathe "import *". – Jürgen A. Erhard Dec 26 '09 at 19:59 2 Doesn't the Zen of Python say explicit is better than implicit?

– Antony Koch Feb 24 '11 at 11:51.

Both ways are supported for a reason: there are times when one is more appropriate than the other. Import module: nice when you are using many bits from the module. Drawback is that you'll need to qualify each reference with the module name.

From module import ...: nice that imported items are usable directly without module name prefix. Drawback is that you must list each thing you use, and that it's not clear in code where something came from. Which to use depends on which makes the code clear and readable, and has more than a little to do with personal preference.

I lean toward import module generally because in the code it's very clear where an object or function came from. I use from module import ... when I'm using some object/function a lot in the code.

I personally always use from package.subpackage. Subsubpackage import module and then access everything as module. Function module.

Modulevar etc. The reason is that at the same time you have short invocation, and you clearly define the module namespace of each routine, something that is very useful if you have to search for usage of a given module in your source. Needless to say, do not use the import *, because it pollutes your namespace and it does not tell you where a given function comes from (from which module) Of course, you can run in trouble if you have the same module name for two different modules in two different packages, like from package1. Subpackage import module from package2.

Subpackage import module in this case, of course you run into troubles, but then there's a strong hint that your package layout is flawed, and you have to rethink it.

2 In the last case, you can always use: import pkgN.sub. Module as modN giving you distinct names for each module. You can also use the 'import modulename as mod1' pattern to shorten a long name, or to switch between implementations of the same API (e.g. DB API modules) with a single name change.

– Jeff Shannon Apr 3 '09 at 0:59.

Import module Is best when you will use many functions from the module. From module import function Is best when you want to avoid polluting the global namespace with all the functions and types from a module when you only need function.

You only pollute the namespace if you do 'from .. import *'. – John Fouhy Apr 2 '09 at 23:25 Yes good point - I could have made that clearer. – Andrew Hare Apr 3 '09 at 1:57.

My own answer to this depends mostly on first, how many different modules I'll be using. If i'm only going to use one or two, I'll often use from ... import since it makes for fewer keystrokes in the rest of the file, but if I'm going to make use of many different modules, I prefer just import because that means that each module reference is self-documenting. I can see where each symbol comes from without having to hunt around.

Usuaully I prefer the self documenting style of plain import and only change to from.. import when the number of times I have to type the module name grows above 10 to 20, even if there's only one module being imported.

To add to what people have said about from x import *: besides making it more difficult to tell where names came from, this throws off code checkers like Pylint. They will report those names as undefined variables.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions