- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the packages in Python?
To understand packages, you also need to know about modules. Any Python file is a module, its name being the file's base name/module's __name__ property without the .py extension. A package is a collection of Python modules, i.e., a package is a directory of Python modules containing an additional __init__.py file. The __init__.py distinguishes a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own __init__.py file.
When you import a module or a package, the corresponding object created by Python is always of type module. This means that the distinction between module and package is just at the file system level. Note, however, when you import a package, only variables/functions/classes in the __init__.py file of that package are directly visible, not sub-packages or modules.
For example, in the DateTime module, there is a submodule called date. When you import DateTime, it won't be imported. You'll need to import it separately.
>>> import datetime >>> date.today() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'date' is not defined >>> from datetime import date >>> date.today() datetime.date(2017, 9, 1)
- What are packages in Java?
- What are Packages in Perl?
- What are various sub-packages in Python SciPy library?
- Packages in Python
- What are the best practices to keep in mind while using packages in Java?
- Add packages to Anaconda environment in Python
- Packages in Java
- Packages in C#
- How to create python namespace packages in Python 3?
- How to check which packages are loaded in R?
- Explain packages in Java
- Capacity To Ship Packages Within D Days in Python
- How to develop programs with Python Namespaced Packages?
- Types of packages in Java
- Overview of Packages in Oracle