0

I might just be doing something very stupid, as I am pretty new to Python programming. I using Windows 10, and I am having issues getting a file to run with a double click or in a batch file. I have other python files that run fine with a double click and in batch files, but this very simple file does not run at all. I added in a print('Hello World') command and that comes up fine, but once I click enter the screen simply disappears.

The code that works in pycharm should be doing some web scraping and then saving an excel file, but since it didn't work with the double click I simplified it down to just creating an excel file and saving it, which also runs fine in pycharm, and that still doesn't work with a double click.

print("hello world")
input()

import pandas as pd

# Create the pandas DataFrame
df = pd.DataFrame([10, 20, 30, 40, 50, 60], columns=['Numbers'])

# Save File
writer = pd.ExcelWriter('test data.xlsx')
df.to_excel(writer, 'Test Data', index=False)
writer.save()`
8
  • What happens when you double click on a file depends very little on the code in the file and a lot on your OS settings and environment. Do you have a Python interpreter on your PATH? Is it the version you expect? Does it have the required libraries installed (i.e. pandas)? Commented Jan 28, 2023 at 21:19
  • 1
    open cmd/terminal and run it to see what the error is. My bet will be it does not find the excel file - because the current working directory is different from the one where it is located Commented Jan 28, 2023 at 21:19
  • Running something "with a doubleclick" has little to do with Python, and more with how your Windows is configured, or how Python is installed. It depends greatly on the version of Windows you have installed, and somewhat on how you installed Python. Please provide some details - what version of Window are you using? How did you install Python? Is Python on the path? Have you checked what Windows does at all when you doubleclick a .py file? Can you provide an example from how you're trying to use the script from a batch file? Have you tried running a simple print('hello world') script? Commented Jan 28, 2023 at 21:20
  • The program could be raising an error and exiting before you see it. You could wrap the whole thing in a generic try: then except Exception as e: - print the error message and then call input("press any key to terminate"). Then you'll have a better idea what went on. Commented Jan 28, 2023 at 21:53
  • Which operating system are you on? They handle double-click differently. Commented Jan 28, 2023 at 21:53

3 Answers 3

1

You told us this is the first line of your file:

import pandas as pd

In some environments a "shebang" is an important part of what happens when you try to double-click or otherwise execute a script. Here is one example of a shebang, followed by source code:

#! /usr/bin/env python
import pandas as pd
...

Here is a simpler one:

#! python
import pandas as pd
...

It may also be necessary to set execute permission:

$ chmod a+x hello_world.py
Sign up to request clarification or add additional context in comments.

2 Comments

Given that OP mentions a "batch file", I'm guessing they're on Windows, which doesn't have chmod or care about shebang lines. This is still great advice for Linux, but I don't think it helps the OP in particular.
@SilvioMayolo This isn't entirely correct, I just found that out the hard way: Python files on a Windows machine with a correct Python installation don't need a shebang to work, yes. The shebangs mentioned in the answer also work fine. But if you use the old #!/usr/bin/env python3 (mind the 3) on Windows, it doesn't work, it opens and closes again instantly without running. When running it via Powershell as just .\file_name.py it shows an error saying python wasn't found, while python .\file_name.py works fine, which is how I found out.
0

On Windows:

  1. Select thefile.py (file to open)
  2. Right click on it and click Properties
  3. Click Change (to the right of Opens with)
  4. Click More apps
  5. Click Look for another app on this PC
  6. Select pythonw.exe - in my case it is at C:\Users\Administrator\AppData\Local\Programs\Python\Python311\pythonw.exe

1 Comment

This isn't how you should do it. If you want double click execution on Windows, you should have the Python Launcher installed during Python installation and use that (aka. py.exe) to open Python files. The reason being that the Python Launcher supports additional features like shebang lines with arguments and such.
0

Had the same problem. What solved it was Rsge's comment:

use that (aka. py.exe) to open Python

So I changed the *.py file association to C:\Windows\py.exe

(using pythonw.exe or python.exe had no effect)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.