-3
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils

Is this the same as:

from keras *

In Java, I can do this shortcut, and it looks much cleaner. I am learning Python.

marlon
  • 6,029
  • 8
  • 42
  • 76
  • In a way, this question is a duplicate of https://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided – Grismar Jan 22 '19 at 01:46
  • 2
    Have a look here: https://stackoverflow.com/questions/2360724/what-exactly-does-import-import Basically the `from keras import *` statement imports much more things than just those in your first four rows. – Valentino Jan 22 '19 at 01:47

2 Answers2

1

I believe from keras * is not valid syntax, but you can do:

from keras import *

Note: PEP-8 recommends that you avoid wildcard imports...

"...as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance)."

But then you need to prefix a call with the submodule it comes from, for example:

f = layers.Dense(1)
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
  • If you are going to suggest that line of code, you should also mention why it is not recommended to use it – jberrio Jan 22 '19 at 01:50
  • 1
    I dropped in the PEP-8 on that topic in an edit to my answer @jberrio and thanks for the constructive criticism! – Charles Landau Jan 22 '19 at 01:55
  • A wildcard import like `from keras import *` will only import was is available in the `keras/__init__.py` file. Many libraries create names for convenience imports there, but not all. Submodules and packages are automatically imported. – Klaus D. Jan 22 '19 at 02:11
1

I guess it's ok, but be aware you'll have to re-type lots of lines to do stuff you want, and your code can be a little messy with that... example:

import keras
keras.models.sequential()
keras.layers.Dense()
keras.wrappers.scikit_learn (...)

In a big code this would be really confusing and annoying to read. I used to have the same habit as yours, but with time I lost it :D