Python get the code from a link or server - python-3.x

Okey so i dont want to put my code in my python.py file
i want to load it from a pastebin/raw link.
example:
import requests
import re
r = requests.get('https://pastebin.com/raw/example')
run(r)
can this be posible in any way ?
why i need to do this ?
i have more than 25 scripts i need to update on 5 different systems, so i dont want to go in each system and update each py file
so i just need to edit my Pastebins from 1 system.
thanks.

Related

Moving up directory to access file not using raw path in Python

I have a project named testrequest and I am working on a file named test_function_pack.py. The working code within the said file is below:
service = Service(r'C:\user\Dev\Selenium\testrequest\drivers\chromedriver.exe')
I wish to try accessing the chromedriver.exe file using .. or . dots and by my understanding, the distance of the two file is two directories upward. I tested using the below code
import os
os.chdir('..')
service = Service(r'../../drivers/chromedriver.exe')
But the code above does not work, am I missing anything here? Any help is appreciated.
Here is the folder by the way:
I guess something like this should work.
import os
stepups = 2
a = '/'.join(os.path.dirname(__file__).split('\\')[:stepups]) + '/drivers/chromedriver.exe'
print(a)
Do your own work regarding the step-ups you require, and change that accordingly.

Change background of windows 10 using python

I was wondering that can we change windows 10 background using python i did a bit research and find a module name ctypes but the problem was that it needs a image to apply but i want that instead of a locally saved file it should use a url of a image and refresh the background at a interval with a new image. Cuz i want to create a script which will apply an anime wallpaper in every 5 minute and the url will be taken from a api.
Please Help!
On Windows with python2.5 or higher, use ctypes to load user32.dll and call SystemParametersInfo() with SPI_SETDESKWALLPAPER action.
For example:
import ctypes
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0)
Your script will have to do that in two steps
Download the image from the url using a package like requests
Set the Windows Background

ImportError: No module named 'TkTreectrl'

I am pretty new to python and am trying to use the MultiListbox. It says I need to import TkTreectrl. I have already done import TkTreectrl as treectrl but it still give me the error.
After some reading online, I was under the impression I am supposed to add some file to the folder my program is in?
Can someone help me understand how exactly I can do this?
Here is a little checklist of what to do to get the file running:
1) Download the package file from the internet or get it whereever you can get it from
2) Unzip it (if nessecary)
3) Move it into the directory of your code file
4) Run your code, if there are no errors it will run.
I assume that the MultiListbox modules are in subfolders, you will need to call them like this:
from MultiListbox.subfolder import *
or
import Multilistbox
I hope I could help you!

Write to %temp% with python?

I have a small program that works fine on my PC but I want to make it portable. What it does is download an image from the internet, set as desktop background, wait one minute and update the image. I know that I cannot write directly to folders like appdata, as I do not know the username of the person using the computer. I need to save the downloaded image somewhere, so I would save it in the windows Temp folder.
Some options I think would be to (However I don't know how to do this in python)
Use something like %temp% to access the folder.
Find out the username of the person running the program and insert into path
Use a variable for the username.
Use relative paths
I would like to try and not have to use another module not by default included in Python 3, as I want to cx_freeze it later on.
import pythoncom
from urllib import request
from win32com.shell import shell, shellcon
from time import sleep
def get_image():
f = open('C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg', 'wb') #Open old image
f.write(request.urlopen('blalbla.com/foo/img.jpg').read()) #Get new image and write
f.close()
pathtoimg = 'C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg'
count = 0
while 1:
get_image()
iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IActiveDesktop)
iad.SetWallpaper(pathtoimg, 0)
iad.ApplyChanges(shellcon.AD_APPLY_ALL)
count += 1
print(count)
sleep(60)
Use this to locate Temp:
import os
mytmpdir = os.environ['TEMP'] #Must be uppercase

python 3.3 basic error

I have python 3.3 installed.
i use the example they use on their site:
import urllib.request
response = urllib.request.urlopen('http://python.org/')
html = response.read()
the only thing that happens when I run it is I get this :
======RESTART=========
I know I am a rookie but I figured the example from python's own website should be able to work.
It doesn't. What am I doing wrong?Eventually I want to run this script from the website below. But I think urllib is not going to work as it is on that site. Can someone tell me if the code will work with python3.3???
http://flowingdata.com/2007/07/09/grabbing-weather-underground-data-with-beautifulsoup/
I think I see what's probably going on. You're likely using IDLE, and when it starts a new run of a program, it prints the
======RESTART=========
line to tell you that a fresh program is starting. That means that all the variables currently defined are reset and/or deleted, as appropriate.
Since your program didn't print any output, you didn't see anything.
The two lines I suggested adding were just tests to figure out what was going on, they're not needed in general. [Unless the window itself is automatically closing, which it shouldn't.] But as a rule, if you want to see output, you'll have to print what you're interested in.
Your example works for me. However, I suggest using requests instead of urllib2.
To simplify the example you linked to, it would look like:
from bs4 import BeautifulSoup
import requests
resp = requests.get("http://www.wunderground.com/history/airport/KBUF/2007/12/16/DailyHistory.html")
soup = BeautifulSoup(resp.text)

Resources