0

I wanted to know, why when importing some module's classes, it has to be done with the from statement.

Here is an example:


>>> import selenium
>>> dir(selenium)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__']

The webdriver class is missing. But when imported with the from statement it gets imported.

>>> from selenium import webdriver
>>> dir(webdriver)
['ActionChains', 'Android', 'BlackBerry', 'Chrome', 'ChromeOptions', 'DesiredCapabilities', 'Edge', 'Firefox', 'FirefoxOptions', 'FirefoxProfile', 'Ie', 'IeOptions', 'Opera', 'PhantomJS', 'Proxy', 'Remote', 'Safari', 'TouchActions', 'WebKitGTK', 'WebKitGTKOptions', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'android', 'blackberry', 'chrome', 'common', 'edge', 'firefox', 'ie', 'opera', 'phantomjs', 'remote', 'safari', 'support', 'webkitgtk']

2 Answers2

0

If you say 'import selenium', you import the selenium.__init__ file. Looking at it, we can see that it does not have any useful content. So you don't really get anything useful by just importing selenium. However if you do 'from selenium import webdriver' you effectively import selenium.webdriver.__init__ which contains the stuff you actually want.

See the docs for importing modules.

0

You don't have to use the from statement (actually, it's even better to avoid using from). The following works too:

import selenium.webdriver
wovano
  • 4,543
  • 5
  • 22
  • 49