Installation / mise à niveau de Python à l'aide du gestionnaire de packages Chocolatey Windows
Disons que vous avez Python 2.7.16:
C:\Windows\system32>python --version
python2 2.7.16
... et vous souhaitez passer à la version 3.xy (désormais actuelle). Il existe un moyen simple d'installer une installation parallèle de Python 3.xy à l'aide d'un outil de gestion de packages Windows.
Maintenant que Windows moderne a la gestion des paquets, tout comme les distributions Debian Linux ont apt-get, et RedHat a dnf: nous pouvons le faire fonctionner pour nous! Ça s'appelle Chocolatey .
Qu'est-ce que chocolaté?
Chocolatey est un outil de ligne de commande scriptable basé sur .NET 4.0 et le gestionnaire de packages nuget intégré à Visual Studio.
Si vous voulez en savoir plus sur Chocolatey et pourquoi l'utiliser, ce que certains lisant ici pourraient trouver particulièrement utile, rendez-vous sur https://chocolatey.org/docs/why
Installation de Chocolatey
Pour obtenir le gestionnaire de packages Chocolatey, vous suivez un processus qui est décrit sur https://chocolatey.org/docs/installation#installing-chocolatey ,
Je vais vous le résumer ici. Il existe essentiellement deux options: utiliser l'invite cmd ou utiliser l'invite PowerShell.
Installation de CMD Prompt Chocolatey
Lancez une invite de commande administrative . Sur Windows 10, pour ce faire:
- Windows+R
- Tapez cmd
- Appuyez sur ctrl+ shift+Enter
Si vous ne disposez pas des droits d'administrateur sur le système, accédez au site Web de Chocolatey . Vous n'êtes peut-être pas complètement malchanceux et pouvez effectuer une installation locale limitée, mais je ne parlerai pas de cela ici.
- Copiez la chaîne ci-dessous dans votre invite de commande et tapez Entrée:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Chocolatey sera téléchargé et installé pour vous comme ci-dessous:
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.11.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\blahblahblah\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\blahblahblah\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
(i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.
Creating Chocolatey folders if they do not already exist.
WARNING: You can safely ignore errors related to missing log files when
upgrading from a version of Chocolatey less than 0.9.9.
'Batch file could not be found' is also safe to ignore.
'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at 'C:\Users\blahblahblah\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
Soit Quitter l'invite CMD ou tapez la commande suivante pour recharger les variables d'environnement:
refreshenv
Installation de PowerShell Chocolatey
Si vous préférez PowerShell à l'invite cmd, vous pouvez le faire directement à partir de là, mais vous devrez dire à PowerShell de s'exécuter avec une stratégie d'exécution de script appropriée pour le faire fonctionner. Sur Windows 10, le moyen le plus simple que j'ai trouvé de le faire est de taper ce qui suit dans la barre de recherche Cortana à côté du bouton Windows:
PowerShell.exe
Ensuite, faites un clic droit sur le choix 'Best Match' dans le menu qui apparaît et sélectionnez 'Exécuter en tant qu'administrateur'
Maintenant que vous êtes dans PowerShell et que vous utilisez, espérons-le, les privilèges d'administrateur, exécutez ce qui suit pour installer Chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
PowerShell téléchargera Chocolatey pour vous et lancera l'installation. Cela ne prend que quelques instants. Cela ressemble exactement à l'installation de CMD, sauf peut-être un texte de couleur fantaisie.
Soit Quitter PowerShell ou tapez la commande suivante pour recharger les variables d'environnement:
refreshenv
Mettre à niveau Python
La commande choco est la même que vous utilisiez PowerShell ou l'invite cmd. Lancez votre favori en suivant les instructions ci-dessus. J'utiliserai l'invite cmd de l'administrateur:
C:\WINDOWS\system32>choco upgrade python -y
Essentiellement, chocolatey vous dira "Hey, Python n'est pas installé" puisque vous venez de 2.7.x et il traite la version 2.7 comme complètement séparée. Il ne vous donnera que la version la plus récente, 3.xy (au moment de la rédaction de cet article, 3.7.2, mais cela changera dans quelques mois):
Chocolatey v0.10.11
Upgrading the following packages:
python
By upgrading you accept licenses for the packages.
python is not installed. Installing...
python3 v3.x.y [Approved]
python3 package files upgrade completed. Performing other installation steps.
Installing 64-bit python3...
python3 has been installed.
Installed to: 'C:\Python37'
python3 can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
see the changes (or in powershell/cmd.exe just type `refreshenv`).
The upgrade of python3 was successful.
Software installed as 'exe', install location is likely default.
python v3.x.y [Approved]
python package files upgrade completed. Performing other installation steps.
The upgrade of python was successful.
Software install location not explicitly set, could be in package or
default install location if installer.
Chocolatey upgraded 2/2 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Soit sortir de la cmd / Powershell rapide et entrez de nouveau, ou l' utilisation refreshenv puis tapez py --version
C:\Windows\System32>refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
C:\Windows\system32>py --version
Python 3.7.2
Notez que l'installation la plus récente de Python prendra désormais le relais lorsque vous tapez Python sur la ligne de commande. Vous pouvez exécuter l'une ou l'autre version à l'aide des commandes suivantes:
py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\>py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>exit()
C:\>
À partir de là, je vous suggère d'utiliser l' utilitaire Python pip pour installer les packages dont vous avez besoin. Par exemple, disons que vous vouliez installer Flask. Les commandes ci-dessous mettent d'abord à niveau pip, puis installez Flask
C:\>py -3 -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 1.6MB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
c:\>py -3 -m pip install Flask
... fera l'affaire. Joyeux Python!