首页 文章

在Windows 7上添加Python路径

提问于
浏览
135

我've been trying to add the Python path to the command line on Windows 7, yet no matter the method I try, nothing seems to work. I'使用了 set 命令,我尝试通过编辑环境变量提示等添加它 .

如果我在命令行上运行set命令,它会列出这个

python = c:\python27

但它仍然无法识别Python命令 .

阅读文档和各种其他来源似乎没有帮助 .

编辑:为了进一步澄清,我在编辑环境提示符中将Python可执行文件的路径附加到PATH . 似乎没有用 .

20 回答

  • -1
    • 保持胜利并按暂停 .

    • 单击“高级系统设置” .

    • 单击“环境变量” .

    • ;C:\python27 追加到 Path 变量 .

    • 重启命令提示符 .

  • 6

    在Windows中设置环境变量时,我在很多场合都出错了 . 我想我应该在这里分享我过去的一些错误,希望它可以帮助某人 . (这些适用于所有环境变量,而不仅仅是在设置Python路径时)

    注意这些可能的错误:

    • 终止并重新打开shell窗口:对环境变量进行更改后,您必须 restart 正在测试它的窗口 .
      设置变量时

    • NO SPACES . 确保添加 ;C:\Python27 没有任何空格 . (通常在分号不合适之后尝试 C:\SomeOther; C:\Python27 那个空格(␣) . )

    • 在拼写完整路径时使用 BACKWARD SLASH . 当你尝试 echo $PATH 时,你会看到正斜杠,但只有反向斜杠对我有用 .

    • DO NOT ADD a final backslash . 只有 C:\Python27 不是 C:\Python27\

    希望这有助于某人 .

  • 5

    使用管理员权限打开 cmd .exe(右键单击应用程序) . 然后输入:

    setx path“%path%; C:\ Python27;”

    请记住以分号结尾,不要包含尾部斜杠 .

  • 8

    我很久没遇到这个问题了 . 我以我能想到的各种方式将它添加到我的路径中,但这里最终对我有用:

    • 右键单击"My computer"

    • 点击"Properties"

    • 单击侧面板中的"Advanced system settings"

    • 点击"Environment Variables"

    • Click the "New" below system variables

    • in name enter pythonexe (或任何你想要的)

    • in value enter the path to your python (例如: C:\Python32\

    • Now edit the Path variable (in the system part) and add %pythonexe%; to the end of what's already there

    IDK为什么会这样,但它确实适合我 .

    然后尝试在命令行中键入“python”,它应该工作!


    编辑:

    最近我一直在使用this program,这看起来效果很好 . 还有this one看起来也不错,虽然我从未尝试过 .

  • 32

    尝试将此 python.bat 文件添加到 System32 文件夹,当您输入 python 时,命令行将运行python

    python.bat

    @C:\Python27\python.exe %*
    

    资源:

    https://github.com/KartikTalwar/dotfiles/blob/master/bat/python.bat

  • 11

    您可以使用 PATH = 命令设置 current cmd window 的路径 . 这只会为当前的cmd实例添加它 . 如果要永久添加它,则应将其添加到系统变量中 . (计算机>高级系统设置>环境变量)

    你会转到你的cmd实例,并输入 PATH=C:/Python27/;%PATH% .

  • 2

    确保不在新目录之前添加空格 .

    好的:老的;老的;老的;新的

    坏:老;老;老;新

  • 2

    Python附带small utility that does just this . 从命令行运行:

    c:\python27\tools\scripts\win_add2path.py
    

    确保关闭命令窗口(使用 exit 或关闭按钮)并再次打开它 .

  • 50

    以下程序将向您的环境添加python可执行文件路径和子文件脚本(例如,安装了pip和easy_install) . 它从绑定.py扩展名的注册表项中找到python可执行文件的路径 . 它将删除环境中的旧python路径 . 适用于XP(也可能是Vista) . 它仅使用基本Windows安装程序附带的模块 .

    # 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)
    
  • 4

    我知道这篇文章已经过时了,但我想补充一点,解决方案假设是管理员权限 . 如果你没有那些你可以:

    转到控制面板,键入路径(现在这是Windows 7,以便在搜索框中),然后单击“为您的帐户编辑环境变量” . 现在,您将看到顶部的“用户变量”和下面的“系统变量”的“环境变量”对话框 .

    您可以作为用户单击顶部的“新建”按钮并添加:

    变量名称: PATH
    变量值: C:\Python27

    (无任何空格)并单击“确定” . 重新启动命令提示符后,User变量中的任何PATH都将附加到系统路径的末尾 . 它不会以任何其他方式替换PATH .

    如果你想要一个特定的完整路径设置,你最好创建一个这个小小的批处理文件:

    @echo off
    PATH C:\User\Me\Programs\mingw\bin;C:\User\Me\Programs;C:\Windows\system32
    title Compiler Environment - %Username%@%Computername%
    cmd
    

    称之为“compiler.bat”或其他,然后双击启动它 . 或链接到它 . 或者固定它等等......

  • 16

    您需要在系统变量中进行更改

    • 右键单击"My computer"
    • 点击"Properties"
    • 单击侧面板中的"Advanced system settings"
    • 单击环境变量 - 您将有两部分用户变量和系统变量
    • 在系统变量部分下搜索变量'Path',单击编辑并添加
      "C:\Python27;" (不含引号)保存
    • 现在打开命令行类型'path'命中输入你将看到路径变量已被修改
    • 现在输入 python --version ,你会看到python版本

    它完成了

  • 3

    对于任何尝试使用Python 3.3实现此目的的人来说,Windows安装程序现在包含一个将python.exe添加到系统搜索路径的选项 . 阅读更多the docs .

  • 262

    使用Windows环境变量总是一种可怕的体验 . 最近,我发现了一个名为Rapid Environment Editor的神奇工具,它为管理它们提供了一个非常简单的GUI .

    如果您使用chocolatey,则可以使用 choco install rapidee 进行安装 . 否则,看看http://www.rapidee.com/en/download

    重读这个,听起来像是付费的,但我发誓我不是!它只是我工具箱中最有用的工具之一,我很惊讶似乎没有人知道它 .

  • 2

    如果Python与其他程序一起安装,例如我的ArcGIS 10.1,那么您还必须包含环境变量中python.exe路径的任何额外文件夹 .

    所以我的环境变量看起来像这样:

    系统变量>路径>添加 ;C:\Python27\ArcGIS10.1

  • 1

    这个问题很老了,但我遇到了类似的问题,我的特定解决方案没有列在这里:

    Make sure you don't have a folder in your PATH that doesn't exist.

    在我的情况下,我有一堆默认文件夹(Windows,Powershell,Sql Server等),然后是我通常使用的自定义_1116499,然后是各种其他调整,如 c:\python17 等 . 事实证明,cmd处理器正在寻找 c:\bin 不存在然后停止处理变量的其余部分 .

    另外,我不知道如果没有PATH manager,我会注意到这一点 . 它很好地突出了该项目无效的事实 .

  • 9

    我刚刚在Windows 7上使用“将python添加到PATH”选项安装了Python 3.3 .

    在PATH变量中,安装程序自动在命令提示符下添加 final backslashC:\Python33\it did not work (我试过几次关闭/打开提示)

    I removed the final backslash 然后它起作用了: C:\Python33

    感谢Ram Narasimhan提示#4!

  • 13

    我使用cmd在Win7 64位下组织了我的python环境变量 .

    我通过windows的环境变量menue设置变量 PYTHONPATH ,并将 %PYTHONPATH% 添加到 PATH 变量:

    ...;%PYTHONPATH%

    cmd shell将变量正确扩展为:

    C:\>echo %PYTHONPATH%
    C:\python27;c:\python27\lib;C:\python27\scripts
    

    更改PATH后,不要忘记重新启动cmd shell .

  • 6

    我的系统是Windows7 32bit,安装了Python 2.7.12(因为pdfminer不支持Python 3.X .... T ^ T)

    有同样的问题,我的命令窗口识别单词“python” .

    事实证明,在PATH变量中,自动添加了一个最后的反斜杠:C:\ Python33 \(与上面提到的Charlie相同)

    删除反斜杠 . 一切正常 .

  • 0

    在命令提示符上写下:

    set Path=%path%
    

    通过Python文件夹的路径替换%path%示例:

    set Path=C:/Python27
    
  • 110

    如果您通过设置python的路径感到沮丧只需下载新版本的python卸载旧版本的python,并在安装新版本时它会询问是否设置路径标记并安装

    这是最好的方式

相关问题