2

When I use Popen to start a subprocess, if shell=True isn't set I get the following error message:

Traceback (most recent call last):
  File "C:\Users\Student01\PycharmProjects\yolov5-master\detect.py", line 37, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

I don't get this error message if shell=True.

I don't want to need to set shell=True because it causes problems when I then try to call communicate or stdout on the subprocess (It just runs the subprocess into the shell without executing anything bellow the communicate call).

Here is the code (In case it can help):

#!/usr/bin/env python
import subprocess

detectPath = "C:\\Users\\Student01\\PycharmProjects\\yolov5-master\\detect.py"

print("First Print Passed")

process = subprocess.Popen("python {} --source 0".format(detectPath), stdout=subprocess.PIPE, shell=False)

# output = str(process.communicate())

while process.poll() is None:
    process.stdout.readline()
    print("Poll is None")

I am using Pycharm

I tried adding the yolov5 project files into the included files of my pycharm project (since I'm working in a venv) but still get the error.

The only solution to the moduleNotFound error seems to be to set shell=True in the Popen but that creates other problems.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 2
    Presumably, PyTorch is installed in a virtual environment managed by PyCharm, not globally. If you know the path to the VM, you can run that python explicitly with `path/to/vm/bin/python`, or run your Python script in the appropriate virtual environment in the first place. – chepner Jan 12 '23 at 22:49
  • 1
    Welcome to Stack Overflow! Please do not add answers to the question body itself. Instead, you should add it as an answer. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan Jan 17 '23 at 13:46

0 Answers0