Home > Software engineering >  Running .py files from a .py file
Running .py files from a .py file

Time:10-04

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:

import test1
test1.run()

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

import os
from output import run
import keyboard
from time import sleep
print('Start afk program? (Y/N)')
cmd = str.lower(input('> '))
if cmd == 'y':
    print('Use X to Stop')
    print('Starting in 10 seconds...')
    run()
    while True:
        if keyboard.is_pressed('x'):
            print('exit key pressed')
            x = '1'
        else:
            x = '0'
        if os.path.exists('bridge.txt'):
            with open('bridge.txt', 'w') as file:
                file.write(x)
                file.write('\n')
                file.close()
        else:
            exit('file not found')
        if x == '1':
            exit(0)
        sleep(0.1)

output.py

import os
from time import sleep
from pynput.keyboard import Controller
keyboard = Controller()


def run():
    global keyboard
    sleep(10)
    keyboard = Controller()
    count = 0
    while True:
        if os.path.exists('bridge.txt'):
            with open('bridge.txt', 'r') as file:
                content = file.readlines()
                for line in content:
                    if line[0] == '1':
                        exit(0)
        if count == 1:
            press_key('w')
        elif count == 2:
            press_key('a')
        elif count == 3:
            press_key('s')
        elif count == 4:
            press_key('d')
        elif count == 10:
            count = 0
            press_key('q')
        count  = 1
        sleep(0.1)


def press_key(key):
    keyboard.press(key)
    sleep(0.5)
    keyboard.release(key)


run()

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

CodePudding user response:

This code seems to have couple of problems from initial check. Since you are calling run() before the if keyboard.is_pressed('x'):, it will always run in infinite loop.

Also, if line[0] == '1':, this needs to be changed to if line[-1] == '1': to check the last character entered, but since the code never reached the line to take x as input, entering a value x will not work either.

There are logical errors here.

CodePudding user response:

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

python main.py

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

# run()

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

import output                                                                                           
output.run() 

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.

  • Related