2

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.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37

3 Answers3

1

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).

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
  • I get this error: Import "requests" could not be resolved from source – Destiny Wotsit Mar 28 '22 at 13:56
  • but this way it is actually importing local file. – Lei Yang Mar 28 '22 at 14:00
  • @LeiYang you are correct. He never said that wasn't an option. Maybe I misunderstood? – Xiddoc Mar 28 '22 at 14:04
  • @DestinyWotsit run `python -m pip install requests` – Xiddoc Mar 28 '22 at 14:06
  • *my goal is to store input from the local app. The app then sends the data to the server and vice versa* -- the OP has such goal in comments. download source doesn't solve. – Lei Yang Mar 28 '22 at 14:06
  • @LeiYang you are correct yet again. I did not read all the comments, I will see if I can find a better solution (although the obvious answer is to just use a database). – Xiddoc Mar 28 '22 at 14:08
  • @Xiddoc side note, whenever i use pip to install something (including this) it just gives me this: "Could not install packages due to an OSError" – Destiny Wotsit Mar 28 '22 at 14:09
  • @DestinyWotsit Try running the command prompt with Admin permissions. – Xiddoc Mar 28 '22 at 14:10
  • @Xiddoc what database would I use and how would I do it. (I've never used a database before lol) – Destiny Wotsit Mar 28 '22 at 14:10
  • @DestinyWotsit Personally, I'd recommend writing an API, since your data is already being hosted on a Web app. If you have any further questions, I suggest you ask in a new thread as this is becoming open ended discussion. Hope I helped, have a nice day! – Xiddoc Mar 28 '22 at 14:13
1

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.

0

Python doesn't support imports over HTTP. You'll have to somehow (there are various methods) map a virtual drive to your local computer, then add that location to sys.path so Python can locate it.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • It's something you do through your operating system. How to do it is really outside the scope of this question, but there are many resources on Google. On Linux you can use `sshfs` to create a file system over SSH, and there are similar interfaces for Windows. You may also want to research FUSE, as there is an HTTPFS module that I saw. – MattDMo Mar 28 '22 at 13:51