0

I have a sed command to change something in a file of python:

sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/local/lib/python2.7/site.py

so I want to replace "ascii" by "utf-8" but this does not work. Someone who can explain why it isn't working and what do I need to change?

>> import sys
>> reload(sys)
>> sys.getdefaultencoding();
--> "ascii"

Run my command

>> import sys
>> reload(sys)
>> sys.getdefaultencoding();
--> "ascii"
DenCowboy
  • 13,884
  • 38
  • 114
  • 210
  • This works fine to me. Post a sample file so we can double check. – fedorqui Apr 15 '16 at 08:17
  • The encoding of a file doesn't change by just changing a line on it. – fedorqui Apr 15 '16 at 08:32
  • @fedorqui this is for changing the default encoding of the installed python environment (/usr/local/lib/python-verison/sites.py). We want to change it using a dockerfile for some tests – DenCowboy Apr 15 '16 at 08:34
  • Not sure what you want to do here, MaxC. Why changing the content of a piece of the file would change its encoding? – fedorqui Apr 15 '16 at 12:59
  • @fedorqui it's a configuration file of python. When you change this in the configurationfile your default encoding type of python will change: http://stackoverflow.com/questions/36623471/persist-utf-8-as-default-encoding – DenCowboy Apr 15 '16 at 14:37
  • I'll put the sed command in my dockerfile to change this configuration of my python. – DenCowboy Apr 15 '16 at 14:37
  • OK this is totally new and unexpected to me. I reopened the question, so sorry for the hassle. Feel free to [edit] the question to include all these informations and tags like docker. – fedorqui Apr 15 '16 at 19:56
  • Are you doing it after installing Python, aren't you? Could you post your dockerfile to try to reproduce your problem? Did you try to execute an bash shell to test if that file was changed as expected? – OscarGarcia May 06 '16 at 15:59

1 Answers1

0

Are you doing it after installing Python, aren't you? Could you post your dockerfile to try to reproduce your problem? Did you try to execute an bash shell to test if that file was changed as expected?

This is my working Dockerfile example:

FROM ubuntu:14.04
MAINTAINER github/ojgarciab

# Use the "noninteractive" debconf frontend
ENV DEBIAN_FRONTEND noninteractive

# Add python
RUN apt-get update && apt-get install -y python && \
  apt-get clean

RUN sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/lib/python2.7/site.py

ADD ejemplo.py /ejemplo.py
CMD [ "python", "/ejemplo.py" ]

Before using sed command the output was:

redstar@nvidiastar:~$ docker run -t python
ascii

And now the output is:

redstar@nvidiastar:~$ docker run -t python
utf-8

The content of ejemplo.py script:

import sys
reload(sys)
print sys.getdefaultencoding();

Note that the path used was /usr/lib/python2.7/site.py and not /usr/local/lib/python2.7/site.py.

OscarGarcia
  • 1,995
  • 16
  • 17