Adding python exe to path

Adding Python to PATH on Windows

I’ve been trying to add the Python path to the command line on Windows, yet no matter the method I try, nothing seems to work. I’ve used the set command, I’ve tried adding it through the Edit Environment Variables prompt, etc. Furthermore, if I run the set command on the command line it lists this.

Yet it still doesn’t recognize the Python command. Reading the documentation, and various other sources haven’t seemed to help. Just to clarify further, I’ve appended the path of the Python executable to PATH in the Edit Environment prompt. Doesn’t seem to work.

Whilst not valid to you, with the Python 3.6 Windows Installer (and potentially earlier versions) you can choose to «Customise» your installation and there is a checkbox to add Python to your path.

22 Answers 22

  1. Hold Win and press Pause .
  2. Click Advanced System Settings.
  3. Click Environment Variables.
  4. Append ;C:\python27 to the Path variable.
  5. Restart Command Prompt.

@rogerklutz: Just make sure you’re adding «;C:\python27» to the PATH variable that already exists, and not creating a new variable with «C:\python27» as the value.

When setting Environmental Variables in Windows, I have gone wrong on many, many occasions. I thought I should share a few of my past mistakes here hoping that it might help someone. (These apply to all Environmental Variables, not just when setting Python Path)

Watch out for these possible mistakes:

  1. Kill and reopen your shell window: Once you make a change to the ENVIRONMENTAL Variables, you have to restart the window you are testing it on.
  2. NO SPACES when setting the Variables. Make sure that you are adding the ;C:\Python27 WITHOUT any spaces. (It is common to try C:\SomeOther; C:\Python27 That space (␣) after the semicolon is not okay.)
  3. USE A BACKWARD SLASH when spelling out your full path. You will see forward slashes when you try echo $PATH but only backward slashes have worked for me.
  4. DO NOT ADD a final backslash. Only C:\Python27 NOT C:\Python27\
Читайте также:  Error java source option 5 is no longer supported use 6 or later intellij idea

The reason that I chose this answer is that most of the time the PATH get’s broken because of one of these 4 things. The easy part is doing it correctly, the hard part is noticing when you did it incorrectly!

The command echo $path is only valid if you’re running a bash shell under windows (e.g. MinGW). The windows command prompt command is echo %path%

Open cmd.exe with administrator privileges (right click on app). Then type:

Remember to end with a semi-colon and don’t include a trailing slash.

+1- This lets you add to the path without needing admin privileges. However, I am not sure the %path% is needed. On my Windows 7 system, new cmd windows now have two copies of the previous paths.

%path% is your old path variable value, it is there so «C:\Python27;» will be appended to your existing path.

I’ve had a problem with this for a LONG time. I added it to my path in every way I could think of but here’s what finally worked for me:

  1. Right click on «My computer»
  2. Click «Properties»
  3. Click «Advanced system settings» in the side panel
  4. Click «Environment Variables»
  5. Click the «New» below system variables
  6. in name enter pythonexe (or anything you want)
  7. in value enter the path to your python (example: C:\Python32\ )
  8. Now edit the Path variable (in the system part) and add %pythonexe%; to the end of what’s already there

IDK why this works but it did for me.

then try typing «python» into your command line and it should work!

Lately I’ve been using this program which seems to work pretty well. There’s also this one which looks pretty good too, although I’ve never tried it.

Try adding this python.bat file to System32 folder and the command line will now run python when you type in python

You can set the path from the current cmd window using the PATH = command. That will only add it for the current cmd instance. if you want to add it permanently, you should add it to system variables. (Computer > Advanced System Settings > Environment Variables)

You would goto your cmd instance, and put in PATH=C:/Python27/;%PATH% .

Make sure you don’t add a space before the new directory.

The following program will add the python executable path and the subdir Scripts (which is where e.g. pip and easy_install are installed) to your environment. It finds the path to the python executable from the registry key binding the .py extension. It will remove old python paths in your environment. Works with XP (and probably Vista) as well. It only uses modules that come with the basic windows installer.

# coding: utf-8 import sys import os import time import _winreg import ctypes def find_python(): """ retrieves the commandline for .py extensions from the registry """ hKey = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, r'Python.File\shell\open\command') # get the default value value, typ = _winreg.QueryValueEx (hKey, None) program = value.split('"')[1] if not program.lower().endswith(r'\python.exe'): return None return os.path.dirname(program) def extend_path(pypath, remove=False, verbose=0, remove_old=True, script=False): """ extend(pypath) adds pypath to the PATH env. variable as defined in the registry, and then notifies applications (e.g. the desktop) of this change. . Already opened DOS-Command prompts are not updated. . Newly opened prompts will have the new path (inherited from the updated windows explorer desktop) options: remove (default unset), remove from PATH instead of extend PATH remove_old (default set), removes any (old) python paths first script (default unset), try to add/remove the Scripts subdirectory of pypath (pip, easy_install) as well """ _sd = 'Scripts' # scripts subdir hKey = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE) value, typ = _winreg.QueryValueEx (hKey, "PATH") vals = value.split(';') assert isinstance(vals, list) if not remove and remove_old: new_vals = [] for v in vals: pyexe = os.path.join(v, 'python.exe') if v != pypath and os.path.exists(pyexe): if verbose > 0: print 'removing from PATH:', v continue if script and v != os.path.join(pypath, _sd) and \ os.path.exists(v.replace(_sd, pyexe)): if verbose > 0: print 'removing from PATH:', v continue new_vals.append(v) vals = new_vals if remove: try: vals.remove(pypath) except ValueError: if verbose > 0: print 'path element', pypath, 'not found' return if script: try: vals.remove(os.path.join(pypath, _sd)) except ValueError: pass print 'removing from PATH:', pypath else: if pypath in vals: if verbose > 0: print 'path element', pypath, 'already in PATH' return vals.append(pypath) if verbose > 1: print 'adding to PATH:', pypath if script: if not pypath + '\\Scripts' in vals: vals.append(pypath + '\\Scripts') if verbose > 1: print 'adding to PATH:', pypath + '\\Scripts' _winreg.SetValueEx(hKey, "PATH", 0, typ, ';'.join(vals) ) _winreg.SetValueEx(hKey, "OLDPATH", 0, typ, value ) _winreg.FlushKey(hKey) # notify other programs SendMessage = ctypes.windll.user32.SendMessageW HWND_BROADCAST = 0xFFFF WM_SETTINGCHANGE = 0x1A SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') if verbose > 1: print 'Do not forget to restart any command prompts' if __name__ == '__main__': remove = '--remove' in sys.argv script = '--noscripts' not in sys.argv extend_path(find_python(), verbose=2, remove=remove, script=script) 

Источник

How do I add Python to the Windows PATH?

I want to be able to run Python commands from the Windows CMD. However, if I don’t specify Python’s full path for each command, I get an error saying «Python is not recognized as an internal or external command, operable program or batch file.» How do I add Python to the Windows PATH permanently?

5 Answers 5

For Windows 10/8/7:

  1. Open System Properties (Right click Computer in the start menu, or use the keyboard shortcut Win + Pause )
  2. Click Advanced system settings in the sidebar.
  3. Click Environment Variables.
  4. Select PATH in the System variables section
  5. Click Edit
  6. Add Python’s path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27 

For Windows XP:

  1. Open System Properties (Type it in the start menu, or use the keyboard shortcut Win + Pause )
  2. Switch to the Advanced tab
  3. Click Environment Variables.
  4. Select PATH in the System variables section
  5. Click Edit
  6. Add Python’s path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27 

The interesting thing here is where Python actually gets installed. Earlier versions would go directly to a folder off the root (C:/Python27) but now it seems the default web install places it in the user’s AppData/Local here: C:\Users\\AppData\Local\Programs\Python\Python36 I didn’t check the box as Python was installing, but after adding this to the end of the path as other users have stated, it seems to work. At least, new command windows had this in the path, and python would start. Git BASH windows still used the old path and probably require a reboot.

Step 1 for Windows 10 is deprecated — Right-click This PC, and select Properties from the menu instead. Win + pause combo should still work but not all laptops have a labelled pause/break key.

Источник

Оцените статью