16

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

10
  • 1
    In your settings, have you specified 1) Project Interpreter 2) Django configuration (Django Project Root, Settings File, Manage.py file)? Commented Apr 15, 2016 at 19:20
  • 1) Yes, i will add screenshot of the "project interpreter" view. 2) All the files were created along with the project, so I hope everything is configurated well there Commented Apr 15, 2016 at 19:41
  • In PyCharm's preferences, navigate to Languages & Frameworks > Django. There, you'll want to make sure that you've configured the Django Project Root to be your project's root, the Settings File to be the settings.py file which exists in your main project folder (this would have been automatically created for you), and the Manage Script should be set to the manage.py file which is in your project root. Commented Apr 15, 2016 at 19:45
  • Hmm, i did exactly what you said, but unfortunatelly nothing changed. Commented Apr 15, 2016 at 19:58
  • 1
    That sounds correct so far. I'm unfamiliar with windows, so this may not work, but try adding DJANGO_SETTINGS_MODULE=untitled.settings to it. You'll want to separate them by a semicolon I believe... Commented Apr 15, 2016 at 20:41

4 Answers 4

31

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

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

3 Comments

Worth noting is that you can just go to "File" > "Project Structure" > "Modules" > Delete the existing module > Click plus sign > "New Module" > Follow steps above
Worked for IntelliJ IDEA 2020.3 too.
When above suggestions failed, I simply edited the *.iml file to contain <component name="FacetManager"><facet type="django" name="Django">.
6

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.

1 Comment

For some reason that doesn't seem to work. What works is removing Django server from run configuration and adding new one. I compared the XML and they are exactly same, but the newly created one works.
6

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 eventhough 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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.