1

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.

Community
  • 1
  • 1
Jeflopo
  • 2,192
  • 4
  • 34
  • 46

1 Answers1

5

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.

Muposat
  • 1,476
  • 1
  • 11
  • 24
  • The interpreter that was set through the windows extensions was my python27 interpreter >_< The "Open With" method solved my problem. But I really appreciate your other insights ! I think that they will help me a lot in the future :) – Jeflopo Oct 30 '15 at 21:18
  • Yep, had the same problem and this led me to the solution. Turns out, I had 2 different versions of Python installed. Running a script with the Python command in a terminal was running with 2.7, as expected. However, running via clicking on the script was running with 3.x and some of the functions I was using in my script did not work in 3.x. Thus, running with "python myscript.py" in a terminal worked fine but double clicking on the script did not. To get it working, I right-clicked on the script, went to Properties -> "Opens with" -> navigate to correct version of Python and select it. – jburn2712 Sep 01 '18 at 04:36