How to Open Python Interactive Shell in Django?

How to Open Python Interactive Shell in Django?

How to Open Python Interactive Shell in Django?

As we saw in the previous tutorial where I have shared simple commands to start with your first Django project. In this tutorial, I will be sharing command to open Python shell in Django.

Python to Learn Django

Many of the developers even don’t know if there is an interactive shell we can open in Django project. What they do is, make the code changes, start the server and check the output in the browser.

If this is working fine…

Why do You Need Python Shell in Django?

Sometimes you need to test some sort of code before putting into the Django project.

If you have Python Shell open for your Django project, you can simply run your commands and test the code.

You can test the code or feature provided by the Django which you can not test in our usual Python interpreter shell.

Now,

How to Open Python shell in Django?

In Django project directory, you can see manage.py file. It manages all the important features for Django. You can use it for our purpose now.

Follow the steps.

  • Open terminal with the current directory as Django project. You can see the manage.py in your current directory.
  • Run the following command:
    python manage.py shell

This interactive shell is similar to the interactive shell that we have for Python, though it looks kinda different.

In this shell, you can run all the basic code as you run on our python basic interactive shell. Moreover, you can import all the libraries which you have installed for Django project and test the code.

How to Exit the Django shell?

As usual, like our Python shell, follow the steps.

  • Press ctrl+D
  • You will ask to confirm. Just press Y and enter.

You will come out of the python Django shell.

Now you can open Python shell in Django, run your commands and exit shell. If you have any question to ask related to this topic, write in the comment.

Happy Pythoning!

2 Comments

  1. I am trying this command, but it is not working. I am in the current directory, but it still shows an error as ‘No such file or directory.’
    Hoping for a reply.
    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *