Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Import Module in Python

Profile picture for user devanshi.srivastava
Written by devanshi.srivastava on 07/01/2021 - 20:58

Import in python is similar to #include header_file in C/C++.In Python, module can get access to code from another module by importing the file/function using import. 

Modules refer to a file containing Python statements and definitions. We can define our most used functions in a module and import it, instead of copying their definitions into different program. It is use modules to break down large programs into small manageable and organized files. It provide reusability of code.

1. import module_name

We can import the definitions inside a module to another module or the interactive interpreter in Python.

We can use any Python source file as a module by executing an import statement in some other Python source file. When the interpreter encounters an import statement, it imports the module if the module is present in the search path.

We use the import keyword to import modules in python. When the import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code.


Syntax:

import module_name

Example:

import math
print(math.pi)

Output: 3.141592653589793

2. import module_name.member_name

We can import specific names from a module without importing the module as a whole.

from math import pi
print(pi)

Output: 3.141592653589793

3. from module_name import *

We can import all names(definitions) from a module using the following construct.

from math import *
print(factorial(7))

Output: 5040

Importing everything with the asterisk (*) symbol is not a good programming practice. It can lead to duplicate definitions for an identifier. It also hampers the readability of our code.

Related Content
Python Tutorial
Python: Namespace and Scope
Python Function
Tags
Python
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use