How to uninstall a package using python setup.py?
When working with Python, managing packages is a common task. Typically, package managers like pip
are used for installing and uninstalling packages. However, in some cases, you might encounter packages installed via a setup.py
script, particularly in development or custom environments. This guide will walk you through the steps to uninstall a package installed using setup.py
.
Use of Python setup.py
The setup.py
is a Python script commonly used in the context of packaging and distributing Python projects. It is the centerpiece of Python's packaging ecosystem, providing metadata about your project and instructions on how it should be installed. Here’s a detailed look at what setup.py
is and how it’s used.
Purpose of setup.py
The primary purpose of setup.py
is to describe your Python project and facilitate its distribution. When you run python setup.py
, various commands and options allow you to:
- Build distributions
- Install the package
- Register and upload the package to the Python Package Index (PyPI)
- Manage dependencies
Understanding setup.py
The setup.py
file is a Python script traditionally used for distributing Python packages. It includes instructions for how to install the package, and it can be executed with the following command:
python setup.py install
This command installs the package, typically by copying its files into Python's site-packages
directory. Unfortunately, setup.py
does not provide a built-in uninstall command. Therefore, uninstalling a package installed via setup.py
requires some manual steps.
Steps to Uninstall a Package
1. Locate the Installed Files
When a package is installed using setup.py
, its files are generally placed in the site-packages
directory within your Python environment. To locate this directory, you can use the following Python command:
import site
print(site.getsitepackages())
This will print the paths to the site package directories. Navigate to these directories to find the files related to the package you want to uninstall.
2. Identify the Package Files
Within the site-packages
directory, look for the files and directories associated with the package. These typically include:
- A directory named after the package.
- One or more
.egg
or.dist-info
directories/files. - Possibly some
.pth
files.
For example, if you installed a package named example-package
, you might see:
example_package/
(directory)example_package-1.0.0-py3.9.egg-info
(file or directory)
3. Manually Remove the Package Files
To uninstall the package, delete the identified files and directories. This can be done using the command line or a file manager. For example, using the command line:
rm -rf /path/to/site-packages/example_package
rm -rf /path/to/site-packages/example_package-1.0.0-py3.9.egg-info
Make sure to replace /path/to/site-packages/
with the actual path to your site-packages
directory.
4. Clean Up Leftover Files
Sometimes, additional files related to the package might be scattered in other directories, such as scripts in the bin
directory or configuration files. It’s a good practice to review the package's setup.py
or its documentation to identify any other potential files that need to be removed.
Using pip
for Uninstallation
If you installed the package using pip
with a local setup.py
file, you can uninstall it using pip
. This is often easier and more reliable:
pip uninstall <package-name>
For example:
pip uninstall example-package
Conclusion
Uninstalling a package installed via setup.py
requires some manual steps since setup.py
does not support an uninstall command. By locating the package files in the site-packages
directory and manually removing them, you can effectively uninstall the package. Always ensure to review any additional files that might be part of the package installation to clean your environment thoroughly.
For future installations, consider using pip
whenever possible, as it provides a more straightforward and automated way to manage package installations and removals.