Как удалить пакет 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
- Manual Removal Methods
- Prevention for Future Installations
- Handling Dependencies
- Troubleshooting Common Issues
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:
python -m pip uninstall <package-name>
On Windows, you might need:
py -m pip uninstall <package-name>
When pip works:
- Modern packages that include proper metadata
- Packages installed with pip that used
setup.pyinternally - 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 developcommand
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:
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-infodirectories - Any
.eggfiles
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:
python setup.py install --record files.txt
Then inspect files.txt and remove all listed files:
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:
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:
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):
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:
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:
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
findordircommands 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:
# 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:
- Always try
pip uninstallfirst – it works for most modern packages and is the safest method. - Use manual removal when pip fails – carefully remove files from site‑packages and clean up scripts.
- Prevent future issues by using proper installation methods and build backends.
- Be cautious with dependencies – ensure you’re not breaking other packages.
- Use the
--recordflag 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
- pip uninstall - pip documentation v25.3
- How to Uninstall Python Packages - ActiveState
- Python setup.py uninstall - Stack Overflow
- How does one remove applications installed through “python setup.py install”? - Ask Ubuntu
- How to uninstall a package using python setup.py? - GeeksforGeeks
- setup.py uninstall - Ian Kelling’s Technical Notes
- how to cleanly uninstall python packages installed with python setup.py · GitHub