Cannot import ctypes for checking admin privileges on Windows platform

214 views Asked by At

Hello Programmers of Stack overflow,

I am trying to import I library for my code, but whenever I execute my code on PyCharm, i get an error:

File "C:\Users\USER\projectextinguish\venv\lib\site-packages\command_runner\elevate.py", line 49, in <module>
raise ImportError(
ImportError: Cannot import ctypes for checking admin privileges on Windows plaform

Does anyone know how i can import ctypes?

I have no idea what to do.

code:

import os   #self explainitory
from command_runner.elevate import elevate


def admin():
  elevate(mainfirewall)



   admin()   # executes admin function

   def mainfirewall ():
os.system("netsh advfirewall set allprofiles state off")              # disables firewall

mainfirewall()         #executes firewall function
1

There are 1 answers

1
Serge Ballesta On

I could reproduce the last part of your error message by installing command_runner and not installing pywin32. I got:

Traceback (most recent call last):
  File "C:\Users\...\ess\venv\lib\site-packages\command_runner\elevate.py", line 43, in <module>
    import win32event  # monitor process
ModuleNotFoundError: No module named 'win32event'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\...\ess\venv\lib\site-packages\command_runner\elevate.py", line 49, in <module>
    raise ImportError(
ImportError: Cannot import ctypes for checking admin privileges on Windows platform.

The first part of the stacktrace gives the cause: the pywin32 package is required to have the win32event module.

How to fix: install the missing package with pip install pywin32