Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to import submodules directly?

Tags:

python

import

Let's say I have a module foo and a submodule foo.bar. If I want to use a method in foo.bar, do I need to import foo.bar directly or is importing foo sufficient?

For example, the following throws an error:

import foo

foo.bar.my_method()

and the following works:

import foo.bar

foo.bar.my_method()

But I'm not sure if this is generally what's needed, or if there's something wrong with my code itself. (I would think importing the submodule directly is generally needed... But I could have sworn I've seen code where it's not imported directly and still works fine.)

like image 570
Joe Avatar asked Apr 04 '18 02:04

Joe


People also ask

Does importing a package imports the subpackages as well in Python?

Either Python is able to handle subpackage, either it's not, but it should not require to play with global configuration to be able to handle local stuff.

How do you access a Subpackage in Python?

Python subpackages To access subpackages, we use the dot operator. This is the __init__.py file in the constants directory. We import the names tuple. This is the data.py module in the constants directory.

What is correct way to import a Python module?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

How do I import a module file?

To use the module, you have to import it using the import keyword. The function or variables present inside the file can be used in another file by importing the module.


1 Answers

If I want to use a method in foo.bar, do I need to import foo.bar directly or is importing foo sufficient?

You'll need to import the submodule explicitly. Executing import foo.bar will automatically import the parent module foo, and necessarily bind the name foo, but the reverse is not true.

But I could have sworn I've seen code where it's not imported directly and still works fine

Yes. Sometimes accessing a submodule works without the explicit import. This happens when a parent module itself imports the submodules. Never rely on that unless it's documented, because it may be an implementation detail and could change without warning after a library version upgrade.

As an example of a popular library which demonstrates both behaviors, look at requests==2.18.4. This package has submodules called sessions and help (amongst others). Importing requests will make requests.sessions available implicitly, yet requests.help will not be available until explicitly imported. You'll find when the source code of the package init is executed that the sessions submodule gets imported, but the help submodule does not.

This makes sense, because subsequent use of foo.bar requires an attribute access on an existing foo object. Note that from foo.bar import something does not bind the name foo nor foo.bar, though both modules foo and foo.bar are imported and cached into sys.modules.

like image 193
wim Avatar answered Oct 11 '22 12:10

wim