⊗pyPmMdMP 84 of 128 menu

Importing Part of a Module in Python

The method described in the previous lessons imports the entire contents of the module. However, you may need to use only part of the code from the module, for example, several functions. To do this, you need to change the syntax of the construction used for import. First, the keyword from is written. To the right of it, the name of the module is indicated. Then the command import is written, to the right of which the necessary functions are listed:

from module import required functions

Let's say our lib module has the following functions:

def func1(): print(1) def func2(): print(2) def func3(): print(3)

Let's import just func1 and func2:

from lib import func1, func2

This design is convenient because when calling a function, you do not need to specify the name of the imported module:

func1() func2()

After executing the code, the results of these functions will be displayed in the console:

1 2

Let's try calling func3:

func3()

After running the code, an error will be displayed in the console:

NameError: name 'func3' is not defined. Did you mean: 'func1'?

Create a module custom_math that will contain the following functions: get_sum for adding two numbers, get_divide for dividing two numbers, get_cube for cubed number. Import only the get_divide and get_cube functions into the working file.

The following variables are given in the user module:

name = 'user1' email = 'user1@mail.com' password = 'qwerty'

Import user name and email from this module. Output them to console.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline