Menginstal / Meningkatkan Python Menggunakan Chocolatey Windows Package Manager
Katakanlah Anda memiliki Python 2.7.16:
C:\Windows\system32>python --version
python2 2.7.16
... dan Anda ingin meningkatkan ke versi 3.xy (sekarang saat ini). Ada cara sederhana untuk menginstal instalasi paralel Python 3.xy menggunakan alat manajemen paket Windows.
Sekarang Windows modern memiliki manajemen paket, seperti distribusi Debian Linux memiliki apt-get, dan RedHat memiliki dnf: kita dapat membuatnya bekerja untuk kita! Ini disebut Chocolatey .
Apa itu Chocolatey?
Chocolatey adalah alat baris perintah skrip yang didasarkan pada .NET 4.0 dan manajer paket nuget dimasukkan ke dalam Visual Studio.
Jika Anda ingin mempelajari tentang Chocolatey dan mengapa menggunakannya, yang menurut beberapa bacaan ini mungkin berguna, buka https://chocolatey.org/docs/why
Memasang Chocolatey
Untuk mendapatkan Chocolatey Package Manager, Anda mengikuti proses yang dijelaskan di https://chocolatey.org/docs/installation#installing-chocolatey ,
Saya akan meringkasnya untuk Anda di sini. Pada dasarnya ada dua opsi: menggunakan prompt cmd, atau menggunakan prompt PowerShell.
Instalasi CMD Prompt Chocolatey
Luncurkan prompt perintah administratif . Di Windows 10, untuk melakukan ini:
- Windows+R
- Ketik cmd
- Tekan ctrl+ shift+Enter
Jika Anda tidak memiliki hak administrator di sistem, buka situs web Chocolatey . Anda mungkin tidak sepenuhnya kurang beruntung dan dapat melakukan pemasangan lokal terbatas, tetapi saya tidak akan membahasnya di sini.
- Salin string di bawah ini ke dalam command prompt Anda dan ketik Enter:
@"%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 akan diunduh dan dipasang untuk Anda seperti di bawah ini:
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
Entah Keluar CMD cepat atau ketik perintah berikut untuk reload variabel lingkungan:
refreshenv
Instalasi PowerShell Chocolatey
Jika Anda lebih suka PowerShell daripada perintah cmd, Anda dapat melakukannya langsung dari sana, namun Anda harus memberi tahu PowerShell untuk menjalankan kebijakan eksekusi skrip yang tepat agar dapat berfungsi. Pada Windows 10, cara paling sederhana yang saya temukan untuk melakukan ini adalah dengan mengetik berikut ini ke dalam bilah pencarian Cortana di sebelah tombol Windows:
PowerShell.exe
Selanjutnya, klik kanan pada pilihan 'Best Match' di menu yang muncul dan pilih 'Run as Administrator'
Sekarang Anda berada di PowerShell, semoga berjalan dengan hak istimewa Administrator, jalankan yang berikut ini untuk menginstal Chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
PowerShell akan mengunduh Chocolatey untuk Anda dan meluncurkan penginstalan. Hanya perlu beberapa saat. Ini terlihat persis seperti instalasi CMD, mungkin menyimpan beberapa teks berwarna.
Entah Keluar PowerShell atau ketik perintah berikut untuk reload variabel lingkungan:
refreshenv
Mengupgrade Python
Perintah choco sama apakah Anda menggunakan PowerShell atau prompt cmd. Luncurkan favorit Anda menggunakan petunjuk seperti di atas. Saya akan menggunakan prompt cmd administrator:
C:\WINDOWS\system32>choco upgrade python -y
Pada dasarnya, chocolatey akan memberi tahu Anda "Hei, Python belum diinstal" karena Anda berasal dari 2.7.x dan memperlakukan versi 2.7 sebagai benar - benar terpisah. Ini hanya akan memberi Anda versi terbaru, 3.xy (saat tulisan ini dibuat, 3.7.2, tetapi itu akan berubah dalam beberapa bulan):
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).
Entah keluar dari cmd / Powershell prompt dan masukkan kembali, atau penggunaan refreshenv kemudian ketik 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
Perhatikan bahwa penginstalan Python terbaru sekarang akan mengambil alih saat Anda mengetik Python di baris perintah. Anda dapat menjalankan salah satu versi dengan menggunakan perintah berikut:
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:\>
Dari sini saya sarankan Anda menggunakan utilitas pip Python untuk menginstal paket apa pun yang Anda butuhkan. Misalnya, Anda ingin menginstal Flask. Perintah di bawah pip upgrade pertama, lalu instal 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
... akan melakukan triknya. Selamat Pythoning!