27

I have python2.6.6 installed in my system. Now I am trying to use python3 while keeping all the python2.6.6 untouched in /usr/bin/. I am able to install python3 in /opt/python35/bin/. By adding export PATH=/opt/python35/bin/:$PATH to the ~/.bashrc file, I am able to access python3 anywhere in my console.

My question is: how could I set an alias (python) for python3 so that, whenever I issue command "python", python3 in /opt/python35/bin/ could be used? I simply couldn't remove python2.6.6 in my system due to some already installed programs in my system.

My current approach is to add a line in the ~/.bashrc file, alias python = "/opt/python35/bin/python3" or simply alias python = "python3". However, when I reload this ~/.bashrc file, I got the following error:

$ source ~/.bash_profile
bash: alias: python: not found
bash: alias: =: not found
bash: alias: /opt/python35/bin/python3: not found

Does anybody know where my problem is? Thanks in advance!

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Cindy
  • 489
  • 1
  • 4
  • 11
  • 4
    One caveat to doing this. Some of the apps on your system that have a dependency on 2.6.6 may just be referencing `python`. Aliasing `python` to `python35` may end up breaking these apps. IMO you're better-off just specifying `python3` when you want to specifically use Python 3.5. You can use this in your shebang line without any issues. – Deacon Feb 16 '16 at 15:19

7 Answers7

58

Spaces are not allowed next to the equal sign when declaring aliases.

Use

alias python=python3

and it should work.

neuhaus
  • 3,886
  • 1
  • 10
  • 27
  • 4
    Put it in your `.bashrc`, otherwise it will only be parsed for login shells – neuhaus Feb 17 '16 at 08:49
  • 4
    You might also wanna do `alias pip=pip3` – Philipp Sep 29 '19 at 15:12
  • This has no effect for me. which python shows /home/user/.local/pypoetry... alias python=[someotherpython] doesn't change it. Resourcing ". ~/.bashrc" doesn't have any effect either. Any suggestions? – illan Feb 28 '23 at 22:59
  • @illan perhaps you are not using bash? macOS these days uses zsh by default. – neuhaus Jun 01 '23 at 08:01
16

in your .bashrc

add

alias python='python3'

In bash script , you can't insert space beside = .

KIDJourney
  • 1,200
  • 1
  • 9
  • 22
  • I couldn't make this work - it should, and I don't know why not. But creating the "missing" python file, containing the statement `python3`, did work. – malcanso May 30 '17 at 00:36
  • This allows using another python, like python3.11, rather than python3 – alercelik Mar 17 '23 at 08:37
6

You can use:

apt install python-is-python3

4b0
  • 21,981
  • 30
  • 95
  • 142
Pablo
  • 79
  • 1
  • 3
4

An alias is nice an works great for a single User.

Sometime you need it for all Users Systemwide. Then create a symlink to point the /usr/bin/python command at the current default python3

cd /usr/bin and sudo ln -s python3 /usr/bin/python

You can check the result with ls -la python* and python --version

2

You can use echo alias python=python3 >> .zshrc and echo alias pip=pip3 >> .zshrc. If you use bash instead of zsh then just replace .zshrc with .bashrc. If using an older version of ubuntu, you might have to install and pip3.

Aarav Garg
  • 21
  • 1
  • 5
0

You can create a python virtual environment in the your project folder:

python3.10 -m venv .venv

Instead of 3.10 place your python version. Then activate it:

source .venv/bin/activate

Then create a symlink for python as You wish:

ln -s .venv/bin/python .venv/bin/pluton

Then You can run python under desired name:

pluton my_program.py
Gleb
  • 1
-1

Or, if you like, append these inside bash_profile:

python(){
python3
}
double-beep
  • 5,031
  • 17
  • 33
  • 41