如何在 Web 服务器上的 Pyscript 中运行添加绝对路径到自定义 python 模块/库

问题描述 投票:0回答:1

我需要从自定义库运行我的代码并在客户端/浏览器显示它 当我在我的客户端托管 Web 端包含路径(根据建议 https://github.com/pyscript/pyscript/issues/519)时,我在从自定义库运行代码 Pyscript 时遇到问题

https://test.com/lib/python/example/index.html 带有代码👇
<html>
<head>
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css"/>
</head>
<body>    

<py-config>
    [[fetch]] 
paths = ["https://test.com/lib/python/example/custom/example.py"] #custom py-module
or 
[[fetch]]
url = "https://test.com/lib/python/example/"
folder = "custom/"
files = ["example.py"]
or
[[fetch]]
path = "lib/python/example/custom/example.py"
uri = "https://test.com/lib/python/example/custom/example.py"

# example.py
/* code file
def hello():
    print("Pyscript")
*/

</py-config>

<py-script>
    import example
    example.hello()
</py-script>
    

</body>
</html>

服务器上的路径:

with path
|---https://test.com/lib/python/example/custom/
|---------------------------------------------/index.html
|---------------------------------------------/example.py

or
|---https://test.com/lib/python/example/custom/
|---------------------------------------------/index.html
|---------------------------------------------/py/example.py

我有错误 - (PY1000):无法从路径确定文件名,请提供“to_file”参数。 😪

我如何解决它并通过网络托管上 Pyscript 的绝对路径运行我的自定义模块代码并显示它😞?

python python-module pyscript pyscripter
1个回答
0
投票

您使用了错误的

<py-config>
属性。您想要的属性是
files
,它采用(相对或绝对 URL:

<py-config>
  [[fetch]]
  files = ['example.py']
</py-config>
<py-script>
  import example
  example.hello()
</py-script>

默认情况下,所有加载的文件都放置在执行 Python 代码的同一目录中。如果要将其放置在 emscripten 虚拟文件系统中的不同文件夹中,请使用

to_folder
属性。要指定存储内容的特定文件名(例如,如果您从不以
[filename].py
结尾的 URL 加载内容),请使用
to_file

© www.soinside.com 2019 - 2024. All rights reserved.