'SyntaxError: Non-UTF-8 code starting with '\x80' in file /venv/bin/python3 Distroless
I am trying to a python flask application from a docker container which I intend to deploy in a cloud run service.
I have a python code:
app.py
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify
from simple_salesforce import Salesforce, SalesforceLogin
from simple_salesforce import SFType
import json
import os
app = Flask(__name__)
@app.route('/<sfobject>', methods=['POST'])
def update_record(sfobject):
request_json = json.loads(request.data)
# Assign Variables to Pass into Salesforce Login
sf_username = "x"
sf_password = "y"
sf_token = "z"
try:
# call salesforce Login
# return Session ID and Instance
session_id, instance = SalesforceLogin(
username = sf_username,
password = sf_password,
security_token = sf_token,
domain = "test")
print(session_id)
print(instance)
record = SFType(sfobject,session_id,instance)
#send payload to Salesforce API
record.create(request_json)
#parse response from Salesforce API
record_submit = record.describe()
print("main - record_submit: {}".format(record_submit))
except Exception as error:
print(f"Error: {repr(error)}")
return {
"statusCode" : 200,
"body" : "success"
}
# debug must be False for prod
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
Dockerfile:
# To change permissions of the scripts this image was used.
FROM busybox:1.35.0-uclibc as busybox
ENV APP_HOME=/app_config
COPY *.py $APP_HOME/
RUN chmod +x $APP_HOME/*.py
# Build a virtualenv using the appropriate Debian release
# * Install python3-venv for the built-in Python3 venv module (not installed by default)
# * Install gcc libpython3-dev to compile C Python modules
# * In the virtualenv: Update pip setuputils and wheel to support building new packages
FROM debian:11-slim AS build
RUN apt-get update && \
apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev && \
python3 -m venv /venv && \
/venv/bin/pip install --upgrade pip setuptools wheel
# Build the virtualenv as a separate step: Only re-execute this step when requirements.txt changes
FROM build AS build-venv
COPY requirements.txt /requirements.txt
RUN /venv/bin/pip install --disable-pip-version-check -r /requirements.txt
# Copy the virtualenv into a distroless image
FROM gcr.io/distroless/python3-debian11
COPY --from=build-venv /venv /venv
COPY --from=busybox:1.35.0-uclibc /bin/sh /bin/sh
ENV APP_HOME=/app_config
COPY --from=busybox $APP_HOME/*.py $APP_HOME/
# RUN pip3 install -r requirements.txt
CMD [ "/venv/bin/python3", "/app_config/app.py" ]
But when I run the docker image, I have an error:
SyntaxError: Non-UTF-8 code starting with '\x80' in file /venv/bin/python3 on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
I have referred several posts, nothing works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
