Python import module only works in a new terminal window - python

I'm trying to run the following python script:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://www.everlane.com/collections/mens-luxury-tees/products/mens-v-antique')
driver.save_screenshot('screen.png') # save a screenshot to disk
print driver.current_url
images = driver.find_elements_by_tag_name('img')
for image in images:
print image.get_attribute('src')
However, every time I try to run it, I get this error:
FitValet-MacBook-Pro:desktop fitvalet$ python selenium.py
Traceback (most recent call last):
File "selenium.py", line 1, in <module>
from selenium import webdriver
File "/Users/fitvalet/Desktop/selenium.py", line 1, in <module>
from selenium import webdriver
ImportError: cannot import name webdriver
FitValet-MacBook-Pro:desktop fitvalet$
But I've installed the module using pip install selenium and it installed fine. When I run a new terminal window, enter python, and then type in from selenium import webdriver, it imports fine. If I exit() python, and then re-enter and try again, the same above error happens, in that it can't import selenium. If I re-open terminal, then it works again, but only in the terminal python window. I can even type out every line of code and it prints the images fine in the terminal!
It never works if I just try to run the script on its own. Any ideas as to why this is? Thanks!!!

WOW. I can't believe this, but my little script, which I so simply named "selenium.py", was the problem. The answer is, DON'T DO THIS! When the script said from selenium import webdriver, it somehow thought it was calling itself, and creating major errors.
I renamed the script to "myselenium.py" and it worked fine.

Related

Execute Python Program line-by-line automatically

I'm doing a python script with selenium. To do tests I use command promp, this way i know if my line of command is working without needing to execute the program over and over to correct line by line. The thing is, i need a way to automatically start this lines so i don't need to keep retype it everytime i want to do some tests.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = webdriver.ChromeOptions()
options.add_argument('lang=pt-br')
driver = webdriver.Chrome(executable_path=r'./chromedriver.exe', options=options
From the command line, running python -m pdb <yourscript.py> should open the Python Debugger and allow you to step through. Each line is run by entering n and returning.

The following code is running through python IDE but not from CMD or by direct execution

import webbrowser
import pyautogui
import time
webbrowser.open("https://meet.google.com/huq-etkk-pwv",new=0,autoraise=True)
#auto_input
time.sleep(10)
pyautogui.hotkey('ctrl','d')
pyautogui.hotkey('ctrl','e')
time.sleep(2)
#custom coordinates are adviced
#coordinates work on standard chrome window with 1920*1080 screen size 16:9 ratio
pyautogui.click(972,428)
When i run this via CMD i get an error in the module pyautogui with Tk() name not defined and when i try to execute this directly it just opens a window of CMD and then stops and does nothing but works perfectly when i run it via the Python IDE.
Your code works fine for me on my Kubuntu (18.04.)
Which IDE do you use? Is it possible, that it by default runs a different version of python than your CMD (e.g. 3.7 in the IDE and 2.7 in the CMD)?
P.S.
would have posted it in a comment but lack the reputation

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')

launchd failing to run python script with phantomJS

I have a python script that does some things online. Every 10 seconds launchd (OSX 10.11.3 El Capitan) launches a shell script, which calls the python script, which goes online using selenium webdriver.PhantomJS() (or should).
When running the shell script from the terminal, everything succeeds. When running in the configuration described above but with webdriver.Firefox() instead of webdriver.PhantomJS(), everything succeeds. However, when running through launchd -> shell -> python with webdriver.PhantomJS(), it exits with abnormal code: 1.
A simplified version of my code (with all the imports):
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.common.exceptions import NoSuchElementException
d = webdriver.PhantomJS()
d.close()
What am I missing? I would suspect full path names, but I do not explicitly import Firefox() either, and that does work...
Is it failing on an import failure? I had a similar-sounding problem, outlined here: Running Python Script with Launchd: imports not found
What was helpful: using LaunchControl, capturing Standard Error, and finding out python could not import a specific package. Not sure if that's your problem, but Standard Error will tell you what abnormal code: 1 actually is.

PyAutoGUI WindowsError: [Error 5]

I am working with PyAutoGUI in Python 2.7 using PyCharm and am getting a permissions error. Here is my code:
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Open the browser.
browser = webdriver.Firefox()
browser.maximize_window()
# Click on the address bar.
pyautogui.click(x=150,y=50)
# Fill in the URL.
pyautogui.typewrite('yahoo.com')
The windows is opening and maximizing just fine and the mouse is moving and clicking (I tried the code on my IDE and it moved and clicked the top toolbar), but alas, nothing is being typed. How do I fix the permissions so that I can click and type?
NOTE: The script DOES work when run from the command line.
Thanks.
This issue should be solved in 0.9.34, so you just need to update PyAutoGUI by running pip install -U pyautogui.

Categories