3

I want to create a local python script that reads code from Github and executes it on my computer.

This will ensure that the latest version of code is always being used.

Here is the raw python code:

I have tried this, but doesn't work...


    with open(script) as file:
        data = file.read()

also

    exec(script)

I feel like this should be easily done but can't figure it out! Any help much appreciated

IAmAliYousefi
  • 1,132
  • 3
  • 21
  • 33
Ben Sharkey
  • 313
  • 3
  • 15
  • 1
    That's not how you should ensure that the latest version is ran. This is usually not a responsibility delegated to a Python script. Instead, write a `run` script which pulls from your master branch then executes the script. – Olivier Melançon Mar 02 '19 at 22:53

1 Answers1

2

You have the URL, you have the file reading and you have the execution. You're just missing the step where you download the file.

Easiest to use urllib in Python3:

import urllib.request

code = 'https://raw.githubusercontent.com/bensharkey3/Guess-The-Number/master/Guess%20the%20number%20game.py'

response = urllib.request.urlopen(code)
data = response.read()

exec(data)

Note that relying on the URL is a pretty fragile way to ensure you have the latest code. Much better would be to use git to pull the latest, but this should at least get you started.

Heath Raftery
  • 3,643
  • 17
  • 34