Portable Python on Windows
I have been asked a number of times how to run a custom version of Python on a Windows machine without administrative access over a given machine (despite the fact that I use Linux and not Windows :P). I'm sure I've written a guide before, but I can't find it so I thought I'd write another and document it here for reference.
Essentially, the solution is as follows:
- Download a compressed archive of 'embeddable Python' for the version and CPU architecture of Python you need
- Delete
python*._pth
(where*
is your Python version) - Update
PATH
- Run your Python program :D
Let's run through these steps in detail.
Downloading Python
To download Python, head to this address: https://www.python.org/downloads/windows/
Then, click on the following:
- "Latest Python 3 Release - Python 3.XX.Y" under "Python Releases for Windows", where
XX.Y
are digits. For example: "Latest Python 3 Release - Python 3.11.4" - Scroll down to the heading "Files"
- Then, download the file entitled "Windows embeddable package (64-bit)", or whichever one suits your CPU architecture
Extract the contents of the resulting .zip
to a folder.
Deleting the _pth file
Before we can do anything, we need to delete in the extracted folder. If you do not do this, you will find Python is unable to locate any modules.
Delete the file ending in ._pth
. The exact name changes depending on the Python release you have downloaded, but in general it will be in the form python3XX._pth
, where XX
will be the same digits from above.
Updating PATH
Now that Python is downloaded and prepared, we need to update the PATH
environment variable.
When you type a command into the command prompt (e.g. python
), the command line interpreter will search all of the directories listed in PATH
(semicolon separated on Windows, colon separated on Linux and macOS) to find that executable. This is also used by Python to locate modules on Windows.
Adapt the following command to your specific situation.
set PATH=C:\path\to\python;C:\path\to\python\Script;%PATH%
Caution: The path you choose MUST NOT contain any spaces.
Whenever you want to use the portable version of Python you've downloaded, you will need to execute your version of this command first.
Note that in some Jupyter Notebook environments the environment variable PATH
is reset, so you will need to adjust the environment variable PATH
in Jupyter.
Installing pip
Installing dependencies your code needs via pip
is a common task, but with this embeddable/portable Python setup it requires a little bit more work. Execute the following command:
curl -sSLO https://bootstrap.pypa.io/get-pip.py
python get-pip.py
If you don't have curl
installed, then download get-pip.py
in your browser instead of running the curl
command.
Once it completes, you should be able to use the pip
command as normal:
pip install tensorflow seaborn pandas numpy
Conclusion
We've downloaded embeddable Python and set it up for portable use on Windows. Please remember that if you are on a shared computer you should be mindful of disk space usage and put your copy of portable Python on a USB flash drive, or otherwise delete it when you're done. This is especially important if you install big packages like tensorflow
, which can be 1GiB+!
Another thing to keep in mind is keeping your portable Python installation up to date. Add a reminder in your calendar to check the Python website I linked to above regularly to ensure you get security updates.
Alternative methods for those with admin access include package managers such as choclately and scoop.
This concludes this guide. If you're looking for a new operating system, I can recommend Linux :D