简体   繁体   中英

Why am I receiving a name error for a name not defined that I haven't included in my code?

Using this code in my website update tool, I receive a NameError: name 'new_link1' is not defined. However I use the name new_link in my code, not new_link1. I am using Jupyter notebooks with some other notebooks running similar code if that might be having an effect. My code is below.

    import requests
    from bs4 import BeautifulSoup
    import schedule
    import time

    # Electricity Northwest Code   
    def get_link():
       url = 'https://www.enwl.co.uk/about-us/financial-investor-relations/information-for-investors/'
       reqs = requests.get(url)
       soup = BeautifulSoup(reqs.text, 'html.parser')

    changing_link = soup.find('a', {'class': 'c-downloads__link js-downloads__link'}).get('href')
    return changing_link

   def handle_changes_to_link(value):
       new_link = get_link()
       if not new_link == value:
            print('New Update from ENWL, remember to copy and paste new link: ', new_link)

   #When an update has occured copy and paste new link into value = 'https://new link here'  below

   schedule.every(30).seconds.do(handle_changes_to_link, value = 'https://www.enwl.co.uk/globalassets/investor-relations/documents/non-password-protected/enw-finance-plc-prospectus-july-2020')
   while True:
       schedule.run_pending()
       time.sleep(10)

And the error I see is:

    NameError: name 'new_link1' is not defined

Edit: full stack trace below

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [9], in <cell line: 25>()
     23 schedule.every(30).seconds.do(handle_changes_to_link, value = 'https://www.enwl.co.uk/globalassets/investor-relations/documents/non-password-protected/enw-finance-plc-prospectus-july-2020')
     24 while True:
---> 25     schedule.run_pending()
     26     time.sleep(10)

File /Applications/anaconda3/lib/python3.9/site-packages/schedule/__init__.py:780, in run_pending()
    776 def run_pending() -> None:
    777     """Calls :meth:`run_pending <Scheduler.run_pending>` on the
    778     :data:`default scheduler instance <default_scheduler>`.
    779     """
--> 780     default_scheduler.run_pending()

File /Applications/anaconda3/lib/python3.9/site-packages/schedule/__init__.py:100, in Scheduler.run_pending(self)
     98 runnable_jobs = (job for job in self.jobs if job.should_run)
     99 for job in sorted(runnable_jobs):
--> 100     self._run_job(job)

File /Applications/anaconda3/lib/python3.9/site-packages/schedule/__init__.py:172, in Scheduler._run_job(self, job)
    171 def _run_job(self, job: "Job") -> None:
--> 172     ret = job.run()
    173     if isinstance(ret, CancelJob) or ret is CancelJob:
    174         self.cancel_job(job)

File /Applications/anaconda3/lib/python3.9/site-packages/schedule/__init__.py:661, in Job.run(self)
    658     return CancelJob
    660 logger.debug("Running job %s", self)
--> 661 ret = self.job_func()
    662 self.last_run = datetime.datetime.now()
    663 self._schedule_next_run()

Input In [2], in handle_changes_to_link(value)
     16 new_link = get_link()
     17 if not new_link == value:
---> 18     print('New Update from ENWL, remember to copy and paste new link: ', new_link1)

NameError: name 'new_link1' is not defined

Answered in comments.

Solution was to restart Kernel.

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