Error running Django in Intellij / Pycharm

Question:

i have problem running Django server in Intellij / Pycharm (I tried in both).

There is that red cross:

enter image description here

And this is the error i get:

Error running Django: Please select Django module

I have Python 2.7.10 and Django (via pip) installed on my computer.
I’ve tried reinstalling both python and Django, but it didn’t help.
I’ve specified project sdk (Python).

Edit:

This is what it looks like in “Project Interpreter” page.

enter image description here

and Django configuration:

enter image description here

Asked By: Broccoli

||

Answers:

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.

Answered By: Curtis Olson

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

Answered By: jbm

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.

Answered By: Bipin Maharjan

This issue can also manifest in a situation where you have multiple modules and the django module isn’t the first one. It looks like intellij just picks the first one and the option for choosing the module is missing from the UI..

You can find .idea/workspace.xml and find the run configuration, there’s a <module name="something" /> that you can edit to match the correct module name. Intellij musn’t be running when the value is edited.

Edit: It appears also to be possible to get past this by selecting "Use SDK of module" and selecting the django module.

Answered By: voneiden