Hello,
I am trying to execute a function that I have written in JavaScript and exposed to eel, but from another python file.
At the moment, I am just doing
import gui
gui.run_test_function("x")
However, that results in
Traceback (most recent call last):
File "main.py", line 328, in
main()
File "main.py", line 314, in main
gui.run_test_function("aaaaaa")
File "../interfacegui.py", line 102, in run_test_function
eel.test_function(test_text)
AttributeError: module 'eel' has no attribute 'test_function'
However, if I run it from within the gui.py folder, it works perfectly.
How can I fix this?
Thanks.
Please can you share a link to your git repo/code or otherwise include more details about your directory structures, files, and code. This will help with debugging. It looks like you might be importing the wrong eel into your gui.py folder - but hard to say for sure without more information.
@samuelhwilliams
Here is my file structure
node_modules
web
css
main.css
html
index.html
images
...
js
...
themes
...
gui.py
main.js
package.json
package-lock.json
renderer.js
src
main.py
Here is the relevant part of my gui.py script
import eel
options = {
'mode': 'custom',
'args': ['node_modules/electron/dist/electron.exe', '.']
}
def run_test_function(test_text):
eel.test_function(test_text)
if __name__ == "__main__":
open("console.txt", "w").close()
eel.init('web')
eel.start('html/index.html', options=options)
And main.py:
sys.path.insert(0, '../interface')
import gui
gui.run_test_function("test")
And here's a script in my index.html file
<script type="text/javascript">
eel.expose(test_function);
function test_function(test_text) {
alert(test_text);
}
</script>
I don't know exactly how these things work, but I guess when I'm importing the gui file and trying to run the function, it's not running because the whole script isn't running. It's just trying to execute that function in isolation, and has nothing to do with the other gui.py instance that's running. Assuming that is correct, I'm not sure how to overcome it.
Ok, the problem is in how you're executing the files and your understanding of the __name__ variable.
The file that you execute is the entrypoint to the program, and within that file, __name__ resolves to __main__. In other files/modules/etc that get imported into the program, __name__ resolves to the name of that file. This allows Python scripts to work out which file was the entrypoint to the program.
When you run gui.py directly, within that file __name__ resolves to __main__. Because of this, the conditional block of code in that file gets run - initialising eel and starting it running.
When you run main.py instead, the __name__ reference in gui.py now resolves to just the word gui.py (or potentially some path leading to that file). So the conditional block doesn't get run when you import that module, which means eel never gets initialised, and so never communicates with javascript to work out what functions are available.
Further reading about __name__ available here (or via Google): https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/
TL;DR I think you need to initialise eel in main.py before using the gui.run_test_function function.
Let me know if this helps - I haven't actually tested this.
@samuelhwilliams I understand. I fixed it by merging the two files. Thanks for your help.
Most helpful comment
@samuelhwilliams I understand. I fixed it by merging the two files. Thanks for your help.