Why doesn't import always import nested packages?

Why does the first code not work during the second?

First code:

import selenium driver = selenium.webdriver.Firefox() 

AttributeError: the 'module' object does not have the 'webdriver' attribute

Second code:

 from selenium import webdriver driver = webdriver.Firefox() 
+6
source share
1 answer

Nested packages do not load automatically; until you import selenium.webdriver , it is available as an attribute. Importing selenium alone is not enough.

Do it:

 import selenium.webdriver driver = selenium.webdriver.Firefox() 

Sometimes a package itself imports a nested package into the package initializer __init__.py ; os imports os.path , so os.path immediately available, even if you only import os .

+12
source

Source: https://habr.com/ru/post/970780/


All Articles