I have installed Anaconda on my PC, and I've also added Anaconda to path. I've made a script which works fine in Jupyter, and it also works fine when I call it from the Anaconda Prompt terminal. However, when I try to execute it as a Python script from my desktop, nothing happens. I don't even get an output, so I can see what's going wrong. This is the script:
from bs4 import BeautifulSoup
import requests
import re
import urllib.request
import os
from tkinter import *
from PIL import Image, ImageTk
import tkinter as tk
from tkinter import Tk, Menu, Canvas
import datetime
from pathlib import Path
url = requests.get("https://politiken.dk/underholdning/wulffmorgenthaler/")
html = url.text
soup = BeautifulSoup(html,'lxml')
main = soup.find_all("a", attrs={"data-prefetchable":"true"})
list_of_links = []
for i in main:
list_of_links.append(re.findall(r'(https://politiken.dk/underholdning/wulffmorgenthaler/.{1,34}(?="))', str(i))[0])
list_of_links = list(dict.fromkeys(list_of_links))
counter = 0
def next_day():
global counter
counter += 1
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
def prev_day():
global counter
if counter == 0:
pass
else:
counter -= 1
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
def image_downloader(date_numbers):
global counter
my_file = Path(fr"C:\Users\Peter\Desktop\Wolfmorgenthaler/{date_numbers}.jpg")
if my_file.exists():
open_img(date_numbers)
elif counter<len(list_of_links):
new_url = requests.get(list_of_links[counter])
new_html = new_url.text
new_soup = BeautifulSoup(new_html,'html')
new_main = new_soup.find_all("img", attrs={"class":"media__image image--fullwidth"})
new_picture_links = re.findall(r'https.+? ', str(new_main[0]))
final_link = new_picture_links[0]
fullfilename = os.path.join(r"C:\Users\Peter\Desktop\Wolfmorgenthaler", f"{date_numbers}.jpg")
urllib.request.urlretrieve(final_link, fullfilename)
open_img(date_numbers)
else:
counter = counter - 1
def open_img(name):
filepath = fr"C:\Users\Peter\Desktop\Wolfmorgenthaler/{name}.jpg"
img = Image.open(filepath)
img = img.resize((960, 449), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
panel = Label(root, image=img)
panel.image = img
panel.grid(row = 1, columnspan=4)
root = Tk()
root.geometry("964x483")
root.resizable(width=True, height=True)
today = datetime.date.today()
date = today - datetime.timedelta(days=counter)
final_date = date.strftime("%m%d")
image_downloader(final_date)
btn1 = Button(root, text='Næste billede', command=next_day, width = 67).grid(row = 2, column = 2)
btn2 = Button(root, text='Forrige billede', command=prev_day, width = 67).grid(row = 2, column = 1)
root.mainloop()
I just reset my PC today, and it worked just fine before I reset it. I have no idea how to get it to work. I would like to just have it be a clickable icon on my desktop, which executes the script.
Executing it with a batch file did not work either. Any ideas?