How to call the .exe executable file in django views.py?

background:
course design involves calling several compiled .exe executables at the back end. Calling hello.exe directly in test.py is tested to be fine, but calling hello.exe in django views.py fails:
"hello.exe" is not an internal or external command, nor is it a runnable program or batch file.

related environment:
python 3.5,2.1django, IDE: PyCharm.

related code:
test.py

import os  

def ccpp():
    main="hello.exe"
    f = os.popen(main)    
    data = f.readlines()    
    f.close()
    return data

if __name__=="__main__":
    print(ccpp())
    

hello.cpp

-sharpinclude<iostream>

void test()
{
    std::cout<<"hello"<<std::endl;
}

int main()
{
    test();
    return 0;
}

views.py

def test(ruquest):
    ...
    main="hello.exe"
    f = os.popen(main)    
    data = f.readlines()    
    f.close() 
    ... 

test.py views.py hello.cpp hello.exe are all in the same level directory.

the result of calling directly in test.py is as follows

views.py test()

Jul.06,2022

there is a BASE_DIR variable in settings.py that represents the directory (absolute path) of the current Django project, so I usually do this:

  1. create a directory under the Django project directory, such as program, and then place hello.exe under the program directory
  2. add from django.conf import settings to the beginning of views.py
  3. in the view function, call the system shell to execute the external program through os.system (os.path.join (settings.BASE_DIR, 'program',' hello.exe')) .

path problem.
os.popen is using the current path. The current path of the Python program generally refers to the path of the Python file at the beginning of execution, which can be seen with os.getcwd () .
such as Django's official example structure :

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

if you run through python manage.py runserver , then the current directory is mysite/ , so place hello.exe in the same directory as manage.py .

or if you insist on putting it in the same directory as views.py , you can do this:

  find-current-directory-and-files-directory  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ed520e-3528.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ed520e-3528.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?