Package Deployment

How code is packaged, deployed and reused across different projects, libraries and applications.

Python Package and Deploy


Compile

Basics of how to compile and execute a basic program and other common tools.

Error Loading

Missing

Compiling and Executing a Basic Program. Python is an interpreted language, so compilation is typically implicit. However, Python does support bytecode compilation.

python script.py # Run a Python script python -m compileall script.py # Generate .pyc bytecode file

Some common tools:

  • pyinstaller: Package Python scripts into standalone executables.
  • cython: Compile Python code to C for performance improvements.

Example of creating an executable:

pip install pyinstaller pyinstaller --onefile script.py

Modules

Create a reusable module in the local project. Manage exports and imports into a consuming module.

Missing

To creating a reusable module: - a module is a .py file containing functions, classes, or variables that can be imported elsewhere.

Example: mymodule.py

def greet(name): return f"Hello, {name}!"

To use it in another script:

import mymodule print(mymodule.greet("Alice"))

Managing Exports: - to control what gets exported:

__all__ = ["greet"]

Import Package

Add an external package to a program and import it. Upgrade to new revisions and lock to a specific version.

Missing

Installing External Packages

Python uses pip to install external packages.

pip install requests # Install package pip list # List installed packages pip show requests # Show package details

Upgrading and Version Locking:

pip install --upgrade requests # Upgrade to latest pip install requests==2.25.1 # Install a specific version pip freeze > requirements.txt # Lock dependencies pip install -r requirements.txt # Install from lock file

Publish a Package

Author and publish a package for external consumption.

Missing

Creating a Package

Structure:

my_package/ ├── mymodule.py ├── __init__.py ├── setup.py

setup.py Example:

from setuptools import setup setup( name='my_package', version='0.1', packages=['my_package'], install_requires=[], )

Publishing to PyPI

pip install twine python setup.py sdist bdist_wheel python -m twine upload dist/*

Tests

Add tests to a package that can be run as part of CI/CD.

Missing

Adding Tests

Using unittest:

import unittest from mymodule import greet class TestGreet(unittest.TestCase): def test_greet(self): self.assertEqual(greet("Alice"), "Hello, Alice!") if __name__ == "__main__": unittest.main()

Running tests:

python -m unittest discover

For CI/CD:

pytest --junitxml=results.xml

Custom Tools

Incorporate custom tools as part of the build process (e.g., OpenAPI code generation).

Missing

Integrating Code Generation (OpenAPI Example)

pip install openapi-generator-cli openapi-generator-cli generate -i api.yaml -g python -o generated_client

Custom Build Commands in setup.py

from setuptools import setup from setuptools.command.build_py import build_py class CustomBuild(build_py): def run(self): print("Running custom build step...") build_py.run(self) setup( name='my_package', version='0.1', cmdclass={'build_py': CustomBuild} )

Executing:

python setup.py build