0

I have chatbot web application in Django framework. So far everything is working, but now, I want to run the chatbot python script using ajax and calling the view for it from the Javascript file. I have an API using REST and the view for the python script and the ajax to call that view.

view.py:

from chat.chatbot1 import main_chatbot

def run_python_script(request):
    os.system('python3 main_chatbot.py')
    return HttpResponse("OK")

index.js:

 function run_chatbot_script(){
     $.ajax({
        type: 'GET',
        url: 'http://127.0.0.1:8000/chatbot/run_python_script/',});

and the python script folder is located in the chat app inside the chatbot django project.

The problem is that the view can't find the file and this error appears:

python3: can't open file 'main_chatbot.py': [Errno 2] No such file or directory
2
  • use full path to script - server may work in different folder than your script. Commented Dec 6, 2016 at 21:02
  • @furas the main_chatbot is inside a folder called chatbot1 that is inside the chat app. wrote the full path and it still the same error the view can't seen the chatbot1 folder Commented Dec 6, 2016 at 23:20

3 Answers 3

2

If is in the same folder you should import it:

Example if is the same folder as the view.py

import .run_python_script 

Then just call the functions you want... You can also put the full path in the os.system but it doesnt seem alright...

Sign up to request clarification or add additional context in comments.

1 Comment

the main_chatbot is inside a folder called chatbot1 that is inside the chat app. wrote the full path and it still the same error the view can't seen the chatbot1 folder
0

Your Django application will probably have the working directory as the location your manage.py file, so it will expect the python script in the same directory.

Use the full path to the script, or the path relative to the directory where mange.py is.

so maybe something like :

os.system('python3 chat/main_chatbot.py')

or

os.system('python3 /home/user/django_project/chat/main_chatbot.py')

(The answer suggesting importing the script is probably a better way of doing it unless there is a specific need to run it as a separate process).

2 Comments

the main_chatbot is inside a folder called chatbot1 that is inside the chat app. wrote the full path and it still the same error the view can't seen the chatbot1 folder
Are the permissions set on that folder so that Django can read it?
0

You ought to give the full path of main_chatbot.py.

The best way to do that, could be using pkg_resources.resource_filename, like this:

import pkg_resources
script_path = pkg_resources.resource_filename('chat', 'main_chatbot.py")

Where chat is the name of the package which contains your script.

To run the script, it could be a good idea to use the same Python executable as your Project's executable (your virtualenv).

Do do that, you can use sys.executable to get the Python path used by your virtualenv:

import sys
python_path = sys.executable

It is a best practice to replace os.system by subprocess.check_call, like this:

import subprocess
subprocess.check_call([python_path, script_path]

See Replacing Older Functions with the subprocess Module

5 Comments

the main_chatbot is inside a folder called chatbot1 that is inside the chat app folder. wrote the full path and it still the same error the view can't seen the chatbot1 folder
the ERROR was subprocess.CalledProcessError: Command '['/Users/emansaad/Desktop/chatbot/bin/python3', '/Users/emansaad/Desktop/chatbot/src/chat/main_chatbot.py']' returned non-zero exit status 1
i tried it again and change the 'chat' to 'chatbot1' the there is ImportError: No module named 'chatbot1'
Why don't you import your script and run it's main() function? Can you post your script (at least the import statements).
in main_chatbot.py there is no main function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.