Burt_Harris
Burt_Harris

Reputation: 6874

How do I remove Python installations with PowerShell Package Management?

I want to write a script which cleans up all existing versions of Python on a machine, and a separate script to re-install both versions 2.7 and 3.5 in standardized locations. I'm currently trying to do this using the Package Management cmdlets in PowerShell 5.1.14393.187.

For the cleanup script, I started with PowerShell's package commands:

Get-Package "*python*" | Uninstall-Package

Which, when run from an admin console, seems to work nicely, but on further investigation leaves some packages remaining...

PS C:\WINDOWS\system32> Get-Package "*python*"

Name                           Version          Source                           ProviderName
----                           -------          ------                           ------------
Python 3.5.1 (64-bit)          3.5.1150.0                                        Programs
Python 3.5.1 pip Bootstrap ... 3.5.1150.0                                        msi
Python 3.5.1 Tcl/Tk Support... 3.5.1150.0                                        msi
Python 2.7.11                  2.7.11150                                         msi
Python 3.5.2 pip Bootstrap ... 3.5.2150.0                                        msi
Python 3.5.2 (32-bit)          3.5.2150.0                                        Programs

Why are these packages still present after Uninstall-Package? Is there a best-practice way to do this? Is there a best-practice way to script Python's re-installation so this won't happen again?

Update

I've had some success in cleaning up the majority of this by using the control panel GUI to first repair and then uninstall the Python 3 installations. I'm surprised there is no Repair-Package command to go with Get-Package.

Once the other parts of Python 3 were repaired, there was one MSI package called "Python Launcher", and the Python 2.7 MSI package remaining reported by Get-Package, but nothing in the GUI. At this point Uninstall-Package on the "Python Launcher" succeeded with a warning that a reboot was required. No such luck with msi:Python 2.7.11/2.7.11150.


Additional information:

I think Chocolatey v0.10.1 may have contributed to the current situation. At least some of the machines may have had python installed using chocolaty from the public repository. On the same machine above, I've also tried this:

PS C:\WINDOWS\system32> choco uninstall python
Chocolatey v0.10.1
Uninstalling the following packages:
python
python is not installed. Cannot uninstall a non-existent package.

Chocolatey uninstalled 0/1 packages. 1 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Failures
 - python - python is not installed. Cannot uninstall a non-existent package.

Did you know the proceeds of Pro (and some proceeds from other
 licensed editions) go into bettering the community infrastructure?
 Your support ensures an active community, it makes you look smarter,
 plus it nets you some awesome features!
 https://chocolatey.org/compare
PS C:\WINDOWS\system32> choco uninstall python3
Chocolatey v0.10.1
Uninstalling the following packages:
python3

python3 v3.5.1
 Skipping auto uninstaller - No registry snapshot.
 python3 has been successfully uninstalled.

Chocolatey uninstalled 1/1 packages. 0 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

PS C:\WINDOWS\system32> choco uninstall python2
Chocolatey v0.10.1
Uninstalling the following packages:
python2

python2 v2.7.11
 Running auto uninstaller...
 Skipping auto uninstaller - The application appears to have been uninstalled already by other means.
 python2 has been successfully uninstalled.

Chocolatey uninstalled 1/1 packages. 0 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

PS C:\WINDOWS\system32> get-package "*python*"

Name                           Version          Source                           ProviderName
----                           -------          ------                           ------------
Python 3.5.1 (64-bit)          3.5.1150.0                                        Programs
Python 3.5.1 pip Bootstrap ... 3.5.1150.0                                        msi
Python 3.5.1 Tcl/Tk Support... 3.5.1150.0                                        msi
Python 2.7.11                  2.7.11150                                         msi
Python 3.5.2 pip Bootstrap ... 3.5.2150.0                                        msi

Upvotes: 3

Views: 19267

Answers (1)

ferventcoder
ferventcoder

Reputation: 12571

To answer this question I'm going to highlight a couple of points for you to consider.

  1. Until official support has been announced, don't use the non-official PowerShell PackageManagement provider for Chocolatey. It is an unsupported preview subject to bugs and security flaws (it was also not created by the Chocolatey team). Instead use choco.exe, or another official provider.
  2. AutoUninstaller is the resource in official Chocolatey clients that can remove natively installed software from packages that do not contain an uninstall script. It's important to note that you also need to install from those official clients. More information at https://chocolatey.org/docs/commands-uninstall

Why are these packages still present after uninstall-package?

It really depends on what you used to install the packages and whether Chocolatey was able to capture a snapshot for auto uninstaller.

Many packages do not require an uninstall script. Some do. When they are an MSI and are upgraded outside of Chocolatey (like Chrome does automatically), you either need Package Synchronizer or an uninstall script to uninstall the software.

Is there a best-practice way to do this?

If this is for organizational use, and you have a low tolerance for breakages, we recommend you build your own internal packages. Then you can completely control the process and have a repeatable, reliable process. This is how hundreds of organizations that use Chocolatey currently have enhanced their installation processes. They typically already have software installers already present on some internal file share and build packages around them to take advantage of better automation processes (versus old batch files they may have been using, or worse, manually installing from).

If you are curious on why you should build your own, see https://chocolatey.org/docs/community-packages-disclaimer (it attempts to highlight the issues with a public repository and it being subjected to distribution rights, something an internal repository is not subject to).

Is there a best-practice way to script python's re-installation so this won't happen again?

Use a configuration management tool like Puppet, Chef, Ansible, or DSC with the Chocolatey provider. https://chocolatey.org/docs/features-infrastructure-automation

This is how you create automation across all of your machines and take advantage of package management.

Upvotes: 1

Related Questions