juju toolz
juju toolz

Reputation: 41

Python using a variable as your source code

so I am trying to use the requests module to make an auto-updater for my program. I want to take the text from a Pastebin file and use that as my code. This is what I've done so far.

import requests

coderaw = requests.get('https://pastebin.com/raw/s3d1kWgu')

code = coderaw.text

So I want the "code" variable to be my source code. Does anybody know a module or an operator that lets me do this?

Thanks.

Upvotes: 0

Views: 58

Answers (2)

Jaime
Jaime

Reputation: 1631

You can use exec to execute the code in coderaw, for example:

my_code = 'print ("Hello World!")'
exec(my_code)

Using your code:

import requests
coderaw = requests.get('https://pastebin.com/raw/s3d1kWgu')
code = coderaw.text
exec(code)

Upvotes: 1

Keith
Keith

Reputation: 43024

If you are willing to take the risk then you can just exec it.

Upvotes: 0

Related Questions