1

Imagine I had the file "module.py" on a webserver. I get the files contents by doing the following

import urllib.request
response = urllib.request.urlopen('http://example.com/module.py')
content = response.read()

The file "module.py" contains the function

def test():
    print("test")

What would be the best way to import the content so I could use the test function? I have seen a few solutions but they no longer seem to work.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Boaprivate
  • 35
  • 1
  • 1
  • 11

3 Answers3

1

So this seems to work for me

import wget
# url = url of the code, path = where you want it saved
wget.download(url, path)

import sys
sys.path.append(path) 
import module.py #this should be the name of the file that is downloaded
mahum
  • 49
  • 3
0

Write down the content of module.py to a local py file. And then import it.

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
0

In response to question on previous answer:

If you have the module.py saved to say, local.py file (in the same folder) you can just:

import local
logic
  • 1,739
  • 3
  • 16
  • 22