【问题标题】:Moving Python installation folder does not update ipython paths移动 Python 安装文件夹不会更新 ipython 路径
【发布时间】:2014-04-29 17:45:20
【问题描述】:

我通过简单地移动文件夹将我的 Python 2.7 安装从 C:\Python 移动到 D:\Python(我知道还有其他方法可以做到这一点)。运行 D:\Python\python.exe 时,我可以导入系统库并毫无问题地运行。但是,我无法运行 D:\Python\Scripts\ipython.exe。 我收到以下错误:

Fatal error in launcher: Unable to create process using '"C:\Python\python.exe" 
"D:\Python\Scripts\ipython.exe" '

ipython 似乎知道它的原始安装目录 C:\Python,并尝试访问它。是否有任何启动选项或环境变量可以用来强制 ipython 使用新的安装目录?

谢谢。

编辑:

以下过程完全正常。我先通过D:\Python\python.exe启动Python,然后运行:

import IPython
IPython.start_ipython()

为什么这与运行 D:\Python\Scripts\ipython.exe 不同?

【问题讨论】:

  • Python 也会将值安装到注册表中,因此仅通过移动文件夹来移动它可能并不安全。

标签: python-2.7 ipython


【解决方案1】:

我也遇到了这个问题。问题是 pip 在安装时硬编码了 Scripts 文件夹(在本例中为 IPython)中可执行文件的 Python 解释器的路径。

我认为这不是控制可执行文件应使用哪个 Python 解释器 pip 的方法。目前,Github 上有几个针对 pip 的未解决问题表明这仍然是一个未解决的问题,例如https://github.com/pypa/pip/issues/2845.

但是,我发现这篇文章http://www.clemens-sielaff.com/create-a-portable-python-with-pip-on-windows/ 中描述的解决方法对我有用。基本上,只需在文本编辑器中打开 exe 文件并编辑 Python 解释器的路径(您会在文件末尾找到它几乎是 #!<your path>)。我正在使用#!python,因为我知道我选择的 Python 解释器将是路径中的第一个。

【讨论】:

    【解决方案2】:

    我有一个非常相似的问题,因为我使用的是通过安装程序提供的便携式 python 应用程序。正如其他答案所示,.exe 中有一个硬编码路径,它指向原始安装程序配置中的 python。

    问题在于,使用此硬编码路径的不只是pip.exeipython.exejupyter.exe.\python\Scripts 文件夹中至少有 20 多个已编译的 exe 脚本。就我而言,解决方案是不要重新安装每个模块,因为这破坏了可移植包的想法。此外,这将需要额外的 Internet 资源,而用户可能无法使用这些资源。

    解决方法是在编译脚本本身中替换python.exe的路径。这是对我有用的解决方案,来自以前的example

    这是链接中断的代码:

    """
    Patch the distribution to make it portable
    
    """
    import os
    import sys
    
    
    def win_patch_paths(folder, python_path, path_to_python="", fLOG=print):
       """
        path are absolute when they are installed,
        see `Create a portable Python with Pip on Windows <http://www.clemens- 
        sielaff.com/create-a-portable-python-with-pip-on-windows/>`_
    
        :param      folder:          folder when to find the executable
        :param      python_path:     python path (string to replace)
        :param      path_to_python:  new python path (replace by)
        :param      fLOG:            logging function
        :return:                     list of tuple ('exe or py', 'modified file')
    
        The first three parameter can be environment variables.
        They will be replaced by their values.
        
       """
       if isinstance(python_path, list):
           operations = []
           for pyt in python_path:
              op = win_patch_paths(folder, pyt, path_to_python, fLOG)
              operations.extend(op)
           return operations
       else:
           if folder in os.environ:
              folder = os.environ[folder]
           if python_path in os.environ:
              python_path = os.environ[python_path]
              if python_path == "EMPTY_STRING":
                 python_path = ""
           if path_to_python in os.environ:
              path_to_python = os.environ[path_to_python]
    
           files = os.listdir(folder)
    
           if len(python_path) > 0 and not python_path.endswith("\\"):
              python_path += "\\"
           if len(path_to_python) > 0 and not path_to_python.endswith("\\"):
              path_to_python += "\\"
    
           operations = []
           for prog in ["python.exe", "pythonw.exe"]:
              shebang = "#!" + python_path + prog
              bshebang = bytes(shebang, encoding="ascii")
              into = "#!" + os.path.normpath(path_to_python + prog)
              binto = bytes(into, encoding="ascii")
    
              fLOG("replace {0} by {1}".format(shebang, into))
    
              for file in files:
                  full = os.path.join(folder, file)
                  if os.path.isfile(full):
                      ext = os.path.splitext(full)[-1]
    
                      if ext in {".py", ""}:
                          with open(full, "r") as f:
                              content = f.read()
                        if shebang in content:
                            content = content.replace(shebang, into)
                            fLOG("update ", full)
                            operations.append(("update", full))
                            with open(full, "w") as f:
                                f.write(content)
                  elif ext == ".exe":
                      with open(full, "rb") as f:
                          content = f.read()
                      if bshebang in content:
                          content = content.replace(bshebang, binto)
                          fLOG("update ", full)
                          operations.append(("update", full))
                          with open(full, "wb") as f:
                              f.write(content)
                      else:
                          pass
    
           return operations
    
    if __name__ == '__main__':
        folder = sys.argv[1]
        old = sys.argv[2]
        new = sys.argv[3]
        print("folder:",folder)
        print("old:",old)
        print("new:",new)
    
        op = win_patch_paths(folder=folder,python_path=old,path_to_python=new)
    

    在 python 文件夹内调用:

    # Python paths are only the path, not including the python.exe as the path
    .\python.exe Scripts <old python path> <new python path>
    

    【讨论】:

      【解决方案3】:

      我在 pip 方面遇到了这个确切的问题,不得不将我的 python 安装升级到 2.7.6 并重新安装 pip。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 2017-10-15
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        相关资源
        最近更新 更多