Python Book
🇺🇦 Stand with Ukraine🎓Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 6. Code Styling

Packages

Package is directory with modules

Why do we need them?

  • For structuring modules for easier usage

How to create a package?

  1. Create directory

  2. Optional: Place a module

  3. That's it...

Note: In Python 2 and Python 3 before 3.5 you required to place there special (maybe empty) file __init__.py

helpers/               Top-level package
    __init__.py        Init the helpers package
    file_processors/       Subpackage
        __init__.py        Init the subpackage
        parser.py              Module from package
        saver.py               Module from package 
main.py                Main module (entry point)

In main.py we can import presented packages/modules in the following ways:

import helpers.file_processors.parser
import helpers.file_processors.parser as hfp
from helpers.file_processors import parser
from helpers.file_processors import parser, saver
from helpers.file_processors import parser as ps
from helpers.file_processors.parser import *
import helpers # IMPORT helpers.__init__.py
import helpers.file_processors # IMPORT helpers.file_processors.__init__.py

If we need to import something in package's modules - it's better to use absolute import and specify path to needed module. It will be possible because entry point is main.py and PYTHONPATH will be set accordingly.

PreviousModulesNextSources for self-learning

Last updated 2 years ago

Was this helpful?