Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while True: error
#1
I have the following code and getting a syntax error on the while True: statement on the 3rd from the last line. Everything else seems to be working fine. Can anyone assist?
import bs4 as bs
import sys
import schedule
import time
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl

import winsound
frequency = 2500
duration = 1000


class Page(QWebEnginePage):

    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print('Load finished')

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()

def exact_url(url):
    index = url.find("B0")
    index = index + 10
    current_url = ""
    current_url = url[:index]
    return current_url

def mainprogram():
    url = "https://amzn.to/2M9IxmW"
    exacturl = exact_url(url)
    page = Page(exacturl)
    soup = bs.BeautifulSoup(page.html, 'html.parser')
    js_test = soup.find('span', id='priceblock_ourprice')
    if js_test is None:
        js_test = soup.find('span', id='priceblock_dealprice')
        str = ""
        for line in js_test.stripped_strings:
            str = line

str = str.replace(", ", "")
current_price = int(float(str))
your_price = 150
if current_price < your_price:
    print("Price decreased book now")
    winsound.Beep(frequency, duration)
else:
    print("Price is high please wait for the best deal")
def job():
    print("Tracking....")

mainprogram(schedule.every(1).minutes.do(job)

    while True:
            schedule.run_pending()
            time.sleep(1)

obviously, my formatting was lost. I am very new at Python (programming in general) so not sure how to paste the actual code with formatting. Thanks again and apologies for the newbie questions and errors!!!!
Reply
#2
Missing a ) from the end of mainprogram(schedule.every(1).minutes.do(job)
mainprogram is being passed an argument but the definition has no parameters
while True: is indented
Reply
#3
Thanks so much!!
Reply
#4
So Yoriz was kind enough to help fix my while True error but now I am having issues with the schedule.

From Terminal, I entered $ pip install schedule and it returned the following:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: schedule in ./.local/lib/python2.7/site-packages (0.6.0)

However when I run the script below using Mu_Code if gives the error shown below. How can I fix this?


import bs4 as bs
import sys
import schedule
import time
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl

import winsound
frequency = 2500
duration = 1000


class Page(QWebEnginePage):

    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print('Load finished')

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()

def exact_url(url):
    index = url.find("B0")
    index = index + 10
    current_url = ""
    current_url = url[:index]
    return current_url

def mainprogram():
    url = "https://amzn.to/2M9IxmW"
    exacturl = exact_url(url)
    page = Page(exacturl)
    soup = bs.BeautifulSoup(page.html, 'html.parser')
    js_test = soup.find('span', id='priceblock_ourprice')
    if js_test is None:
        js_test = soup.find('span', id='priceblock_dealprice')
        str = ""
        for line in js_test.stripped_strings:
            str = line

str = str.replace(", ", "")
current_price = int(float(str))
your_price = 150
if current_price < your_price:
    print("Price decreased book now")
    winsound.Beep(frequency, duration)
else:
    print("Price is high please wait for the best deal")
def job():
    print("Tracking....")

mainprogram(schedule.every(1).minutes.do(job))

while True:
            schedule.run_pending()
            time.sleep(1)
Error:
Traceback (most recent call last): File "/home/pi/mu_code/amazon_price_checker.py", line 3, in <module> import schedule ModuleNotFoundError: No module named 'schedule'
Reply
#5
(Oct-04-2019, 03:59 PM)2wheelz4me Wrote: Requirement already satisfied: schedule in ./.local/lib/python2.7/site-packages (0.6.0)
Have you more than one Python installations on your computer?

Why would you use python 2.7 if it is going to be deprecated in less than three months???!!!

Please, check here: We have decided that January 1, 2020, will be the day that we sunset Python 2.

or here: Python 2.7 will retire in...

Error:
Traceback (most recent call last): File "/home/pi/mu_code/amazon_price_checker.py", line 3, in <module> import schedule ModuleNotFoundError: No module named 'schedule'
Have you tried python -V (sorry, not sure how it is in Linux) to see what version of python you have?

And then, have you tried schedule -V (sorry, not sure how it is in Linux) to see what version of schedule you have and where it is?

All the best,



MORE ON THIS ISSUE ...
I have just found another thread for a linux user having the same problem in here. The solution that some other user gave, and that seems to solve the problem is:

"According to these logs, i found that the PYTHONPATH is different in manual shell and systemd. I tried to add "/home/ubuntu/.local/lib/python3.5/site-packages" into /etc/profile but systemd logs show that it still can't found the path.
So I did a stupid thing, I added

sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")
in my code, and it works..."

All the best,



EVEN SOME MORE ON THIS ISSUE ...
On the same site, and maybe more geared to your problem, as I think you have two versions of python installed is this question and answer.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
So I checked and in fact have both instances running (2.7 and 3.7). I tried inputting "sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")" into my script and still got the same error.
Error:
Traceback (most recent call last): File "/home/pi/mu_code/amazon_price_checker.py", line 3, in <module> import schedule ModuleNotFoundError: No module named 'schedule'
The other option was to use virtualenv but I don't have it and have not been using it.

Still not sure how to fix this.
Reply
#7
(Oct-04-2019, 05:45 PM)2wheelz4me Wrote: So I checked and in fact have both instances running (2.7 and 3.7). I tried inputting "sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")" into my script and still got the same error.
Keep in mind that you are not that user! You have to use the appropriate direction to the folder where YOU have the schedule module (I mean, you said you used "sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")" into your script). You have to do the necessary changes to your personal case.

Otherwise, you could try doing it in python 3.7 by typing python3 (if you are using Linux) from your command prompt, and then pip install from there to look for the schedule module and install it there, with pip install schedule.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
Fully get that I am not that user but thought it would still get me to the right place. I'll try and see if I can execute the script from Python 3 via Terminal. Thanks again so much!! Still very new at all this and trying to learn!
Reply
#9
(Oct-04-2019, 07:58 PM)2wheelz4me Wrote: Fully get that I am not that user but thought it would still get me to the right place.
It could, but making the necessary changes (your python is not python3.5 like that person, nor you have the same folders than that person).

According to the message you received when you installed schedule:
(Oct-04-2019, 03:59 PM)2wheelz4me Wrote: Requirement already satisfied: schedule in ./.local/lib/python2.7/site-packages (0.6.0)
schedule is installed in that path, so you could adapt:
sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")
that is the path from that person, to your personal case, maybe something like:
sys.path.append("./.local/lib/python2.7/site-packages")
or maybe even something like this:
sys.path.append("./.local/lib/python2.7/site-packages (0.6.0)")
And then, you have to run your program amazon_price_checker.py from python 2.7, because it's where you have installed schedule. If you try to run the program from python 3.7 (that is probably what you have done), the schedule module is not going to be found, because it is not there!!!


(Oct-04-2019, 07:58 PM)2wheelz4me Wrote: I'll try and see if I can execute the script from Python 3 via Terminal. Thanks again so much!! Still very new at all this and trying to learn!
Please, keep in mind, that according to the message you received when installing schedule, YOU DON'T HAVE THE SCHEDULE MODULE INSTALLED IN PYTHON3, so you have to install the schedule module ALSO IN PYTHON3, if you want to use the program amazon_price_checker.py from python 3.7.

Don't worry! I'm also a newbie, with just a few more weeks at it, so I can fully understand this kind of problems!!!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while True error Nickd12 2 2,081 Sep-13-2020, 06:29 AM
Last Post: Nickd12
  While True is a syntax error? Piethon 8 13,646 Jul-21-2019, 12:40 PM
Last Post: metulburr
  Returning True or False vs. True or None trevorkavanaugh 6 9,265 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020