Python file not running on double click of batch file

  anaconda, batch-file, python, python-3.x, windows

I have installed Anaconda and am using Python with a few packages, like Pandas, xlwings, os and pyodbc. Other people in the organization would like to run my files, but we can’t find a suitable way to run the scripts I have written.

On my computer, I can run everything within Spyder. If I double click the same program in Windows Explorer, however, it seems to open something black, but then shuts down without printing or creating any outputs. I have right clicked the file, selected Python to ‘always open with’, and all the .py files have the python symbol next to them.

To understand what is happening, I wrote a simple test file to see if the packages would be accessed, and if it can be run:

import datetime
# just to be sure in accesses packages
import pandas

start_date = datetime.date(2021, 3, 1)
var = "hello world"
print(var, start_date)

# just in case print function is not visible when window closes
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

If I navigate in the CMD window to the correct folder, I can run this test file. It prints and appends the text file.

If I double click the file, a window flashes open and closed. No output printed or in text file.

In case the path is an issue, I did ‘where Python’, and created a batch file to run python code, as described here:

ECHO ON
REM A batch script to execute a Python script
SET PATH=%PATH%;C:....Anaconda3
python test.py
PAUSE

When I double click it, it seems to execute something without errors, but there is no output from python.

Q:.....Miscpynlich>ECHO ON

Q:.....Miscpynlich>REM A batch script to execute a Python script

Q:.....Miscpynlich>SET PATH=C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;c:Program Files (x86)itWatch;C:Program Filesdotnet;C:Program Files (x86)Microsoft SQL Server0DTSBinn;C:Program FilesAzure Data Studiobin;C:....AppDataLocalMicrosoftWindowsApps;;C:Program FilesAzure Data Studiobin;C:....Anaconda3

Q:.....Miscpynlich>python test.py

Q:.....Miscpynlich>PAUSE

DrĂ¼cken Sie eine beliebige Taste . . .

I’ve wiped and re-installed Anaconda. Tried the other location where Python lists. Tried it on other computers in our organization with just Python installed (without Anaconda or any packages), but it is the same situation – runs from cmd, but not on double click.

Source: Windows Questions

LEAVE A COMMENT