-1

I am trying to run a file in the back ground to keep the computer from going to sleep

Main file:

from sub process import Popen
import sys
script_path = 'C:\\Users\\PycharmProjects\\Flash_Cycle'
sys.path.append(script_path)

def test_Popen():
     stay_awake = Popen(['python', script_path + '\\stay_awake.py'])
     print(stay_awake)

Background file:

import pyautogui
import time
print('sub process started')
while True:
    pyautogui.press('numlock')
    time.sleep(0.1)
    pyautogui.press('numlock')
    time.sleep(60)

The error I am getting is

ModuleNotFoundError: No module named 'pyautogui'

Redox
  • 1,240
  • 1
  • 4
  • 13
rpuh
  • 26
  • 2
  • 1
    Do you have `pyautogui` installed? – blackbrandt May 24 '22 at 13:46
  • yes the stay_awake.py runs perfectly fine if I run it by itself but when trying to call it with the subprocess Popen is when I get the error – rpuh May 24 '22 at 13:49
  • 1
    Probably because the command you pass to `Popen` (`python`) is a Python 2 interpreter. Try `stay_awake = Popen(['python3', ...` – Tomerikoo May 24 '22 at 13:50
  • still didnt seem to help – rpuh May 24 '22 at 13:51
  • 1
    Does this answer your question? [Running a py file through subprocess gives different output than running the file directly](https://stackoverflow.com/q/70555383/6045800) – Tomerikoo May 24 '22 at 13:54
  • TL;DR Use `sys.executable` instead of `"python"`. But why are you using `subprocess` to run another Python scipt? Why not just `import`? – Tomerikoo May 24 '22 at 13:56
  • A few problems with your question: you mispelled `sub( )process` and didn't actually call the function. Please make sure your question has a [mre] – Tomerikoo May 24 '22 at 13:57
  • I am using pycharm so I am just running the function as a test. Also I want to run the second file as a process in the back ground. The problem is not the output but the problem is that the imports for the back ground process are not being found – rpuh May 24 '22 at 14:00
  • You are getting hung up on the title. The symptom is the same. A different Python version is being used and so an import is missing. Did you try with `sys.executable`? (make sure to `import sys`) – Tomerikoo May 24 '22 at 14:03
  • it is telling me str object is not callable – rpuh May 24 '22 at 14:07
  • stay_awake = sys.executable(script_path + '\\stay_awake.py') – rpuh May 24 '22 at 14:08
  • You didn't understand me: `stay_awake = Popen([sys.executable, script_path + '\\stay_awake.py'])` – Tomerikoo May 24 '22 at 14:09
  • no that still gave me a return of none and didnt print – rpuh May 24 '22 at 14:13
  • What do you mean? What gave you a return of `None`? Please clarify the question and provide a [mre]. Did it remove the `ModuleNotFoundError`? – Tomerikoo May 24 '22 at 14:35
  • I found out that it was the path to my python executable. I needed to change the path so that it was able to find my python exe – rpuh May 24 '22 at 14:50

0 Answers0