Skip to main content

Python Package

 Python Package


We don't usually store all of our files in our computer in the same location. We use a well-organized hierarchy of directories 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. Similar, as a directory can contain sub-directories and files, a Python package can have sub-packages and modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.

Importing module from a package

We can import modules from packages using the dot (.) operator. For example, if want to import the start module in the above example, it is done as follows.

import Game.Level.start

Now if this module contains a function named select_difficulty(), we must use the full name to reference it.

Game.Level.start.select_difficulty(2)

If this construct seems lengthy, we can import the module without the package prefix as follows



from Game.Level import start

We can now call the function simply as follows.

start.select_difficulty(2)

Yet another way of importing just the required function (or class or variable) form a module within a package would be as follows.

from Game.Level.start import select_difficulty

Now we can directly call this function.

select_difficulty(2)

Although easier, this method is not recommended. Using the full namespace avoids confusion and prevents two same identifier names from colliding.


Ref: Learn python:Python tutorial app

App by Avrram piperidas

Most Popular Course:Data Science of Harvard, MIT, IBM.... 

John Academy: 97% Off on Popular Online Courses



Amazon Best Seller in Baby Products

Comments

Popular posts from this blog

Python Exception | Learn python

  Python Errors and Built-in Exceptions When writing a program, we, more often than not, will encounter errors. Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error. >>> if a < 3 File " " , line 1 if a < 3 ^ SyntaxError: invalid syntax Ref: Learn python:Python tutorial app App by Avrram piperidas Most Popular Course:Data Science of Harvard, MIT, IBM....  John Academy: 97% Off on Popular Online Courses Amazon Best Seller in Home and Kitchen Amazon Best Seller in Office Products Amazon Best Seller in Baby Products We can notice here that a colon is missing in the if statement. Errors can also occur at runtime and these are called exceptions. They occur, for example, when a file we try to open does not exist (FileNotFoundError), dividing a number by zero (ZeroDivisionError), module we try to import is not found (ImportError) etc. Whenever these type of runtime error occur, Python ...

Python User-Defined Exception | Learn python

  Python Custom Exceptions Python has many built-in exceptions which forces your program to output an error when something in it goes wrong. However, sometimes you may need to create custom exceptions that serves your purpose. In Python, users can define such exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from Exception class. Most of the built-in exceptions are also derived form this class. >>> class CustomError (Exception) : ... pass ... >>> raise CustomError Traceback (most recent call last): ... __main__.CustomError >>> raise CustomError( "An error occurred" ) Traceback (most recent call last): ... __main__.CustomError: An error occurred Here, we have created a user-defined exception called CustomError which is derived from the Exception class. This new exception can be raised, like other exceptions, using the raise statement with an optional error message. When we are develo...

Python Directory | Python File Handling

  Python Directory and Files Management If there are a large number of files to handle in your Python program, you can arrange your code within different directories to make things more manageable. A directory or folder is a collection of files and sub directories. Python has the os module, which provides us with many useful methods to work with directories (and files as well). Get Current Directory We can get the present working directory using the getcwd() method. This method returns the current working directory in the form of a string. We can also use the getcwdb() method to get it as bytes object. >>> import os >>> os.getcwd() 'C:\\Program Files\\PyScripter' >>> os.getcwdb() b'C:\\Program Files\\PyScripter' The extra backslash implies escape sequence. The print() function will render this properly. >>> print(os.getcwd()) C:\Program Files\PyScripter Changing Directory We can change the current working directory using the chdir...