21

My question is if you can install python with powershell, cmd, vbs or any other language built into Windows already? If this was already asked please redirect me to the answer. "How to install Python using Windows Command Prompt" explains how to install the python if you already have the exe installed, not how to install the exe.


EDIT: I am trying to install python with a file on a pc that does not have python installed, only thing restricted might be that the account does not an administrator and if possible in the background.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • 3
    Possible duplicate of [How to install Python using Windows Command Prompt](https://stackoverflow.com/questions/46056161/how-to-install-python-using-windows-command-prompt) – Stack Sep 30 '18 at 13:26
  • this requires you to have the .exe downloaded already –  Sep 30 '18 at 13:40
  • I added details –  Sep 30 '18 at 14:27
  • 2
    but --- as for .."Windows doesn't have built-in package manager like Linux distributions", Windows PowerShell does --- Find-Package -Name '*python*' or Get-Package -Name '*python*', then Install-Package -Name 'pythonversionname' --- details https://blogs.technet.microsoft.com/packagemanagement/2015/04/28/introducing-packagemanagement-in-windows-10 – postanote Sep 30 '18 at 23:15

6 Answers6

24

The best way to install Python through Windows Command Prompt will be through Chocolatey (Windows Package Manageer).

Steps to install python 3 will be as follows :-

  1. Open CMD using 'Run as Administrator'.

  2. Download and Install Chocolatey using the following command.

@"%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"
  1. Download and install python using the following command.
choco install -y python3
  1. You can check the version to verify if Python was successfully installed as follows.
python --version
BrokeMyLegBiking
  • 5,898
  • 14
  • 51
  • 66
Cyborg
  • 357
  • 5
  • 12
19

You could download the setup you want to install and then install it automatically without using the setup's UI:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe" -OutFile "c:/temp/python-3.7.0.exe"

c:/temp/python-3.7.0.exe /quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0

I don't think it will work without admin privileges though, I tried using InstallAllUsers=0 to install it only for the current user but it is still asking for elevation.

There are some options you can use when installing it this way, here is the doc: https://docs.python.org/3.6/using/windows.html#installing-without-ui

Isma
  • 14,604
  • 5
  • 37
  • 51
  • is there a way to make it not use admin –  Oct 04 '18 at 12:10
  • Maybe yes, check this question https://stackoverflow.com/questions/33876657/how-to-install-python-any-version-in-windows-when-youve-no-admin-priviledges unfortunately I didn't see any msi package in the latest Python releases so maybe they are not doing this anymore, so you need to generate the msi package yourself before installing. Also, you could download the zipped version and just copy it to a folder and update the path. – Isma Oct 04 '18 at 12:45
  • How do i setup python from the zip file or make a msi file –  Oct 04 '18 at 14:23
  • Expand-Archive YourPackage.zip -DestinationPath c:/python37 and to add your python folder to the path using Powershell, see here: https://codingbee.net/tutorials/powershell/powershell-make-a-permanent-change-to-the-path-environment-variable – Isma Oct 04 '18 at 14:26
  • The zip has another zip in it, where do i extract that and do i need to add that to the path also? –  Oct 04 '18 at 14:33
  • Works, I ran into some sys admin / privilege issues, so I did the the last step manually and "deselected" when asked to install for all users – openwonk Sep 05 '19 at 19:29
  • the setting of the SecurityProtocol was not neccessary on my machine. I would rather not set it in a script that does other things too (like setting up a build machine as in my case) – rominator007 Apr 15 '20 at 06:52
  • Note that there is one more flag you need to install without user input: InstallLauncherAllUsers. The full command will be `c:/temp/python-3.7.0.exe /quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0`. Then it will not show the popup wizard. – Chris du Plessis Mar 22 '23 at 18:01
  • Your code does not work. Are you able to provide helpful comments to assist [this related question that emerged from the failure of the code you gave in your answer here](https://stackoverflow.com/questions/76863325/install-python-from-powershell-script)? – CodeMed Aug 08 '23 at 22:34
6

You can not install it without administrator privileges. It would be lack of security I guess. What you can use in pipelines for instance is:

$url = "https://www.python.org/ftp/python/3.7.6/python-3.7.6-amd64.exe"
$output = "C:/tmp/python-3.7.6-amd64.exe"

if (Test-Path $output) {
    Write-Host "Script exists - skipping installation"
    return;
}

New-Item -ItemType Directory -Force -Path C:/tmp

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $output


& $output /passive InstallAllUsers=1 PrependPath=1 Include_test=0 

But still, Admin rights are required

J.Wincewicz
  • 849
  • 12
  • 19
1

Yes, there is a way to download python with cmd. All you gotta do is to write "python" and you will automaticly be taken to the msstore. Ive made some scripts for ya. Its done automaticly you just click on one batch file and its all done. Here ya go:

@echo off

::Checks if python is installed
:check
python --version 3>NUL
if errorlevel 1 goto errorNoPython

::If python is installed then it runs the program
start <python progam>
exit

::Installes python
:errorNoPython
python
timeout /t 60
goto check
0

Not related to the question directly, but some people may end up here looking for a different solution.

To use Python within Powershell:

  • Install Python from the Microsoft Store (an App in the programs list)
  • Open the Powershell window (can hold SHIFT and right click in the window where your python file is located 'Open Powershell Window here')
  • type 'python' a space and the name of the file Example: PS C:\Users...\Project> python test.py
  • -1 This answer is unrelated. It mostly focuses on how to run Python and not how to install it. The instructions to install it also don't answer the question about a command line install. Perhaps this would be better suited as a comment on another answer. – Adam Apr 07 '21 at 14:03
0

Probably it's correct to guess that after installing python with PowerShell, the next question will be about installing environment (and possibly dependencies) with PowerShell?

Packed multiple related answers in easy 1-click powershell-batch for this goals. Look "with end-user download" option.
If no admin rights are allowed, portable distribution "or portable usage" from the same repo may be an option.

halt9k
  • 527
  • 4
  • 13