Recently, I’ve been facing many issues with my pyos3 project, and yet another has occurred.
I have a function, file_exec(), which takes a file, reads it, and calls exec() on it. However, when using it, using imported functions does not seem to work. Here’s an example:
from func.util import file_exec
file_exec("install.py")
# Successfully imports the modules from install.py without any problems,
# but throws an error when imported functions are used?
This also only occurs when using the Run button, and seems to work if the shell is used. However, this doesn’t make any sense. I had configured the Run button to execute a Bash script that would cd into the folder containing install.py and execute boot.py (the file that is represented with the code above), so if the same Bash command is being called, why isn’t it working? Also, there are no errors in install.py, and syntax highlighting and such is showing that it is in fact imported correctly.
After many tries of debug, I still cpuldn’t figure out why.
However here is some clues for the others who also wanna help with this:
In install.py, things are all working fine until the main function is being called, where every single variable except local is gone, no imported module, even the website_url variable is gone.
Moving the entire code in install.py to replace the exec in util.py works, idk why
because you need relative imports basically what you’re doing is you’re importing assuming that the run is occuring within the src folder but if you run it from the root then when you run import func.util it’ll try to import that from root instead of properly from src so you should have instead
Well that is not the case here, since there is an __init__.py it is actually considered a module instead of a file, so you can call it anywhere no matter from root or from inside
Try to modify your file_exec() function to pass the globals and locals parameters, this will make the namespaces of your file_exec function available to the code.
Yep, tested this within a completely new Repl with all of the same configuration as the normal, and it works fine. Will I have to migrate to a new Repl?
Just tried creating a fork, the same issue occurs.
Well, I just realized I was running install.py in Shell instead of boot.py. Running install.py directly works as expected, but running boot.py throws errors.