Skip to content Skip to sidebar Skip to footer

Moving Python Installation Folder Does Not Update Ipython Paths

I moved my Python 2.7 installation from C:\Python to D:\Python by simply moving the folder (I understand there are other ways to do this). When running D:\Python\python.exe, I can

Solution 1:

I also ran into this issue. The problem is that pip hardcodes the path to the Python interpreter for the executables in the Scripts folder (in this case IPython) at the time of the installation.

I don't think that is a way to control which Python interpreter pip should use for the executables. Currently, there are a couple of open issues on Github for pip that indicate that this is still an unsolved issue, e.g. https://github.com/pypa/pip/issues/2845.

However, I found a workaround described in this post http://www.clemens-sielaff.com/create-a-portable-python-with-pip-on-windows/ that works for me. Basically, just open the exe file in a text editor and edit the path to the Python interpreter (you will find it almost at the end of the file as #!<your path>). I'm using #!python since I know that my Python interpreter of choice will be first in path.

Solution 2:

I had this exact problem with respect to pip and had to upgrade my python installation to 2.7.6 and reinstall pip.

Solution 3:

I had a very similar issue, since I am using a portable python app which comes via an installer. As the other answers indicate, there is a hard coded path within the .exe which points to python in the original installer configuration.

The issue is that it's not just pip.exe, ipython.exe, or jupyter.exe that use this hard-coded path, but at least 20+ compiled exe scripts in the .\python\Scripts folder. The solution, in my case, is not to re-install each module since that breaks the idea of the portable package. Plus, this will require additional internet resources which might not be available for the user.

The solution is to replace the path to the python.exe in the compiled scripts themselves. Here is the solution which worked for me, from a previous example:

This is the code incase the link breaks:

"""
Patch the distribution to make it portable

"""import os
import sys


defwin_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.
    
   """ifisinstance(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)

       iflen(python_path) > 0andnot python_path.endswith("\\"):
          python_path += "\\"iflen(path_to_python) > 0andnot 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", ""}:
                      withopen(full, "r") as f:
                          content = f.read()
                    if shebang in content:
                        content = content.replace(shebang, into)
                        fLOG("update ", full)
                        operations.append(("update", full))
                        withopen(full, "w") as f:
                            f.write(content)
              elif ext == ".exe":
                  withopen(full, "rb") as f:
                      content = f.read()
                  if bshebang in content:
                      content = content.replace(bshebang, binto)
                      fLOG("update ", full)
                      operations.append(("update", full))
                      withopen(full, "wb") as f:
                          f.write(content)
                  else:
                      passreturn 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)

Call within python folder:

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

Post a Comment for "Moving Python Installation Folder Does Not Update Ipython Paths"