Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.

Have an account? Sign In

Captcha Click on image to update the captcha.

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Tech Answered Logo Tech Answered Logo
Sign InSign Up

Tech Answered

Tech Answered Navigation

  • Home
  • Tutorials
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us
Home / Questions /Q 3850
In Process
Himasree
Himasree

Himasree

  • India
  • 10 Questions
  • 11 Answers
  • 0 Best Answers
  • 53 Points
View Profile
  • 0
Himasree
Asked: January 12, 20222022-01-12T18:22:57+05:30 2022-01-12T18:22:57+05:30In: Python

What is the difference between modules and packages in Python?

  • 0

Hello, I have recently taken up python in my engineering.., python being one of the most trusted and preferred languages on desktop and being one of the high-level programming languages I now want to write a code for the Fibonacci series using modules and packages but I am not clear about the difference between modules and packages the same..? Can you elaborate on it? Thank you.

how many modules in python?modules and packages in pythonmodules in pythonmodules vs packages in pythonpackage library in pyhtonpython modules and packages listpython modules vs python packageswhat is module library and package in python?
  • 1
  • 1,463
  • 0
  • 0
Answer
Share
  • Facebook

    Related Questions

    • how to remove indentation error in python
    • what is the difference between list and tuple in python
    • Python Program To Print Stars (*) In Different Patterns?
    • What Is The Python Program To Print The Calendar Of A Particular Month?
    • Write A Python Program To Accept A File Name From The User And Print The Extension Of The File Name.
    • Python Program To Count The Number Of Vowels In A Sentence?
    • Addition of two numbers in Python using user input example program

    1 Answer

    1. Roshan

      Roshan

      • India
      • 6 Questions
      • 5 Answers
      • 0 Best Answers
      • 36 Points
      View Profile
      Roshan
      2022-01-13T14:58:37+05:30Added an answer on January 13, 2022 at 2:58 pm

      A Python package is a collection of Python modules and sub-packages. Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file. A module is a single python file that contains a set of functions, classes, or variables defined and implemented. Actually, it’s a good idea to split code into smaller modules.  Python package is like a directory holding sub-packages and modules. There is a Python Package Index. (PyPI). We can also create our own packages.

      Python module/package names should generally follow the following constraints:

      • Allow lower case
      • Unique on pypi, even if you don’t want to make your package publicly available.
      • Underscore separated or no word separators at all(don’t use hyphens).

      Let us see how packages and modules of Python are interrelated:

      we store our files in organized hierarchies. We don’t store them all in one location. Likewise, when our programs grow, we divide them into packages. In real-life projects, programs are much larger, package lets us hold similar modules in one place. Like a directory may contain subdirectories and files, a package may contain sub-packages and modules. But what distinguishes a package from a regular directory?

      Well, a Python package must have an __init__.py file in the directory. You may leave it empty, or you may store the initialization code in it. But if your directory does not have an __init__.py file, it isn’t a package; it is just a directory with a bunch of Python scripts. Leaving __init__.py empty is indeed good practice.

      Packages are namespaces that contain multiple packages and modules themselves. They are simply directories, but with a twist.

      Each package and subpackage in Python is a directory that MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported. This will define what gets brought into the namespace with the import statement.

      If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the __init__.py file inside the foo directory.

      To use the module bar, we can import it in two ways:

      import foo.bar   or  from foo import bar
      In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don’t, because we import the module to our module’s namespace.

      Sample Package module structure:

      Here, the root package is Game. It has sub-packages Sound, Image, Level, and file __init__.py. Sound further has modules load, play, and pause, apart from file init.py. Image package has modules open, change, and close, apart from __init__.py. Finally, Level package has modules start, load, and over, apart from __init__.py.

      An example python package

      Python packages come in a variety of structures, but let’s create a simple demo one here that we can use in all the examples.

      /src
      /example_pkg
      __init__.py
      foo.py
      bar.py
      baz.py
      setup.py
      README.md
      LICENSE
      It is composed of three modules: foo.py, bar.py, and baz.py, each of which has a single function that prints the name of the module where the function resides.

      This is some information about the python module and package. Hope this information might help you to get an idea of python modules and packages.

      You may also read:

      Python Program To Count The Number Of Vowels In A Sentence 

      • 3
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Sidebar

    Ask A Question

    More Questions

    • Swathi

      how to remove indentation error in python

    • Swathi

      what is the difference between list and tuple in python

    • Vipinkv

      Python Program To Print Stars (*) In Different Patterns?

    • Vipinkv

      What Is The Python Program To Print The Calendar Of

    • Krishna Vijayan

      Write A Python Program To Accept A File Name From

    • Vipinkv

      Python Program To Count The Number Of Vowels In A

    • arunraj503

      Addition of two numbers in Python using user input example

    • sita

      how to print complex numbers in python with example program

    Stats

    • Questions 1,989
    • Answers 2,095
    • Posts 12
    • Best Answers 73

    Related Questions

    • Swathi

      how to remove indentation error in python

    • Swathi

      what is the difference between list and tuple in python

    • Vipinkv

      Python Program To Print Stars (*) In Different Patterns?

    • Vipinkv

      What Is The Python Program To Print The Calendar Of A Particular

    • Krishna Vijayan

      Write A Python Program To Accept A File Name From The User

    • Vipinkv

      Python Program To Count The Number Of Vowels In A Sentence?

    • arunraj503

      Addition of two numbers in Python using user input example program

    • sita

      how to print complex numbers in python with example program

    Explore

    • Home
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Recent Comments

    • shree on How to use philips citrus press juicer 25 watts Demo
    • kiran kumar85 on How To Insert Names Automatically in Excel Sheet With VBA Code
    • Satish on How To Insert Names Automatically in Excel Sheet With VBA Code
    • jameslaurence on alarm won’t go off while on facetime how to fix
    • jameslaurence on how to solve export problem in kinemaster
    Ask A Question

    Archives

    Tags

    laptop buying guide (1) limit google news notifications (1) printer buying guide (1) stop google news notifications (1) turn off google news notifications (1)

    Social media

    © 2023 @ Techanswered.com.
    Terms, Privacy policy & Sitemap.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.