Packages in Python

Lesson 19/20 | Study Time: 10 Min
In simple terms Python packages are collections of multiple Python files.

And these Python files are known as modules in Python. If we keep all of our code in the same file, it will result in:

  • Huge code base: Ends up messy
  • Lots of code you won't use
  • Maintenance problem

If we take an example of ourselves, we don't usually store all of our files on our computer in the same location. We use a well-organized hierarchy of directories/folders for easier access.

    Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory. Analogous to this, Python has packages for directories and modules for files.

      As our application program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages. This makes a project (program) easy to manage and conceptually clear.

      Similarly, as a directory can contain subdirectories and files, a Python package can have sub-packages and modules.

      Packages are Directory/Folder of Python Scripts.

      Where each script is a module that performs a specific function. We can specify functions, methods, types in a script.

      Thousands of packages are available in python.

      For data science, the commonly used packages are:

      • Numpy: Working with arrays
      • Matplotlib: Data Visualisation
      • Scikit-learn: ML


      Importing packages/modules

      A Python package can have sub-packages in it. Further these sub-packages have some modules (i.e. Python files).

      Each module consists of some Python functions. To make use of these functions we need to load the module in our working environment.

      To load any package or module we use the term import followed by module name or the package name.

      In the video, tutor has loaded ‘math’ module of Python and used functions like sqrt (to calculate square root of a number), pow (to calculate power of a number), etc.

      GDPR

      When you visit any of our websites, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and manage your preferences. Please note, that blocking some types of cookies may impact your experience of the site and the services we are able to offer.