0

I'm trying to set an alias for python to python3, and so far in .bashrc I have set the following:

.bashrc

alias python=python3

Following which, I ran: source ~/.bashrc. However, when I execute which python, it still points to /usr/bin/python, while which python3 returns /user/bin/python3.

I'm currently using bash shell, but I'm not sure why my python is not being aliased correctly to python3.

I've read in some places I need to set the alias in .bash_aliases or .bash_profile but this is my first time doing all of this so I'm a little lost. Appreciate any help!

code_learner93
  • 571
  • 5
  • 12
  • 1
    Does this answer your question? ['which' vs 'command -v' in Bash](https://stackoverflow.com/questions/37056192/which-vs-command-v-in-bash) – Marcin Orlowski Nov 28 '22 at 14:13
  • 2
    Use: `python --version` , since `which` will always print the real file/app name... – Jetchisel Nov 28 '22 at 14:16
  • 2
    `which` ignores aliases, because it is in bash an external program. You could switch to zsh, where `which` is a builtin, or stay with bash, but use `type` instead of `which`. – user1934428 Nov 28 '22 at 14:36
  • 1
    (Or more precisely, `bash` does not have a built-in `which` to shadow the external `which` command, and external commands do not have any insight to shell-specific features like aliases.) – chepner Nov 28 '22 at 15:05
  • 1
    You will almost certainly want to use a virtual environment anyway to handle project-specific dependencies, where `python` already refers to the version of Python the virtual environment uses. (Basically, it creates a symlink in the virtual environment's `bin` directory, which is added to the front of your path when you activate the virtual environment.) Aliases are probably the least desirable way to accomplish what you want. – chepner Nov 28 '22 at 15:07
  • 2
    Funny, I just pasted this link on another question on Unix&Linux: [Why not use "which"? What to use then?](https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then) You might consider `alias which="type -a"` – glenn jackman Nov 28 '22 at 15:24

1 Answers1

0

by definition, "which" will always show the full path of your shell commands, in your case, which python will show /usr/bin/python and for python3 /usr/bin/python3.SO what your system is doing is correct.

An alias definition provides a string value that replaces a command name when the command is read. The alias command does not allow you to use another known command as an alias, you are creating a conflict.

Okamiram
  • 43
  • 5
  • 2
    That second paragraph is false – ptierno Nov 28 '22 at 16:34
  • Yes, you can do it but it might lead to conflict so it is not a best practise. So it is best not to use it. Anyway, thanks for your correction. – Okamiram Jan 05 '23 at 19:16
  • Using RedHat Linux as an example (and some others) it ships with aliases like `alias rm="rm -i"`, so im not sure where it is defined as a best practice. Also, im not sure what conflict you are referring to. – ptierno Jan 06 '23 at 15:13