简体   繁体   中英

Python script doesn't execute by doubleclick on Windows

For a project structure like this:

/tumblr
    /tumblr
        /module_foo
            __init__.py
            submodule_foo.py
        /module_bar
            __init__.py
            submodule_bar.py
        __init__.py
        bot.py

I was doing something like that:

import sys
sys.path.append('C:\\repos\\bot\\tumblr\\tumblr')

from tumblr.bot import Bot

class SubmoduleFoo(Bot):
    def __init__(self):
        super().__init__()
        # ...
    # ...
if __name__ == "__main__":
    SubmoduleFoo()

The bot.py only defines a an empty class with some initializations:

import sys
sys.path.append('C:\\repos\\bot\\tumblr\\tumblr')

class Bot:
    def __init__(self):
        self.settings = dict()
        # ...

If I call these scripts from command line they work:

python C:\path\to\submodule_foo.py

If I doubleclick my python submodule_foo.py "script" It doesn't work.

I think the problem Is caused by the inheritance, and calling the parent init

But since I'm just doubleclicking it, I don't know how to debug it properly.

I tried to include a:

with open('codes.txt', 'a') as file:
    file.write("It Works \n")

Just after the if __name__ == "__main__": statement and doesn't work :$

My PATH variable (in windows 10) includes the c:\\python34 and c:\\python34\\scripts I also have a PYHOME env variable that points to c:\\python34

And I have already tried this solution: Python scripts stopped running on double-click in Windows But it doesn't work for me.

I also tried to create these classes throught the python interpreter. And the import to tumblr.bot.Bot() was working

I'm running out of Ideas.

Since your file output does not work then it is possible that python interpreter is not started at all. Right-click the file in Windows Explorer, find where it says "opens with", and press button "Change" it it looks incorrect.

Open DOS command prompt by typing cmd in Start, and try to run your script both with and without specifying the interpreter.

c:\home>python.exe myscript.py

or

c:\home>myscript.py

if #1 works and #2 does not it means Windows is not configured to open *.py files with the correct interpreter.

To see output when you start a script by double-click, you can show the error message like this:

#!/usr/bin/env python
import sys
try:
# your code here   
# your code here   
# your code here   

except Exception as e:
    print("\nTHERE WAS AN ERROR PROCESSING YOUR REQUEST:", e, file=sys.stderr)
    input("Press Enter to exit...")

The stderr channel is highly recommended because by default Python prints to console in MS version of latin-1, and any Unicode characters will trigger an encoding exception. stderr does not have this limitation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM