Portable Python/Leo for Windows

89 views
Skip to first unread message

Thomas Passin

unread,
May 11, 2020, 11:55:01 PM5/11/20
to leo-e...@googlegroups.com
There used to be a portable app type package for Python so that it could be run from a USB stick and used on different computers.  But no longer.  I thought it would be interesting to see what would be involved in setting one up.  This is for Windows.  I'm sure it could be adapted for Linux without much trouble.

First of all, the standard Python installer will install to any drive, including a usb stick.  That's easy.  Your want to install for all users so that there won't be a problem using it on some computer where you are not the specific user you installed for.   But that's not the end of it.

You have to take care of at least two things, if you really want to be portable:

1. Where python will look for configuration and settings files, the site-customize location, .pth files, etc.  There is an environmental variable to use for this:  PYTHONUSERBASE.  You can find where it is on your current  Python installation:

>>>import site
>>> site.getuserbase()
'C:\\Users\\tom\\AppData\\Roaming\\Python'
>>> print(site.getsitepackages())
['C:\\Users\\tom\\AppData\\Local\\Programs\\Python\\Python38', 'C:\\Users\\tom\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']
>>>


You need to set the user base so that it is on your usb stick.

2. Leo will look for some of its files, like its .leo/db directory, its user name file, and the standard workbook.leo file, in os.path.expanduser('~'):

>>> import os
>>> os.path.expanduser('~')
'C:\\Users\\tom'

You need to change this to point to the USB stick.  It is set by the HOME environmental variable.

You will need to decide on a directory structure.  For my tests, with the USB stick mounted as drive f:, I installed Python into f:\python3, and created a directory f:\user\python to use for both the user base and home directories.   I set the environmental variables and run Python using the following cmd file named pyth.cmd:

@echo off
setlocal
path f:\python3\Scripts;f:\python3;%PATH%
set PYTHONUSERBASE=f:\user\python
set HOME=f:\user\python
call python %*
endlocal

After I installed Python 3.7 for the f:\python3 directory, I updated pip:

pyth -m pip install --upgrade pip

I have found that some python installers do not install wheel, and therefore some packages don't install because they can't build their wheels.  So I now always run:

pyth -m pip install --upgrade wheel

This time, there was no previous wheel installation.  Then I installed Leo:

pyth -m pip install leo

Running Leo, everything worked the first time:

pyth -m leo.core.runLeo --use-docks

I copied my standard settings from the myLeoSettings.leo file  in my usual on-computer Leo installation. Note that I always run Leo with the -m leo.core.runLeo invocation because I find that sometimes I don't have a leo, runLeo, or launchLeo installed - depending on what Leo package I'm using - but this way always works. (Or maybe I do have them but my system path doesn't find them.  Anyway, this method is very dependable.).

I have run Leo on this usb stick on two different Windows 10 computers.  I did have to edit the batch file paths from f:\ to e:\ because the mount name of the drive was different on the two computers.  I haven't come up with a way to adapt to the change in drive letter except by editing the batch file, sorry to say.

The stick did fail to work on a third computer.  However, that one was so old that Windows hadn't been able to update itself for two or maybe even three years.  So I expect there is some kind of DLL incompatibility going on.  I would get the Leo splash screen but then it would die with no error message to the console.  One of the computers that did work was a laptop I got back around 2012 (which has been able to be updated, though I'm retiring it so it may not get many more updates).  So an old computer doesn't necessarily mean that this USB stick technique won't work.

Of course, you could do all the usual maneuvers to use Leo from a git clone or a zip download if you want to.

I hope this will be useful to someone.

Edward K. Ream

unread,
May 12, 2020, 9:22:38 AM5/12/20
to leo-editor
On Mon, May 11, 2020 at 10:55 PM Thomas Passin <tbp1...@gmail.com> wrote:
There used to be a portable app type package for Python so that it could be run from a USB stick and used on different computers. 

I thought this was what Matt just did. What's the difference?

Edward

Thomas Passin

unread,
May 12, 2020, 9:44:38 AM5/12/20
to leo-editor
I thought that Matt made a self contained installer for Leo, though I wasn't too clear about it.  Maybe I didn't understand.  Using an installer wouldn't necessarily get you a portable version of Leo, though.

Thomas Passin

unread,
May 12, 2020, 2:30:09 PM5/12/20
to leo-editor
One more thing.  In your directory tree on the USB stick, it's best to create the directories PythonX.Y/site-packages in the location used for PYTHONUSERBASE. If you install Python 3.7, X.Y will be 3.7.  This is where Python will look for *.pth files(if you use them to set paths), it is where you would put a site-customize directory if you use one, and apparently occasional packages will use the directory during install or operation.

Matt Wilkie

unread,
May 13, 2020, 11:06:51 AM5/13/20
to leo-editor
 
I thought that Matt made a self contained installer for Leo, though I wasn't too clear about it.  Maybe I didn't understand.  Using an installer wouldn't necessarily get you a portable version of Leo, though.


Thomas built a portable python environment that happens to contain Leo. I built a portable Leo environment that contains enough python (as libraries) to run Leo.

It's an unfortunate but easy to understand confusion as the name of the tool that makes the package ends with "installer", but the output isn't an installer but a self contained executable package (that can be fed to an install maker like NSIS). Maybe I should rename this mini project to make the distinction clearer.

-matt


Matt Wilkie

unread,
May 13, 2020, 11:18:07 AM5/13/20
to leo-editor
I did have to edit the batch file paths from f:\ to e:\ because the mount name of the drive was different on the two computers.  I haven't come up with a way to adapt to the change in drive letter except by editing the batch file, sorry to say.

If using CMD (as opposed to Powershell) use %~dp0

%0 - name of bat file (or call :label)
%~ - trigger expansion features
%~d - drive letter
%~p - path
%~n - filename
%~x - file extension

%~dpnx0 - fully qualified path and file name of currently executing batch file.


-matt

Thomas Passin

unread,
May 13, 2020, 7:31:09 PM5/13/20
to leo-editor
Thanks, very cool!  These expansion thingies are new to me. I used to use 4dos and 4nt, but haven't done anything at all tricky with batch files for ages.

Thomas Passin

unread,
May 13, 2020, 7:44:32 PM5/13/20
to leo-editor

On Wednesday, May 13, 2020 at 7:31:09 PM UTC-4, Thomas Passin wrote:
Thanks, very cool!  These expansion thingies are new to me. I used to use 4dos and 4nt, but haven't done anything at all tricky with batch files for ages.

On Wednesday, May 13, 2020 at 11:18:07 AM UTC-4, Matt Wilkie wrote:
I did have to edit the batch file paths from f:\ to e:\ because the mount name of the drive was different on the two computers.  I haven't come up with a way to adapt to the change in drive letter except by editing the batch file, sorry to say.

If using CMD (as opposed to Powershell) use %~dp0
[snip]

With this cmd-specific technique, the script to launch Python from the USB stick becomes:

@echo off
setlocal
: Find effective drive for this file.
set ed=%~d0
path %ed%\python3\Scripts;%ed%\python3;%PATH%
set PYTHONUSERBASE=%ed%\user\python
set HOME=%ed%\user\python
call python %*
endlocal



 

Thomas Passin

unread,
May 13, 2020, 7:49:07 PM5/13/20
to leo-editor
This is a nice concise description.  My goals were to 1) make a portable environment within which one could do some development on Leo.  So I wanted to be able to add new packages to Python; 2) preferably, no compilation or other software would be needed to make the environment.
Reply all
Reply to author
Forward
0 new messages