简体   繁体   English

如何在pycharm中以管理员权限运行python文件

[英]How to run python file with admin rights in pycharm

I'm running this code for trying to ping a server on pycharm:我正在运行此代码以尝试 ping pycharm 上的服务器:

import subprocess as sp

ip = "216.52.241.254"
status,result = sp.getstatusoutput("ping -c1 -w2 " + ip)

if status == 0:
    print("System " + ip + " is UP !")
else:
    print("System " + ip + " is DOWN !")

print(result)

However the print(result) line always prints "Access denied. Option -c1 requires administrative privileges."然而, print(result)行总是打印"Access denied. Option -c1 requires administrative privileges." . . I've tried running pycharm as admin but it doesn't have any affect.我试过以管理员身份运行 pycharm 但它没有任何影响。 How can I give admin privileges to the file?如何授予文件管理员权限?

Startup pycharm as Administrator then all scripts will be ran as administrator.以管理员身份启动 pycharm,然后所有脚本都将以管理员身份运行。

From Windows start menu right click the Pycharm shortcut and select 'Run as administrator.'从 Windows 开始菜单右键单击 Pycharm 快捷方式并选择“以管理员身份运行”。 Optionally create a short and change the options to always Run as Administrator under the "Compatibility" tab. (可选)在“兼容性”选项卡下创建一个简短的选项并将选项更改为始终以管理员身份运行。

To run something as admin in Windows, you need to execute your application/scrpit from elevated CMD.要在 Windows 中以admin身份运行某些内容,您需要从提升的 CMD 执行您的应用程序/scrpit。

So running your python script in CMD with Adiminstrator privileges should do the work.因此,使用Adiminstrator权限在CMD运行您的 python 脚本应该可以完成工作。

Steps:脚步:

  1. Open CMD as Administrator以管理员身份打开CMD
  2. run python full\\path\\to\\your\\script.py运行python full\\path\\to\\your\\script.py

Now regarding to Windows ping : -c flag is exist only in Linux version of ping.现在关于 Windows ping-c标志仅存在于 Linux 版本的 ping 中。 Use -n parameter instead for count使用-n参数代替计数

Simply run the PyCharm shortcut as administrator, and then PyCharm terminal will run with admin privileges.只需以管理员身份运行 PyCharm 快捷方式,然后 PyCharm 终端就会以管理员权限运行。

See these images for referral,请参阅这些图像以供参考,

For Desktop PyCharm Shortcut对于桌面 PyCharm 快捷方式

For Start menu search of PyCharm Shortcut对于 PyCharm Shortcut 的开始菜单搜索

Change SMTP port to 587 in smtplib.py for gmail use.将smtplib.py中的SMTP端口改为587,供gmail使用。

SMTP_PORT = 587 SMTP_SSL_PORT = 465 SMTP_PORT = 587 SMTP_SSL_PORT = 465

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM