2

i am kind of new to Linux and i have to work with a Linux computer that has just been formatted. i am linked the this server though a SFTP. earlier before formatting this server i was using the commands 'python' and 'pip' when working with python. but now it seems like 'python' uses 2.7 and need to use 'python3' to access the python 3.7.

pretty sure earlier command 'python' used the version 3.6

pip is also another problem. pip in this refers to the 2.7 similarly and pip3 to 3.X but even if i use these commands with 3 the error to update pip confuses me.

> You are using pip version 8.1.1, however version 19.3 is available. > You should consider upgrading via the 'pip install --upgrade pip' command. 

i tried using the command

pip3 install --upgrade pip 

then it started giving me this error everytime i try to use pip.

> File "/usr/bin/pip", line 9, in <module> > from pip import main > ImportError: cannot import name main 

i dont know how to fix this or maybe i was supposed to just ignore the warning. can someone please explain me what is happening and please let me know how i can get this to work with 'python' and 'pip' again if possible.

2 Answers 2

5

So, the reason you have two python versions installed is that both are still used. You can change that however if you want to. You can make the command 'python' refer to python3 and change the command for python2 or remove python2 altogether.

Do this with caution, a lot of your programs may be using python and may mean python2 when they use the command 'python'. These programs may not run on python3 and this could break your system. Refer to this if you want to know more. Backup your files before you do anything

Commands in Linux are really just scripts that are executed when you enter them. So, the command 'python3' really refers to a script with the name 'python3' in your computer. To make it run with the command 'python', you have to simply rename the script with the name 'python3' to 'python'. To find where this script or file is stored, enter the command -

which python3 

This will tell you the location of the python3 file and then you can simply rename it to python. To do this, cd into the directory and change the name with the mv command with sudo. Example on my machine($ is the prompt and > means the output on terminal) -

$ which python3 > /usr/bin/python3 $ cd /usr/bin/ $ sudo mv python3 python 

Do the same for pip

If you want to change the command for python2, then simply follow the above procedure. If you want to remove it, which I strongly advise that you do not do because it could break a few things, then run the following command

sudo apt purge python 

Run the above command before you change the rename python3 to python.

Sign up to request clarification or add additional context in comments.

1 Comment

There are some Linux distros that might have parts that depend on Python 2 and rely on the python command to run that explicit version. I'd expect that to be rare these days with Python 2 being unsupported, but it's still a possibility to be aware of.
1

This kind of versioning issues is really common.

python3 and pip3 might be referring to different versions/installations of python. This is why it is best to use virtual environments as it ensures that everything in the virtual environment is using the same python installation.

Here is what I would suggest you to do: 1) First use the python3 installation you have to install virtualenv or any other virtual env manager. I am going to assume you are using virtualenv

2) Then you have to ensure that you use the pip that corresponds to your python3 installation to install virtualenv

python3 -m pip install virtualenv 

3) Now use virtualenv to create a new environment. Since virtualenv is installed with python3, in the environment, the python should also be python3

4) Activate the environment and use python --version and pip --version to check the version of python and pip in the environment

Everytime you are working on a new project, you should ideally create a new environment to prevent versioning issues.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.