Error running Django in Intellij / Pycharm

If your IntelliJ is up to date, there is another solution.

I had the exact same problem in IntelliJ 2017.2 and it was driving me crazy until I read this post from a IntelliJ maintainer.

If you use IntelliJ Idea and "Load an existing project", it will model it as a Java project with a Python modules attached. You cannot get Django loaded, no matter what you do.

I handled this by purging the .idea directory, and created a new Django project, with the pre-existing Django directory as the base directory in IntelliJ. I can now see Django in the project structure > project settings > module part of Intellij, and I can select the django settings file.

Step by step in pictures

  1. Delete .idea folder

  2. Create new project Create new project

  3. Select Python > Django Select Python > Django

  4. Hit next Hit next

  5. Select existing django project path (or start from scratch with a new folder) Enter existing project path

  6. Add DJANGO_SETTINGS_MODULE=yourprojectname.settings to your run configuration (can be found in yourprojectname/wsgi.py file).

Edit run configuration

Add env variable

Enjoy your Django development 🚀


Problem Analysis in IntelliJ

The problem is whenever you import a python project in IntelliJ. It will load as java project and adjust itself into python language without changing the project type to python. So, IntelliJ thinks you are in java project even you are running python code in it and that's the reason you are not allowed to switch to Django or any other python framework.

Here is the fix for that:

find the .iml the file inside of .idea folder or just inside of your project folder and change module type from JAVA_MODULE to PYTHON_MODULE inside of .iml file Close the project and re-open your project and it will prompt you to configure your project as a Django project or whatever python framework you are using.
You also need to configure the framework inside of project_structure>module>(Your Framework)

For PyCharm user make sure you have added the framework support to your imported project.


Try adding DJANGO_SETTINGS_MODULE=untitled.settings to the environment variables listed in the configuration menu by clicking the dropdown titled 'Django' in your first photo.