[英]How do I import a python file from a different server?
我很困惑,因為我有一個 python 服務器在https://python-server-password-manager.wotsitgamer.repl.co/上運行。 該服務器上有一個名為“main.py”的文件。 我需要將該文件中的 function 鏈接到本地 python 應用程序。 我嘗試使用以下內容:
import https://python-server-password-manager.wotsitgamer.repl.co/
...但這只會給我一個錯誤。
任何幫助都會很棒。
您不能直接遠程導入文件,但可以下載它然后通過導入執行它。
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
更新:
如評論中所述,OP 希望與遠程腳本進行交互,就好像它位於本地一樣(特別是數據傳輸) 。 在這種情況下,沒有比在網站上運行 API 或某種共享數據庫(例如 Firebase)更好的選擇了。
Python 不支持超過 HTTP 的導入。您必須以某種方式(有多種方法)map 一個虛擬驅動器到您的本地計算機,然后將該位置添加到sys.path
,以便 Python 可以找到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.