是否有任何选项包括托管在在线Web主机上的Python脚本?

2024-07-04 18:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个小python项目,我想知道是否有任何方法可以导入托管在我的网页上的脚本。该脚本包括一些变量,如

CurrentV = 'The current version is 1.2'

…所以我不需要发短信告诉每个人我制作了一个新版本。他们可以在他们的脚本中看到它,因为在我的web主机上更新版本要快得多

我这样试过:

import requests
oimport = requests.get('https://website.com/version.py')
from oimport.content import CurrentV

print(CurrentV)
print('your current version is 1.1')

但不是这样的


Tags: the项目方法import版本脚本web网页
1条回答
网友
1楼 · 发布于 2024-07-04 18:34:26

一种方法是将“模块”写入磁盘,然后导入。例如:

from pathlib import Path
import sys

oimport = requests.get('https://website.com/version.py')

module_path = "./modules"

# Write the code to modules/version.py
with open(f"{module_path}/version.py", "w") as module_file:
  module_file.write(module_path)

# Make sure our path in sys.path
sys.path.append(module_path)

from version import CurrentV

这将以代码注入的形式打开一罐蠕虫!但它确实/应该做你需要它做的事

相关问题 更多 >

    热门问题