Hello,
I would like to refactor my code in several python files and therefore use functions written in a .py file from another .py file. The two python files are in the same directory, so I try to import the file with the “import …” syntax but a ModuleNotFoundError is raised.
Can anyone tell me if what I want to do is possible and how?
Thanks!
Yes indeed! If you add the <py-config>
element to the page, you can use one of its parameters to load files from the network into the Emscripten local file system that Pyodide creates “within the browser window”, so to speak. This local file system is where Python looks for files when using import
.
Let’s say we want to load a file from the url some/url/to/mymodule.py
to a file located at ./mymodule.py
in the local filesystem. That is to say, in the same folder that Python scripts are executed from.
In the current release (2022.09.1), the syntax is:
<!-- PyScript 2022.09.1 -->
<py-config>
paths = ['some/url/to/mymodule.py']
</py-config>
<py-script>
import mymodule
</py-script>
The upcoming release (2022.12.1 probably) introduces a powerful new feature called “Fetch Configurations ”; there are a few ways to get the file where it’s wanted with a fetch configuration, but for this URL and destination, one solution is:
<!-- PyScript 2022.12.1 (probably) -->
<py-config>
[[fetch]]
from = 'some/url/to'
files = ['mymodule.py']
</py-config>
<py-script>
import mymodule
</py-script>
1 Like
Thank you for your quick response!
It was quite simple after all. However, I can’t use a Pyscript function such as Element(‘…’).write(‘…) :’(
I saw that this problem was raised on github (issue 525):
opened 05:47AM - 18 Jun 22 UTC
type: bug
needs-triage
### Checklist
- [X] I added a descriptive title
- [X] I searched for other issu… es and couldn't find a solution or duplication
- [X] I already searched in Google and didn't find any good information or help
### What happened?
On importing a child python script via py-env, the child script is unable to use pyscript and Element().write().
This forces to return the value to main python script and write in the main file.
Alternatively, I could use js.document.getElementById and write through that, but that requires importing js and also I lose out on the formatting MIME conversions done by pyscript.write as mentioned here - https://github.com/pyscript/pyscript/blob/main/pyscriptjs/src/pyscript.py#L49-L104
https://jeff.glass/post/pyscript-intro/
### What browsers are you seeing the problem on? (if applicable)
Chrome, Other
### Console info
```shell
pyodide.asm.js:14 Uncaught (in promise) PythonError: Traceback (most recent call last):
File "/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception
File "/lib/python3.10/asyncio/tasks.py", line 232, in __step
result = coro.send(None)
File "<exec>", line 18, in my_fetch
File "/home/pyodide/dom_writer.py", line 55, in visualise
pyscript.write('pie', fig)
NameError: name 'pyscript' is not defined
at new_error (pyodide.asm.js:14:238191)
at pyodide.asm.wasm:0xedbcb
at pyodide.asm.wasm:0xf1a0e
at method_call_trampoline (pyodide.asm.js:14:238105)
at pyodide.asm.wasm:0x134c2c
at pyodide.asm.wasm:0x217a84
at pyodide.asm.wasm:0x174a14
at pyodide.asm.wasm:0x135149
at pyodide.asm.wasm:0x135243
at pyodide.asm.wasm:0x1352e6
at pyodide.asm.wasm:0x1fff83
at pyodide.asm.wasm:0x1f98b5
at pyodide.asm.wasm:0x135329
at pyodide.asm.wasm:0x201f1b
at pyodide.asm.wasm:0x1ff9ff
at pyodide.asm.wasm:0x1f98b5
at pyodide.asm.wasm:0x135329
at pyodide.asm.wasm:0xf16d8
at Module.callPyObjectKwargs (pyproxy.gen.ts:360:23)
at Module.callPyObject (pyproxy.gen.ts:384:17)
at wrapper (pyodide.asm.js:14:205222)
```
### Additional Context
_No response_
Do you know if it is possible today to make calls to Element from the side script?
In the upcoming release, it will be possible to use import pyscript
or from pyscript import Element
etc. thanks to some work done around PR #961 .
That next release is coming quite soon! Release Candidate 1 is out now: PyScript 2022.12.1.RC1 . Feel free to check it out.
1 Like
Thanks, I’ll keep an eye on it!
i use your code @JeffGlass
<py-config>
[[fetch]]
files = ['example.py']
from = '../wp-content/themes/codetheme/py/'
</py-config>
<py-script>
import example
example.hello()
</py-script>
AttributeError: module ‘example’ has no attribute ‘hello’
#!/var/www/u0745362/data/env/bin/python
import cgi
print("Content-type: text/html\n")
def hello():
print("ok")
if i’m use code on server side without pyscript between two py-files and import module from one to second file it works fine
import example
example.hello()