Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alias python2 to python3 in a docker container?

I am trying to set the default python in my docker container to be python3 and have set the aliases in the dockerfile. When I open the .bashrc file, they show up. As far as I can tell, it should work but the default python version is still 2.7. if I run which python, it will still point to usr/bin/python rather than python3. Same with pip. Can anyone tell me what the problem is? Here is the command I'm using to alias:

   RUN \
   echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
   echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc

Does this look right? I am using ubuntu 17.10

like image 887
GoMonkey Avatar asked Mar 25 '19 20:03

GoMonkey


People also ask

How do I use python3 instead of python2?

If you are using Linux, add the following into into ~/. bashrc alias python=python3 Restart the shell and type python and python3 should start instead of python2.


Video Answer


2 Answers

You try to create a symlink for python bin

RUN ln -s /usr/bin/python3 /usr/bin/python & \
    ln -s /usr/bin/pip3 /usr/bin/pip

other option is use update-alternatives for more visit this site

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3

and another option is trying source the bashrc file after updating

RUN \
   echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
   echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc && \
   source /root/.bashrc

I recommend seeing all options of python images on Docker Hub

Tip: use anaconda or conda for managing your python versions (conda site)

like image 197
iddar Avatar answered Sep 19 '22 09:09

iddar


The answer above is great, except it should be as follows:

RUN ln -s /usr/bin/python3 /usr/bin/python && \
    ln -s /usr/bin/pip3 /usr/bin/pip

Perhaps they typo-ed by writing ls which just lists the contents of the directory, rather than using ln which actually creates symlinks.

like image 21
rdurrani Avatar answered Sep 21 '22 09:09

rdurrani