'How to run python file with admin rights in pycharm

I'm running this code for trying to ping a server on 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.". I've tried running pycharm as admin but it doesn't have any affect. How can I give admin privileges to the file?



Solution 1:[1]

Startup pycharm as Administrator then all scripts will be ran as administrator.

From Windows start menu right click the Pycharm shortcut and select 'Run as administrator.' Optionally create a short and change the options to always Run as Administrator under the "Compatibility" tab.

Solution 2:[2]

Simply run the PyCharm shortcut as administrator, and then PyCharm terminal will run with admin privileges.

See these images for referral,

For Desktop PyCharm Shortcut

For Start menu search of PyCharm Shortcut

Solution 3:[3]

To run something as admin in Windows, you need to execute your application/scrpit from elevated CMD.

So running your python script in CMD with Adiminstrator privileges should do the work.

Steps:

  1. Open CMD as Administrator
  2. run python full\path\to\your\script.py

Now regarding to Windows ping: -c flag is exist only in Linux version of ping. Use -n parameter instead for count

Solution 4:[4]

Change SMTP port to 587 in smtplib.py for gmail use.

SMTP_PORT = 587 SMTP_SSL_PORT = 465

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron Pedersen
Solution 2
Solution 3
Solution 4 Chavez