Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an import not always import nested packages?

Why the first code doesn't work while the second does?

First code:

import selenium

driver = selenium.webdriver.Firefox()

AttributeError: 'module' object has no attribute 'webdriver'

Second code:

from selenium import webdriver

driver = webdriver.Firefox()
like image 379
FrozenHeart Avatar asked Jun 12 '14 21:06

FrozenHeart


1 Answers

Nested packages are not automatically loaded; not until you import selenium.webdriver is it available as an attribute. Importing just selenium is not enough.

Do this:

import selenium.webdriver

driver = selenium.webdriver.Firefox()

Sometimes the package itself will import a nested package in the __init__.py package initializer; os imports os.path, so os.path is immediately available even if you import just os.

like image 171
Martijn Pieters Avatar answered Oct 04 '22 06:10

Martijn Pieters