Starbeamrainbowlabs

Stardust
Blog

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:

  1. Download a compressed archive of 'embeddable Python' for the version and CPU architecture of Python you need
  2. Delete python*._pth (where * is your Python version)
  3. Update PATH
  4. 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:

  1. "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"
  2. Scroll down to the heading "Files"
  3. 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

Tag Cloud

3d 3d printing account algorithms android announcement architecture archives arduino artificial intelligence artix assembly async audio automation backups bash batch blender blog bookmarklet booting bug hunting c sharp c++ challenge chrome os cluster code codepen coding conundrums coding conundrums evolved command line compilers compiling compression conference conferences containerisation css dailyprogrammer data analysis debugging defining ai demystification distributed computing dns docker documentation downtime electronics email embedded systems encryption es6 features ethics event experiment external first impressions freeside future game github github gist gitlab graphics guide hardware hardware meetup holiday holidays html html5 html5 canvas infrastructure interfaces internet interoperability io.js jabber jam javascript js bin labs latex learning library linux lora low level lua maintenance manjaro minetest network networking nibriboard node.js open source operating systems optimisation outreach own your code pepperminty wiki performance phd photos php pixelbot portable privacy problem solving programming problems project projects prolog protocol protocols pseudo 3d python reddit redis reference release releases rendering research resource review rust searching secrets security series list server software sorting source code control statistics storage svg systemquery talks technical terminal textures thoughts three thing game three.js tool tutorial twitter ubuntu university update updates upgrade version control virtual reality virtualisation visual web website windows windows 10 worldeditadditions xmpp xslt

Archive

Art by Mythdael