1

I made a portable python script on Windows, by putting the whole python folder into the root directory:

ProjectFolder
|- run.bat
|- main.py
|- python <--- Python 3.10.1, downloaded from https://www.python.org/downloads/release/python-3101/
 |- ...
 |- python.exe
 |- ...

run.bat

@setlocal enableextensions
@cd /d "%~dp0"
start "%cd%\python\python.exe" main.py

It works well on my side, but not on some of the others' systems.

Note: all testers do not have python installed in their OS, but only some of them have this error:

Windows will ask for an application to open .py file, or simply open a notepad -- I think I should have specified python.exe which could run the .py file.

May I know what exactly caused this problem?

TaihouKai
  • 133
  • 11
  • Do they all have the same version of **Python** as yours? – Feras Alfrih Jan 02 '22 at 14:55
  • @FerasAlfrih All testers do not have python pre-installed. I put python 3.10 in the project folder. So I think they are supposed to use that same version of Python... – TaihouKai Jan 02 '22 at 14:55
  • 4
    If you had read the Command Prompt output of the usage information for the `start` command, you'd have noticed that the first double-quoted string is parsed as the window title. So when you use `start "%cd%\python\python.exe" main.py` your code is running `start main.py`, with the Window Title `%cd%\python\python.exe`. This means that their system looks for whatever application is configured to run `.py` files. If they already have a python.exe version registered, it will use that, if they don't it will trigger the prompt. What you need therefore is `start "" "%cd%\python\python.exe" main.py`. – Compo Jan 02 '22 at 15:55
  • Please add a few details: How do you run the batch file? What do the variable replacements in that file evaluate to? – Ulrich Eckhardt Jan 02 '22 at 16:02
  • Either duplicate, either should have better specified name: https://stackoverflow.com/questions/44488174/how-to-make-python-portable – halt9k Mar 02 '23 at 10:55

1 Answers1

0

As per my earlier comment, if you had read the Command Prompt output of the usage information for the start command, you'd have noticed that the first double-quoted string is parsed as the window title; i.e.

C:\users\TaihouKai>start /?
Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
…

So when you use start "%cd%\python\python.exe" main.py your code is running start main.py, with the Window Title %cd%\python\python.exe.

This means that their system looks for whatever application is configured to run .py files. If they already have an executable file, (most likely python.exe) registered, it will use that, if they don't it will trigger the 'Choose an application…' prompt.

What you need therefore is a title, even an empty one, to prevent that parsing issue of the first double-quoted string.

@SetLocal EnableExtensions
@CD /D "%~dp0"
@Start "" "%CD%\python\python.exe" main.py

Although, in your case, as you have a known directory structure, and it does not include any characters which require double-quoting, I'd say that you could probably do it like this:

@SetLocal EnableExtensions
@CD /D "%~dp0"
@Start python\python.exe main.py

Alternatively you could use the /D option for start to predefine the working directory:

@SetLocal EnableExtensions
@Start "" /D "%~dp0." "%~dp0python\python.exe" "main.py"

This final example encloses all strings with double-quotes as a robust, rule of thumb, safety habit.

Compo
  • 36,585
  • 5
  • 27
  • 39