Skip to content

Latest commit

 

History

History
104 lines (55 loc) · 3.98 KB

tips-and-tricks-for-python-applications-b5e1c82.md

File metadata and controls

104 lines (55 loc) · 3.98 KB

Tips and Tricks for Python Applications

Get to know the Python buildpack for the SAP BTP, Cloud Foundry environment.

Check the following Cloud Foundry documentation: Python Buildpack

Sometimes, productive Cloud Foundry applications might need to be deployed as self-contained. That means, they need to carry all of their dependencies so that the staging process does not require any network calls. For more information, see: Vendor App Dependencies

Depending on the region where the application is deployed:

  • Running on the China (Shanghai) region: it is mandatory for the application to be deployed as self-contained

  • Running on the AWS, Azure, or GCP regions: it is only recommended but not mandatory for the application to be deployed as self-contained.

At some point, you might need (or decide) to deploy your application with a particular buildpack version from the community python-buildpack repository. For example, if this buildpack contains a Python version that is no longer supported by the SAP BTP, Cloud Foundry environment.

Let's say, you want to pin version 1.8.4. To do that, proceed as follows:

  1. Open the manifest.yml file of your Python application.

  2. For the buildpack attribute, add the URL to version 1.8.4, like this:

    
    ---
    applications:
    - name: myapp
      random-route: true
      buildpack: https://github.com/cloudfoundry/python-buildpack.git#v1.8.4
      memory: 128M
    
  3. Redeploy your application. Run:

    cf push myapp
    

Alternative way:

If you don't want to make changes in your manifest.yml file, you can include the buildpack version in the cf push command.

  • To deploy just a single application with this particular buildpack version, run:

    cf push myapp -b https://github.com/cloudfoundry/python-buildpack.git#v1.8.4
    
  • To pin this buildpack version for all applications running in your SAP BTP, Cloud Foundry environment subaccount, run:

    cf push -b https://github.com/cloudfoundry/python-buildpack.git#v1.8.4
    

When deploying an application in the SAP BTP, Cloud Foundry environment without specifying the application memory requirements, the Cloud Foundry controller assigns the default (1G of RAM currently) for your application. Many Python applications require less memory and assigning the default is a waste of resources.

To save memory from your quota, specify the memory size in the deployment descriptor of your Cloud Foundry application – manifest.yml. For example:


---
applications:
- name: myapp
  memory: 128M
  buildpack: python_buildpack
...
  • The cfenv package provides access to the Cloud Foundry application environment settings by parsing all the relevant environment variables. The settings are returned as a class instance. See: https://github.com/jmcarp/py-cfenv

  • When developing Python applications, it’s a good practice to use virtual environments. The most popular Python package providing such a functionality is virtualenv. See: https://virtualenv.pypa.io/en/stable/

  • The PEP 8 Style Guide for Python will help you improve your applications.