Preliminary (Important)
- (Caution Note) Linux – It might not be safe to uninstall Python. Follow the instructions keenly.
- Windows – It is safe to uninstall Python completely, provided it is done correctly.
We may want to remove Python in our Linux or Windows systems for different reasons – conflicting packages and the need for a given Python version are some top reasons. Unlike in Windows, Python is a core program in most Linux distributions. In this case, uninstalling Python without utmost care may cause your Linux system to break. This article focuses on how we can safely remove Python from Windows and Linux systems.
How to Uninstall Python in Linux
Most Linux distros come with Python installed. In fact, in most cases, the default Python coming with the operating system is a core package for the system such that if it is removed, the graphical display manager breaks down, among other awful things. Other Python version(s) can also be installed alongside Linux-inbuilt Python (we will discuss how to do this later). We can safely remove the Python built from the source, but removing the Python that came with the distribution is not recommended.
To uninstall Python installed from the source, follow these steps:
Step A. First of all, check all the Python versions installed using the whereis command in Linux
From Figure 1, what we are after are the binaries. They are located at bin/or local/bin. The inbuilt Python is in bin/, whereas the Python built from the source is mainly installed at the local/bin. In our case, we have Python3.9 (inbuilt – we don’t want to uninstall this) and Python3.6 located in local/bin directory, and we can safely remove it.
The following list can fully explain the Linux directories we are interested in:
- /bin – binary or executable programs.
- /lib – it contains kernel modules and a shared library.
- /usr – user-related programs.
- The /usr/local directory is where locally compiled applications are installed by default, preventing them from mucking up the rest of the system.
- /etc – system configuration files.
Misc A: Why it is not recommended to uninstall Python at /usr/bin
Many programs in most Linux distributions depend on pre-installed Python. You can identify the default Python by running python on the terminal. You can also find it by using which command.
If we take a closer look at the long listing of /usr/bin you will realize that Python and its dependencies are linked to other programs.
1 |
cd /usr/bin && ls -la python* |
The Figure above shows that python points to python3, which is linked to python3.9 This means that running any of them wakes up Python 3.9.2 installed on the machine. Notice that the symbolic links also point to other locations containing other programs.
Since we now know why uninstalling built-in Python is not recommended, we can now see how we can remove Python we installed from the source.
Step B. Removing Python Installed from the Source in Linux
In most cases, manually installed Python will be located at the /usr/bin/local directory. It is safe to remove this Python. This can be achieved by manually removing the binary, configuration files, and all other files generated during the installation using the rm command. To do that, save the following commands on the bash file named “uninstall_python.sh” and execute it with sudo privileges.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
loc='/usr/local/' py_version="$1" rm -rf \ $HOME/.local/lib/Python${py_version} \ ${loc}bin/python${py_version} \ ${loc}bin/python${py_version}-config \ ${loc}bin/pip${py_version} \ ${loc}bin/include/python${py_version} \ ${loc}lib/libpython${py_version}.a \ ${loc}lib/python${py_version} \ ${loc}lib/pkgconfig/python-${py_version}.pc \ ${loc}lib/libpython${py_version}m.a \ ${loc}bin/python${py_version}m \ ${loc}bin/2to3-${py_version} \ ${loc}bin/python${py_version}m-config \ ${loc}bin/idle${py_version} \ ${loc}bin/pydoc${py_version} \ ${loc}bin/pyvenv-${py_version} \ ${loc}share/man/man1/python${py_version}.1 \ ${loc}include/python${py_version}m \ ${loc}bin/easy_install-${py_version} |
Execute the bash file by running:
sudo bash <location of uninstall_python.sh> <python version to remove>
In my case,
The bash script we just created removes all the files associated with Python 3.6, and now it is removed when you check it with whereis.
Uninstalling Python 2 in Linux
Note: I have never installed Python 2, but from the output above, notice that some Python2.7 libraries (/usr/lib/python2.7) exist. Other system programs probably use these libraries, so they are better not manually removed them using rm. Unless it is pre-installed, you can remove Python2.7 without any problem. You can run the following commands on the terminal to do that
1 2 |
sudo apt remove python2.7-minimal sudo apt autoremove |
Recommendations
As said earlier, we sometimes want to remove Python from our system because of conflicting packages or because we want a specific version of the programming language. You can avoid conflicting packages by running Python in a virtual environment. If you want a particular version of Python, you can install it alongside the Python coming with your Linux distribution.
Let’s discuss those two suggestions in the two coming sections.
How to Manually Install Python in Linux
Before we do, let’s check the Python version(s) installed.
As we start, I have only the pre-installed Python. It is version 3.9.
Step 0: Update repositories and install dependencies required to build Python
On Debian-based distributions, execute:
1 2 3 4 |
sudo apt update sudo apt install build-essential zlib1g-dev \ libncurses5-dev libgdbm-dev libnss3-dev \ libssl-dev libreadline-dev libffi-dev curl |
On Fedora-based systems:
1 |
sudo dnf groupinstall development |
Step 1: Download the Stable Release of Python on its Official Website
In this step, go to https://www.python.org/downloads/source/ and download XZ compressed source tarball (.tar.xz) file. This file contains all the source files we can build to get the Python we want (I am downloading Python 3.10.5, so I get, Python-3.10.5.tar.xz file).
Step 2: Extract the tarball
Use the inbuilt extraction functionality to extract the tarball, or you can use the tar command in Python as follows
tar -xf Python-****.tar.xz
(In my case, I have to run tar -xf Python-3.10.5.tar.xz)
Step 3: Run the configuration
This is accomplished by running the following command on Linux Terminal
cd Python-****/ && ./configure
That is, cd into the extracted directory and run the configure file.
Step 4: Lastly, Build the package
Since we want to install this Python version along with the pre-installed one, we will run:
sudo make altinstall
Creating Virtual Environment
A virtual environment in Python allows us to isolate the interpreter and packages from those packages installed in other virtual environments and on the default system location. It is recommended that a virtual environment should be created for each project so that package conflict is avoided.
Python provides a module venv that can allow us to make virtual environments. For example, if we want to make a virtual environment called env1 we will execute the following on the terminal.
python -m venv env1
That creates a directory called env1 with the interpreter and pre-installed libraries. To activate the environment, run
source env1/bin/activate
Once activated, the environment name will precede the terminal prompt ($). To deactivate the environment, just run
deactivate
Uninstalling Python in Windows
To completely remove Python from your windows system, we need to do three things: Uninstall Python through the Control Panel, remove pip-installed packages, and delete Python from the system path.
A. Uninstall Python through the Control Panel
- Navigate to the Control Panel > Programs > Uninstall program and uninstall the Python version you want to be removed. You can open the Control Panel by searching on the Start Menu search bar or open the Run dialog box by clicking Windows Key + R and searching “control panel”.
B. Remove pip-installed packages
- Windows keeps these packages at %LOCALAPPDATA%\pip by default; therefore, we can trash it to remove them. %LOCALAPPDATA%\ is a hidden folder located at C:\Users\<username>\AppData.
C. Next is to remove Python from the Environment.
To do that, follow these steps:
- Open the Run dialog box by Clicking Windows Key + R,
- Type sysdm.cpl and click Enter. This should open the System Properties window. Go to the Advanced tab and click on Environment Variables at the bottom.
- On the environments, select the Python paths as shown below and Delete