Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import a python file from a different server?

I am very confused because I have a python server up and running on https://python-server-password-manager.wotsitgamer.repl.co/. On that server there is a file named "main.py". I need to link a function from that file to the local python application. I tried to use the following:

import https://python-server-password-manager.wotsitgamer.repl.co/

... but that just gives me an error.

Any help would be great.

like image 281
Destiny Wotsit Avatar asked Jul 03 '26 08:07

Destiny Wotsit


2 Answers

You cannot directly import a file remotely, but you can download it then execute it by importing it.

from requests import get

# Download the file
code = get("https://python-server-password-manager.wotsitgamer.repl.co/main.py").text

# Write the data to a file
with open("main.py", "w") as f:
    f.write(code)

# Run the code
import main

Update:
As mentioned in the comments, OP wants to interact with the remote script as if it is located locally (specifically, transferal of data). In that case, there is not really a better option than running an API on the website, or a shared database of some sort (e.g., Firebase).

like image 193
Xiddoc Avatar answered Jul 04 '26 23:07

Xiddoc


You can play around with Pythons ModuleFinder.

I have never done it, but this project is an example for that.

It is in my opinion a bit of an over kill, and a more trivial solution will be to fetch the files to your local machine, and import in the usual way.

like image 20
Gai Ashkenazy Avatar answered Jul 04 '26 23:07

Gai Ashkenazy