Skip to content
Advertisement

Running .py files from a .py file

I am trying to make a simple AFK script for if I need to leave my pc for short period and the code works on it’s own but when I try to use one script to run another, It runs but then stops a few seconds later with exit code 0. I’m not sure what’s wrong and I’ve tried multiple things such as:

JavaScript

And that doesn’t seem to work. Every site I find tells me to use the above example or stuff such as exec which I’ve been told is dangerous. Note: a text file named ‘bridge’ will have to be created so the file can be stopped
main.py

JavaScript

output.py

JavaScript

I get that having the two systems apart can be easily avoided and will be fixed later, but the answer to this question will help me with other projects

Advertisement

Answer

For me (Python 3.8), your code works fine if you simply type on the terminal

JavaScript

provided that you comment out or delete the last line in output.py:

JavaScript

which would execute function run upon importing output in main. The program also works if I import from a local module

JavaScript

If for some reason you’re trying to import output from a different directory, you may have to deal with relative imports — a subject nothing to do with the specific implementation of your scripts.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement