Build Python Yourself

Create a portable Python with Pip on Windows

After building Python from source and creating a stand-alone deployment, there is still one major thing missing from any Python installation: pip.

I was happy to learn that pip is now included in the official installation packages from python.org — package managers are an essential tool for working with a collaboration-centric language like Python and pip is the best. However, when building from source, pip is not automatically installed as well.

Luckily fixing that is very easy.
Download the get-pip.py script from their site, copy it into your deployment directory next to the python.exe and run

The excellent installation script will do the rest. Aftwards you are as set up as with any “real” Python installation from the officials.

But wait! Up to this point, our Python deployment was totally self-sufficient. No matter where we put it and no matter how we named the root folder, it would perform on relative paths alone. With pip, this has now changed. Btw. the following also applies to installation from the official website.

If we were to move or rename the root folder of our Python installation, pip will no longer work but instead produce the error:

Fatal error in launcher: Unable to create process using [OLD PATH] 

As it turns out, pip.exe and the other executables in Scripts/ have the Python path at the time of installation hard-coded inside of them! But we can fix that.

Open pip.exe and the other executables in the text editor of your choice (Notepad++ is my workhorse of choice) and scroll all the way down, pass the bite-code gibberish. The 12th to last line ends in a shebang containing the old path. We can simply replace it by a relative path like this:
Change
#!C:\Users\Clemens\Desktop\Python-3.4.2\python.exe

to

#!..\python.exe

and while we’re at it, run a search/replace for all other executables as well.

Now, no matter where you place the root-folder, you can always navigate to the Scripts\-directory and invoke pip.

A word to the warning, however. This little trick works only because by navigating to the Scripts\-directory first, we have the current working directory set to a path from which ../python.exe actually points to the correct executable. You will not be able to run Scripts\pip.exe from the root-directory, for example.

Lastly, if you want to put the directories containing pip and the one containing Python on the PATH but still don’t want to hard-code an absolute path into pip and the other executables, you can replace the path with #!python . This way, as long at the correct Python interpereter is found first (and at all) in the PATH, pip will use it – no matter where it is.

This concludes this 3 part series on how to build your own, stand-alone, pip-equipped, state-of-the-art Python.
Let me know if something did not work as described here … and let me know if it did!