python subprocess.run() returns AttributeError: 'module' object has no attribute 'run'

4.8k Views Asked by At

I'm trying to execute a shell command from a python script. I'm using python 3.7.3, so the run() function should work properly, but it always throws the AttributeError: 'module' object has no attribute 'run' Error. I set the default version of python to 3.7.3. python --version also returns Python 3.7.3. In order to understand the problem I wrote a simple script called out.py:

#!/usr/bin/env python
import subprocess
subprocess.run(["ls", "-l"])

If I execute this script from the shell with sudo python out.py it returns the above mentioned error. If I execute it with sudo python3 out.py it works just fine. Am I missing something here? I`ve never used python2 in the first place. Note: I'm doing this on a Raspberry Pi. The script where I want to execute the shell command from is invoked as part of a trigger routine, so I don't know if I can change something there (Gammu RunOnReceive directive)

1

There are 1 best solutions below

0
On

Fixed my issue by setting the python version of the root user to python3