3

I am able to load my txt file using the line below on my local machine.

lines = open('movie_lines.txt', encoding = 'utf-8', errors = 'ignore').read().split('\n')

But this method is giving error on gcloud.

ScreenShot of the data file. enter image description here how to open this txt file on gcloud?

ERROR: TypeError: 'encoding' is an invalid keyword argument for this function

I am using google App Engine for this.

Marison
  • 149
  • 1
  • 11
  • Can you share the error message you are getting? As well, what environment are you opening the file on? (i.e. the Cloud Shell/App Engine application/Compute VM instance...) – Joan Grau Noël Jan 07 '19 at 08:25
  • @Joan i am using App engine and i have updated the question with error? – Marison Jan 07 '19 at 09:15

2 Answers2

4

You are getting the error because the default runtime environment for App Engine is Python 2.7, while you are running Python 3.x. Python 2.7 does not have an option to specify encoding in open function, hence invalid keyword error.

Check this answer to see how to open file on Python 2.7, or use the Python 3 runtime.

To use Python 3 runtime, put the following in your app.yaml:

runtime: python37

More on that you will find in GCP documentation. Python 3.x is available nowadays both in standard and flexible environments. On the differences you can read here.

Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53
  • can you give the code for use the Python 3 runtime? I tried to understand but i did not get it. – Marison Jan 07 '19 at 20:11
  • @Marison Done. Let me know if you need further clarification. – Lukasz Tracewski Jan 08 '19 at 07:57
  • @Marison Please mind that in the edit you have asked a new question. We all be glad to help you, but it's advised that we keep things separated. It's a new problem that requires some debugging. I'd advise the following: (1) accept this one as the answer (2) move your EDIT question to a new post. – Lukasz Tracewski Jan 08 '19 at 08:05
  • thank you for the advise. i have asked as a new question. if possible kindly help me with this. https://stackoverflow.com/questions/54087895/how-to-open-a-file-in-google-app-engine-using-python-3-5 – Marison Jan 08 '19 at 08:33
0

To run python3.x version, there is one more method which involves directly specifying in the arguments while starting the running .

gcloud ml-engine jobs submit training $JOB_NAME \
--job-dir $OUTPUT_PATH \
--runtime-version 1.12 \
--python-version 3.5 \
--module-name trainer.bot \
--package-path ./trainer \
--region $REGION \
-- \
--train-file $TRAIN_DATA

python version can be specified using python-version argument.

Marison
  • 149
  • 1
  • 11