简体   繁体   中英

Python script runs when double-clicked, but won't run in IDLE

So I've been trying to write a short script that automates pulling up the Unity3D help pages (five of them), as well as Unity itself and TortoiseHG. Since the webbrowser module was not behaving the way I wanted (on my Win7 box), I've been using subprocess.run.

Last night, I thought I finally fixed the thing and got it doing what I want, only to have it open five windows instead of tabs if Firefox wasn't already running. Same issue I was having with webbrowser.open, go figure.

But that's not why I'm here. I'm here because when I opened the script with IDLE to try to fix that problem, I ran into a new problem: the script will run just fine if I double click on it, but if I try to run it through IDLE with F5 I get

  Traceback (most recent call last):
  File "C:\Users\<me>\AppData\Local\Programs\Python\Python35-32\GameDevEnvironment.py", line 5, in <module>
    subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials', shell=True)
AttributeError: 'module' object has no attribute 'run'

My full code:

import os
import subprocess
#import time

subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials', shell=True)

#time.sleep(5)

subprocess.run(r'start firefox -new-tab https://answers.unity3d.com', shell=True)

subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials/topics/scripting', shell=True)

subprocess.run(r'start firefox -new-tab https://docs.unity3d.com/Manual/index.html', shell=True)

subprocess.run(r'start firefox -new-tab https://docs.unity3d.com/ScriptReference/index.html', shell=True)

#os.startfile(r'C:\Program Files\Unity\Editor\Unity.exe')

#os.startfile(r'C:\Program Files\TortoiseHg\thgw.exe')

I was going to try using the time.sleep method to get a Firefox process running so that it wouldn't open a new window for each subprocess.run call (as said before, they'll open in tabs if Firefox is already running). I commented it out while trying to solve the new problem.

What I've already tried: looking for a .pyc file that could be confusing import. Found none. Making sure none of my methods or classes are named after Python modules--as you can see, there aren't any method or class definitions, and unless Python has a module called GameDevEnvironment.py... I found lots of questions here and elsewhere of people having the opposite issue of running in IDLE not on double click etc., but couldn't find anything obviously relevant...

I appreciate your time and help!

Since the original question is a two-parter, here is a two part answer (both are also in the comments).

The reason I could not run the script from IDLE was that the shell version was older than the code I wrote. Basically, in Python 3.2 the Subprocess class didn't exist. When I uninstalled Python32 to ensure I could only open in Python35, the problem was solved.

The other problem, which led to this problem, was subprocess.run() opening several Firefox windows if there was not already an instance of Firefox running. The solution was to add a time.sleep() in between the first and second calls to subprocess.run(). For me, eight seconds was a good amount of time for the process to get going and allow the first page to partially load, which made the second page load faster. Since my machine is old, I ended up putting a sleep in between each tab--it looks a lot smoother, but it adds twenty seconds to the process; not very elegant.

If you landed here looking for a way to get results without getting Selenium, my advice: get Selenium. I'm pretty sure it would have saved me time and frustration had I just downloaded and learned it.

The finished code (had to break the links because my account is new):

import os
import subprocess
import time

os.startfile(r'C:\Program Files\Mozilla Thunderbird\thunderbird.exe')

subprocess.run(r'start firefox -new-tab unity3d[breaking link]/learn/tutorials', shell=True)

time.sleep(8)

subprocess.run(r'start firefox -new-tab answers.unity3d[breaking link]', shell=True)

time.sleep(3)

subprocess.run(r'start firefox -new-tab unity3d[breaking link]/learn/tutorials/topics/scripting', shell=True)

time.sleep(3)

subprocess.run(r'start firefox -new-tab docs.unity3d[breaking link]/Manual/index.html', shell=True)

time.sleep(3)

subprocess.run(r'start firefox -new-tab docs.unity3d.[breaking link]/ScriptReference/index.html', shell=True)

time.sleep(3)

os.startfile(r'C:\Program Files\TortoiseHg\thgw.exe')

os.startfile(r'C:\Program Files\Unity\Editor\Unity.exe')

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