Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python import statements

Tags:

python

I've started working on Python for about a month now and I've ran into something I would like to understand better. It's related to imports. So I have a module: root.core.connectivity

Now in this module I have defined a class Connectivity. This module also has a __main__ only for testing purposes(not sure if this makes any differences).

Now if I do:

from root.core.connectivity import Connectivity as class_name

This works fine, however if I try:

import root.core.connectivity.Connectivity as class_name

This will fail with:

ImportError: No module named Connectivity

So my question is, why does it fail and what are the differences between the two statements.

Regards, Bogdan

like image 835
Bogdan Avatar asked Apr 23 '26 19:04

Bogdan


1 Answers

import takes a module. from X import Y takes a module in X, and any element of that module in Y. Connectivity is not a module.

like image 170
LaC Avatar answered Apr 25 '26 09:04

LaC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!