Другое

Как удалить пакет Python, установленный через setup.py

Узнайте, как полностью удалить пакет Python, установленный командой `python setup.py install`. Пошаговые инструкции, команды pip и ручное удаление файлов.

Как удалить пакет Python, установленный командой python setup.py install?

Я установил пакет Python, используя команду python setup.py install. Как правильно удалить этот пакет? Есть ли специальные команды или инструменты, которые я должен использовать, чтобы полностью удалить пакет и его зависимости?

The proper way to uninstall a Python package installed with python setup.py install is to use pip uninstall <package_name>, though this may not work for all pure distutils packages. For packages that pip cannot uninstall, you’ll need to manually remove the files from your Python site‑packages directory and clean up any scripts or entry points.

Contents

Using pip uninstall

The most straightforward approach is to use pip’s uninstall command, which works for many packages installed via setup.py install. According to pip documentation, pip can uninstall most packages, though there are some limitations.

Command syntax:

bash
python -m pip uninstall <package-name>

On Windows, you might need:

bash
py -m pip uninstall <package-name>

When pip works:

  • Modern packages that include proper metadata
  • Packages installed with pip that used setup.py internally
  • Any package that left uninstallable metadata

When pip may not work:

  • Pure distutils packages installed with python setup.py install
  • Packages that don’t provide metadata indicating which files were installed
  • Script wrappers installed by setup.py develop command

As ActiveState’s guide explains, “Unfortunately, there is no python setup.py uninstall command. To uninstall a package installed with setup.py, use the pip command.”


Manual Removal Methods

When pip cannot uninstall the package, you’ll need to manually remove it. Here are the steps:

1. Locate the package files

Find where your packages are installed:

bash
python -c "import site; print(site.getsitepackages())"

2. Remove package files from site‑packages

Navigate to your site‑packages directory and remove:

  • The package directory (usually named <package-name>-<version>)
  • Any .egg-info directories
  • Any .egg files

3. Remove entry points and scripts

Check for installed scripts in:

  • /usr/local/bin/ (Linux/macOS)
  • C:\PythonXX\Scripts\ (Windows)
  • Your Python installation’s Scripts directory

4. Alternative method using --record during installation

If you anticipate needing to uninstall later, use the --record flag during installation:

bash
python setup.py install --record files.txt

Then inspect files.txt and remove all listed files:

bash
cat files.txt | xargs rm -f

This method, as described on Ian Kelling’s technical notes, allows you to track exactly what files were installed.


Prevention for Future Installations

To avoid uninstallation issues in the future, follow these best practices:

Always use pip when possible

According to Ask Ubuntu, “You should always install Python apps with pip. Pip supports the uninstall option.”

Use editable installations for development

For development packages, use:

bash
pip install -e .

This creates an editable installation that’s easier to manage.

Use build backends

Modern Python packaging uses build backends like setuptools or flit that generate proper metadata for pip to work with.


Handling Dependencies

When uninstalling a package, you need to consider its dependencies:

Checking dependencies

Before uninstalling, check what depends on the package:

bash
pip show <package-name>

Managing dependencies carefully

As GeeksforGeeks notes, “Always make sure the dependent packages are not dependencies for other packages you still have installed.”

If you need to force uninstall dependencies (use with caution):

bash
pip uninstall <package-name> -y

Troubleshooting Common Issues

Permission errors

If you encounter permission errors, use sudo on Linux/macOS or run as administrator on Windows:

bash
sudo python -m pip uninstall <package-name>

Package not found

If pip says the package isn’t found despite being installed:

  • Check if it’s installed system‑wide vs. user‑wide
  • Try uninstalling from user packages:
bash
pip uninstall --user <package-name>

Leftover files

If files remain after uninstall:

  • Manually search for package files in site‑packages
  • Check for hidden files or directories
  • Use find or dir commands with appropriate search terms

As Stack Overflow explains, “From here you can remove the package which was troubling me.”

Multiple Python versions

If you have multiple Python installations, ensure you’re uninstalling from the correct environment:

bash
# For a specific Python version
python3.9 -m pip uninstall <package-name>

Conclusion

Uninstalling Python packages installed with setup.py install can be challenging, but most issues can be resolved with the right approach. The key takeaways are:

  1. Always try pip uninstall first – it works for most modern packages and is the safest method.
  2. Use manual removal when pip fails – carefully remove files from site‑packages and clean up scripts.
  3. Prevent future issues by using proper installation methods and build backends.
  4. Be cautious with dependencies – ensure you’re not breaking other packages.
  5. Use the --record flag if you anticipate needing to uninstall later.

For persistent issues, consider checking the package’s documentation or contacting the maintainers for specific uninstall instructions.

Sources

  1. pip uninstall - pip documentation v25.3
  2. How to Uninstall Python Packages - ActiveState
  3. Python setup.py uninstall - Stack Overflow
  4. How does one remove applications installed through “python setup.py install”? - Ask Ubuntu
  5. How to uninstall a package using python setup.py? - GeeksforGeeks
  6. setup.py uninstall - Ian Kelling’s Technical Notes
  7. how to cleanly uninstall python packages installed with python setup.py · GitHub
Авторы
Проверено модерацией
Модерация