subprocess.run AttributeError: 'module' object has no attribute 'run'
时间: 2023-11-25 16:52:15 浏览: 47
这个错误通常是由于Python版本过低导致的,因为`subprocess.run`是在Python 3.5及以上版本中引入的。如果你的Python版本低于3.5,那么你需要使用`subprocess.call`或`subprocess.Popen`来代替`subprocess.run`。如果你的Python版本是3.5及以上,那么你需要检查一下你的代码是否正确导入了`subprocess`模块。如果还是无法解决问题,可以尝试升级Python版本或者检查一下你的代码是否存在其他问题。
以下是两个例子:
1. 使用`subprocess.call`代替`subprocess.run`:
```python
import subprocess
result = subprocess.call(['ls', '-l'])
print(result)
```
2. 检查代码是否正确导入了`subprocess`模块:
```python
import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True)
print(result.stdout)
```
相关问题
AttributeError: 'module' object has no attribute 'run'
抱歉,我之前的回答中的代码在 Python 2 中使用了 `subprocess.run()` 方法,而该方法在 Python 2 中是不存在的。如果你使用的是 Python 2,你可以尝试使用 `subprocess.call()` 方法替代。修改后的代码如下所示:
```python
# encoding: utf-8
import time
import os
import random
import subprocess
class root_password:
Ap = 'QAZWSXEDCRFVTGBYHNUJMIKOLP'
Bp = 'qazwsxedcrfvtgbyhnujmikolp'
Cp = '!@#$%^&*'
Dp = '0123456789'
def __init__(self):
self.characters = 'QAZWSXEDCRFVTGBYHNUJMIKLOP'
self.Dpass = random.choice(self.characters)
print(self.Dpass)
self.characters = '!@$%^&*'
self.fpass = ''.join(random.choice(self.characters))
print(self.fpass)
self.characters1 = self.Bp + self.Ap
self.passfa1 = ''.join(random.choice(self.characters1) for _ in range(2))
print(self.passfa1)
self.characters2 = self.Bp + self.Dp
self.passfa2 = ''.join(random.choice(self.characters2) for _ in range(2))
print(self.passfa2)
self.characters3 = self.Bp + self.Cp
self.passfa3 = ''.join(random.choice(self.characters3) for _ in range(3))
print(self.passfa3)
self.characters4 = self.Ap + self.Dp
self.passfa4 = ''.join(random.choice(self.characters4) for _ in range(2))
print(self.passfa4)
self.characters5 = self.Ap + self.Cp
self.passfa5 = ''.join(random.choice(self.characters5) for _ in range(2))
print(self.passfa5)
self.characters6 = self.Dp + self.Cp
self.passfa6 = ''.join(random.choice(self.characters6) for _ in range(3))
print(self.passfa6)
self.NEWPASS = self.Dpass + self.passfa3 + self.passfa6 + self.fpass + self.passfa1 + self.passfa4 + self.passfa5
print(self.NEWPASS)
command = 'echo ' + self.NEWPASS + ' | sudo passwd --stdin root'
subprocess.call(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
command1 = 'echo ' + time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())) + '--root >> /root/pass.log'
command2 = 'echo ' + self.NEWPASS + ' >> /root/pass.log'
subprocess.call(command1, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.call(command2, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
```
请确保你的 Python 版本是 2.x,并尝试运行代码,看看是否解决了问题。如果你使用的是 Python 3.x,请忽略这个解决方案,并确保在运行代码时使用正确的 Python 版本。
AttributeError: 'module' object has no attribute 'DEVNULL'
`AttributeError: 'module' object has no attribute 'DEVNULL'` 错误表明模块中没有名为 `DEVNULL` 的属性。
根据你提供的代码,这个错误可能是因为你正在使用的 Python 版本较低,而 `subprocess.DEVNULL` 是在 Python 3.3 及以上版本中引入的。
如果你使用的是 Python 2.x 版本,你可以尝试使用 `subprocess.PIPE` 作为替代。修改代码如下所示:
```python
command = 'echo ' + self.NEWPASS + ' | sudo passwd --stdin root'
subprocess.call(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
command1 = 'echo ' + time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())) + '--root >> /root/pass.log'
command2 = 'echo ' + self.NEWPASS + ' >> /root/pass.log'
subprocess.call(command1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.call(command2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
```
这样修改后的代码应该能够在 Python 2.x 版本中正常运行。请确保你使用的是正确的 Python 版本,并尝试运行代码,看看是否解决了问题。