2

I am running latest version of Kali Linux:

uname -a                           
Linux User 5.14.0-kali4-amd64 #1 SMP Debian 5.14.16-1kali1 (2021-11-05) x86_64 GNU/Linux

It already came with Python 3.9.8. But I needed to install Python 2.7. So I first installed it with sudo apt install python2.7.18. But following happened:

python -V        
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3

python2 -V  
Python 2.7.18

python3 -V       
Python 3.9.8

Also:

which python
python not found

WHAT I TRIED:

I checked locations such as /usr/opt/, /usr/bin/, /usr/share/ etc. I checked /usr/bin and found python2 and python3 binaries:

enter image description here

So I reckoned that python environment variable is not set. I added python=/usr/bin/python2 to /etc/environment and then did source /etc/environment. But that did not help.

Then I checked /usr/share and found that python folder Was there.

enter image description here


Then I did some research on the internet and found pyenv, which apparently allows us to install and use multiple Python versions without them conflicting with each other. So I followed all steps in the given guide to isntall Python 2.7.18 and set it as default. But that did not solve the problem either

sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git

curl https://pyenv.run | bash

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc     

echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

exec $SHELL

pyenv 2.2.3

pyenv install 2.7.18

pyenv global 2.7.18

Then I checked:

pyenv versions                                                                                                                                                                              1 ⨯
  system
* 2.7.18 (set by /home/bruno/.pyenv/version)

So far so good. But then:

python           
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3

Moreover, to confirm:

enter image description here

Shy
  • 542
  • 8
  • 20

2 Answers2

5

Because that distribution wants to be clear about which version of Python you're running, and it's perfectly allowed to do so.

In short, you'll have to use python3 to run Python 3.x, and python2 (if such a symlink gets set up by pyenv, or if you apt install python2.7 (if it's even available on Kali)) to run Python 2.x.

Please remember Python 2.x is EOL, and you shouldn't use it for any new development.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 2
    And if you want to have python3 as the default, you can symlink it yourself `sudo ln -s $(which python3) /usr/bin/python` – sshow Jan 26 '22 at 08:09
  • 2
    @sshow Or, preferably, `apt install python-is-python3` so the distribution takes care of that. – AKX Jan 26 '22 at 08:12
0

Manually adding path to /home/bruno/.pyenv/versions/2.7.18/bin to $PATH environment variable solved the python command not found for me, because that directory contains python binary.

Shy
  • 542
  • 8
  • 20
  • You're just inviting trouble doing that, if you ask me. Why do you need `python` to be Python 2 in the first place? – AKX Jan 26 '22 at 08:13
  • Because somebody handed over some scripts to me which require python2 and refer to it as python. I just need to connect to vpn using openvpn and use obfsproxy because government has banned vpn. My colleague gave me config files and scripts and did set up in Windows infront of me, and said, "You'll need Python 2 and also virtual env". I am just trying to copy their steps and get my obfsproxy and vpn up and running – Shy Jan 26 '22 at 08:18
  • It would be better and more future-proof to just change those scripts to use `python2` instead of `python`. – AKX Jan 26 '22 at 08:19