# how to run pywinauto in django views.py ?

**URL:** <https://forum.djangoproject.com/t/how-to-run-pywinauto-in-django-views-py/7327>\
**Category:** Using Django\
**Created:** [April 1, 2021, 5:31pm UTC](https://forum.djangoproject.com/t/how-to-run-pywinauto-in-django-views-py/7327 "2021-04-01T17:31:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomcypress](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomcypress/32/4085_2.png) [@tomcypress](https://forum.djangoproject.com/u/tomcypress)\
**Post date:** [April 1, 2021, 5:31pm UTC](https://forum.djangoproject.com/t/how-to-run-pywinauto-in-django-views-py/7327/1 "2021-04-01T17:31:39Z")

</div>

from django.shortcuts import render  
from django.http import HttpResponse  
from pywinauto.application import Application  
from pywinauto.keyboard import send\_keys  
from pywinauto import mouse  
import psutil  
import os

def get\_pid(name): # define function get process\_pid  
pids = psutil.process\_iter()  
for pid in pids:  
if(pid.name() == name):  
return pid.pid

def autoAllocate(request):  
process=get\_pid(“HsCMP.exe”) #it works  
app = Application(backend=“uia”).connect(process=get\_pid(“HsCMP.exe”)) # it no works why?  
…  
…  
return HttpResponse(“Hello, world. You’re at the polls index.”+str(process))

HttpResponse Server Error (500)

I run the python script alone and it works, I only wrote one line of code"app = Application(backend=“uia”).connect(process=get\_pid(“HsCMP.exe”))",and it no works why? Can somebody help me?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 2, 2021, 1:31am UTC](https://forum.djangoproject.com/t/how-to-run-pywinauto-in-django-views-py/7327/2 "2021-04-02T01:31:52Z")

</div>

I don’t have an answer for you, but I do have a couple things that might help you diagnose the problem.

First, how are you running Django? Are you running this from the command line using runserver or runserver\_plus? If so, check the console log where that’s running - that should have a more detailed error message.

You could also turn debug on in your settings to get a more detailed response - that may provide more information beyond just the 500.

Also, you could try running that from within the Django shell (or shell\_plus) and see what error messages you may receive.

You might also want to check the Windows event log to see if something was logged in there.

I think you’re going to need to find more information about what’s actually happening before you can identify a solution.

---

<div class="post-metadata">

**Author:** ![tomcypress](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tomcypress/32/4085_2.png) [@tomcypress](https://forum.djangoproject.com/u/tomcypress)\
**Post date:** [April 3, 2021, 2:32am UTC](https://forum.djangoproject.com/t/how-to-run-pywinauto-in-django-views-py/7327/3 "2021-04-03T02:32:04Z")

</div>

Thank you very much !!!  
According to your reminder, I found a solution.  
Thank you very much indeed !!!
