How to import not the whole module, but its variable? Posted by:

import modulname.variable 

so:

 import modulname modulname.variable 

But it displays completely. How to do the right thing?

  • Explain what "displays completely"? - insolor
  • one
    @insolor seems like you forgot import telepathy :) - Nick Volynkin

3 answers 3

In Python, modules are always completely imported when the import command completes successfully.

Both forms: import module and from module import name import module module . The difference is that the from form also adds the name name to the current namespace.

If module.name already exists (available as an attribute), then from module import name equivalent to:

 import module name = module.name 

(except for the introduction of the module name — the module itself is loaded in both cases: you can see it in sys.modules['module'] ).

Both forms can be used, but if the module is not from the standard library, then the module.name form of module.name is preferable, since it is clearer where the name comes from.

If the module name is often found, then you can use the abbreviation for convenience, for example:

 import numpy as np import pandas as pd import matplotlib.pyplot as plt 

    Use from
    from modulname import variable
    You can also use an alias for a variable.
    from modulname import variable as var

      Using from :

       >>> from math import pi >>> pi 3.141592653589793 >>>