在Pycharm编辑器中调用getpass模块

本文探讨了在PyCharm中使用Python的getpass模块遇到的问题及解决方法。作者发现PyCharm不支持getpass模块,而在命令行和其他环境中可以正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 兴趣来潮想复习一下python中的getpass模块编辑器使用Pycharm很简单的代码写完执行不到输入部分代码如下

1
2
3
4
5
6
7
import  getpass
print ( """
  --------------------**************-----------------
""" )
#ss=getpass.getpass('please The password:')
=  getpass.getpass( 'input your password:' )
print (s)

执行结果

1
2
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C: / Users / Administrator / PycharmProjects / s14 / helloword.py
  - - - - - - - - - - - - - - - - - - - - * * * * * * * * * * * * * * - - - - - - - - - - - - - - - - -

  但是事与愿违并没有出现理想的状况 input your password:   ,由于初学python所以始终自己哪里敲错了检查了几遍没有错误同时执行的时候没有任何的报错就想着是不是编辑器的原因

于是在Windows的命令行执行以上代码出现了预期的结果

1
2
3
4
5
6
7
8
9
10
11
C:\Users\Administrator>python
Python  3.5 . 2  (v3. 5.2 : 4def2a2901a5 , Jun  25  2016 22 : 18 : 55 ) [MSC v. 1900  64  bit (AMD64)] on win32
Type  "help" "copyright" "credits"  or  "license"  for  more information.
>>>  import  getpass
>>> p = getpass.getpass( 'input your password:' )
input  your password:
>>>  print (p)
88
>>> p = getpass.getpass( 'input your password:' )
input  your password:
>>>  print (p)

  在其他工具下执行也出现了预期的结果于是就比较纳闷了开始百度和各种找资料最后发现Pycharm竟然不支持getpass模块。

wKiom1jL4YOz7K8xAAAby65Oyx0348.png-wh_50



本文转自 tianya1993 51CTO博客,原文链接:http://blog.51cto.com/dreamlinux/1907763,如需转载请自行联系原作者

import subprocess import re class VMwareShellParser: def __init__(self, host, user, password): self.host = host self.user = user self.password = password self.vmrun_path = "/usr/bin/vmrun" # 需根据实际路径修改 def execute_vmware_cmd(self, command): """执行ESXi主机命令""" try: ssh_cmd = f"sshpass -p {self.password} ssh {self.user}@{self.host} '{command}'" result = subprocess.run(ssh_cmd, shell=True, capture_output=True, text=True, check=True) return result.stdout.strip() except subprocess.CalledProcessError as e: return f"Error: {e.stderr}" def parse_vm_list(self): """解析虚拟机列表,实现基础解析器功能""" output = self.execute_vmware_cmd("vim-cmd vmsvc/getallvms") vm_data = [] # 使用正则解析输出(示例:1 ubuntu [datastore1] ubuntu.vmx linux) pattern = r'^(\d+)\s+(\S+)\s+\[(\S+)\]\s+(\S+\.vmx)\s+(\S+)' for line in output.split('\n')[1:]: # 跳过标题行 match = re.match(pattern, line) if match: vm_data.append({ "vmid": match.group(1), "name": match.group(2), "datastore": match.group(3), "config": match.group(4), "os_type": match.group(5) }) return vm_data # 使用示例 if __name__ == "__main__": parser = VMwareShellParser( host="esxi.example.com", user="root", password="your_password" # 建议使用密钥认证替代 ) vms = parser.parse_vm_list() for vm in vms: print(f"ID: {vm['vmid']}, Name: {vm['name']}") Traceback (most recent call last): File "D:\vmware_parser.py\main.py", line 48, in <module> vms = parser.parse_vm_list() File "D:\vmware_parser.py\main.py", line 23, in parse_vm_list output = self.execute_vmware_cmd("vim-cmd vmsvc/getallvms") File "D:\vmware_parser.py\main.py", line 16, in execute_vmware_cmd result = subprocess.run(ssh_cmd, shell=True, capture_output=True, text=True, check=True) File "D:\Program Files\Python36\lib\subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process: TypeError: __init__() got an unexpected keyword argument 'capture_output'
最新发布
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值