Python 2.7 移动Python安装文件夹不会更新ipython路径

Python 2.7 移动Python安装文件夹不会更新ipython路径,python-2.7,ipython,Python 2.7,Ipython,我通过简单地移动文件夹(我知道还有其他方法)将Python2.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\Script

我通过简单地移动文件夹(我知道还有其他方法)将Python2.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不同?

我在pip方面遇到了这个问题,必须将Python安装升级到2.7.6并重新安装pip。

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

我不认为这是一种控制pip应该为可执行文件使用哪种Python解释器的方法。目前,Github for pip上有几个未解决的问题,表明这仍然是一个未解决的问题,例如


然而,我发现了一个在这篇文章中描述的适合我的解决方法。基本上,只需在文本编辑器中打开exe文件并编辑Python解释器的路径(您几乎可以在文件末尾找到它,即
)。我正在使用
#!python
因为我知道我选择的python解释器将是path中的第一个。

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

问题是,使用此硬编码路径的不仅仅是
pip.exe
ipython.exe
jupyter.exe
,而是
\python\scripts
文件夹中至少有20多个已编译的exe脚本。在我的例子中,解决方案是不重新安装每个模块,因为这打破了便携软件包的概念。此外,这将需要额外的互联网资源,用户可能无法使用这些资源

解决方案是在编译的脚本中替换python.exe的路径。这是一个对我有效的解决方案,来自于以前的:

这是链接断开时的代码:

"""
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)
“”“
对分发版进行修补,使其可移植
"""
导入操作系统
导入系统
def win_patch_路径(文件夹,python_路径,路径到python=”,fLOG=print):
"""
安装时路径是绝对的,
请参见`在Windows上使用Pip创建可移植Python`_
:param folder:何时查找可执行文件的文件夹
:param python_path:python路径(要替换的字符串)
:param path_to_python:new python path(替换为)
:param fLOG:日志记录功能
:return:元组列表('exe或py','modified file')
前三个参数可以是环境变量。
它们将被它们的价值观所取代。
"""
如果isinstance(python_路径,列表):
操作=[]
对于python_路径中的pyt:
op=win\u patch\u路径(文件夹、pyt、路径到python、fLOG)
扩展操作(op)
返回操作
其他:
如果os.environ中的文件夹:
folder=os.environ[文件夹]
如果在os.environ中使用python_路径:
python\u path=os.environ[python\u path]
如果python_path==“空_字符串”:
python_path=“”
如果os.environ中的路径为python:
path_to_python=os.environ[path_to_python]
files=os.listdir(文件夹)
如果len(python\u path)>0而不是python\u path.endswith(“\\”):
python\u路径+=“\\”
如果len(path\u to \u python)>0而不是path\u to \u python.endswith(“\\”):
路径到python+=“\\”
操作=[]
对于[“python.exe”、“pythonw.exe”]中的程序:
shebang=“#!”+python_路径+程序
bshebang=字节(shebang,encoding=“ascii”)
into=“#!”+os.path.normpath(路径到python+prog)
binto=字节(输入,编码=“ascii”)
fLOG(“用{1}替换{0}”。格式(shebang,into))
对于文件中的文件:
full=os.path.join(文件夹、文件)
如果os.path.isfile(完整):
ext=os.path.splitext(完整)[-1]
如果ext在{.py“,”“}中:
以开(满,“r”)作为f:
content=f.read()
如果在内容上是shebang:
content=content.replace(shebang,into)
鞭笞(“更新”,全部)
附加((“更新”,完整))
打开(全“w”)作为f:
f、 写作(内容)
elif ext==“.exe”:
打开(完全,“rb”)作为f:
content=f.read()
如果内容不完整:
content=content.replace(bshebang,binto)
鞭笞(“更新”,全部)
附加((“更新”,完整))
打开(完整,“wb”)作为f:
f、 写作(内容)
其他:
通过
返回操作
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
folder=sys.argv[1]
old=sys.argv[2]
新建=系统argv[3]
打印(“文件夹:”,文件夹)
打印(“旧:”,旧)
打印(“新:”,新)
op=win\u patch\u路径(folder=folder,python\u path=old,path\u to\u 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>
#Python路径只是路径,不包括Python.exe作为路径