Home

How to View Package Content in Python

|
|  Updated:  
2022-08-16 19:58:19
Python Essentials For Dummies
Explore Book
Buy On Amazon
Python gives you several different ways to view package content. The method that most developers use is to work with the dir() function, which tells you about the attributes that the package provides.

Function attributes are automatically generated by Python for you. These attributes perform the following tasks or contain the following information:

  • __builtins__: Contains a listing of all the built-in attributes that are accessible from the package. Python adds these attributes automatically for you.

  • __cached__: Tells you the name and location of the cached file that is associated with the package. The location information (path) is relative to the current Python directory.

  • __doc__: Outputs help information for the package, assuming that you’ve actually filled it in. For example, if you type os.__doc__ and press Enter, Python will output the help information associated with the os library.

  • __file__: Tells you the name and location of the package. The location information (path) is relative to the current Python directory.

  • __initializing__: Determines whether the package is in the process of initializing itself. Normally this attribute returns a value of False. This attribute is useful when you need to wait until one package is done loading before you import another package that depends on it.

  • __loader__: Outputs the loader information for this package. The loader is a piece of software that gets the package and puts it into memory so that Python can use it. This is one attribute you rarely (if ever) use.

  • __name__: Tells you just the name of the package.

  • __package__: This attribute is used internally by the import system to make it easier to load and manage packages. You don’t need to worry about this particular attribute.

It may surprise you to find that you can drill down even further into the attributes. Type dir(MyLibrary.SayHello) and press Enter.

Viewing package content in Python

Some of these entries, such as __name__, also appeared in the package listing. However, you might be curious about some of the other entries. For example, you might want to know what __sizeof__ is all about. One way to get additional information is to type help(“__sizeof__”) and press Enter. You see some scanty (but useful) help information.

Attribute information in Python

Python isn’t going to blow up if you try the attribute. Even if the shell does experience problems, you can always start a new one. So, another way to check out a package is to simply try the attributes. For example, if you type MyLibrary.SayHello.__sizeof__( ) and press Enter, you see the size of the SayHello() function in bytes.

Using attributes in Python

Unlike many other programming languages, Python also makes the source code for its native language libraries available. For example, when you look into the Python33Lib directory, you see a listing of .py files that you can open in IDLE with no problem at all.

Viewing package code in Python

Viewing the content directly can help you discover new programming techniques and better understand how the library works. The more time you spend working with Python, the better you’ll become at using it to build interesting applications.

Make sure that you just look at the library code and don’t accidentally change it. If you accidentally change the code, your applications can stop working. Worse yet, you can introduce subtle bugs into your application that will appear only on your system and nowhere else. Always exercise care when working with library code.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.