suggesting a launcher wrapper script for portable python

Gelonida N gelonida at gmail.com
Sat Aug 11 20:49:40 EDT 2012


I just started looking at portable Python and was rather surprised, that 
I didn't find any recommended method in the documentation of how to
launch scripts with portable python.
Creating py2exe scripts on ones own USB drive seems to be kind of overkill.

So here my own thoughts / suggestsions.
I'm interestted in feedback of how others use portable pythons
and how they run their scripts from a USB stick.


Let's assume I install portable python on my USB drive and then I'd like 
to store self written python scripts on this drive.

It would of course be greate if I could just click on the script and 
they'd be started.


However under windows this would not be the case.
The python script would either not be started at all or if the PC had 
his own python installed, then the script would be started with the PC's 
version of python.

Thus a tiny wrapper script would be needed.


Suggestion:
--------------
The current directory structore for portable python (2.7) is (assuming 
that %PP% is the base directory)

%PP%/Python-Portable.exe       # launches the python interactive shell
%PP%/PyScripter-Portable.exe   # launches some IDE
%PP%/App

Let's assume I add two more directories:
%PP%/myscripts    # location of all callable scripts
%PP%/launchers    # location with icons one can click on
                   # to start the scripts in myscripts




if I wrote a script named %PP%/myscripts/test1.py,
and I created an aproprriate  named %PP%/launchers/test1.bat

then I could just click on test1.bat and the Python script test1.py 
would be started. If the wrapper script is written properly, then it can 
look at its own base name and call the related python script.

If I dragged and dropped some filenames on the bat file, then they would 
be passed to sys.argv of the script.

Running the script from command line would also work and the present 
working directory would be preserved (which might be useful in some cases)

If the script name would not be .py, but .pyw then it woudl be started 
with pythonw.
T

Below suggested script:



@echo off
REM 
=========================================================================
REM script to start a python file with portable python
REM 
=========================================================================

REM basepath of this .bat file
set basepath=%~dp0

REM create the name of the python file related to this bat file
REM Unfortunately I do not know how to normalyze %pyfile%,
REM so we got stuck with the '..'
set pyfile=%basepath%..\myscripts\%~n0.py

If EXIST "%pyfile%" (
     REM a normal console python file with .py suffix
     "%basepath%\..\App\python.exe" "%pyfile%"  %*
) ELSE (
If EXIST "%pyfile%w" (
     REM a non  console python file with .pyw suffix
     start "" "%basepath%\..\App\pythonw.exe" "%pyfile%w"  %*
) ELSE (
     REM found neither a .py nor a .pyw file
     echo found no python file %pyfile%
)
)
REM 
=========================================================================
REM end of script
REM 
=========================================================================


One minor drawback of my suggested script would be, that a console 
window pops up for a few seconds when starting a .pyw file.

This could be avoided by using either a small compiled C-file (which 
sounds like overkill though)
or by writing a windows scripting host .wsf file.
However I don't know this well enough to replicate my batch file.
AN article on SO mentions how to write such a script.
However it does not parse command line arguments nor does it
automatically determine the scripts file name.
So any help for creating a .wsf file starting a .pyw file with command 
line arguments would be appreciated.



An alternativce approach could be to provide a scipt named
mk_wrapper.bat
If one drags and drops a python script on it, then an apropriate wrapper 
file would be created in the launcher directory.

If well done, then this could be implemented such, that the script may 
be located in an arbitrary location on the same USB drive.



I think it would be great if the official portable python release 
contained some upport for launching scripts.
Perhaps it exists alrady and I just didn't find it?

If not,then I wouldn't mind if my script or a similiar sand a related 
README.txt cript were added to the official release











More information about the Python-list mailing list