Exec() imported module throws error


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

# 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.

A likely needed Repl link:

1 Like

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

1 Like

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

from .file import read_file

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

1 Like

no it is because he tried importing that function in src/funcs/utils.py not src/__init__.py

and btw it’s init with two _ (it’s called a “magic” function)

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.

def file_exec(path):
    exec(read_file(path), globals(), locals())

Thanks for the reply, but no luck on this one. The weird thing is this works from the Shell, so no idea why it wouldn’t work normally.

I use exec() like this:

def _ex(dir):
   open(dir, "r").read(),

It works perfectly fine for me so.

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.

I don’t know maybe it has something to do with the package itself?

1 Like

func, do you think he’s messed with some of the setup files or smth

Doesn’t seem like it, I set up a scenario with the exact same circumstances in another Replit, works fine. And if I run it using Shell, works fine.

1 Like

what file are you running tho

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.


k so what are you running exactly

Silly me. I guess I wasn’t googling thoroughly enough. It turns out that adding an empty dictionary as the globals parameter fixes this.

from func.file import read_file

def file_exec(path):
    exec(read_file(path), {})

I appreciate all of the help, and apologies to disregard googling a bit.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.