19

My Python IDE (pycharm) has stopped auto completing my modules (suggestions). I get unresolved references after every django module I try to import so:

from django - works, however soon as I add a 'dot' it fails so from django.db import models gives me unresolved errors...

The ackward thing is after compiling references DO work.

I discovered that all my __init__.py files (everywhere) no longer are marked with python icon and are now notepad icons. Also opening init files in my interpreter gives non-color marked up text (no syntax highlighting). So I think Python doens't recognizes these files.

My python interpreter is python 2.6.1 with Django 1.2.4 and my django is installed under: /Lib/python/2.6/site-packages (full directories, not egg)

When I unfold sitepackages from external libraries within the IDE I do see colored mark up for all .py files EXCEPT __init__.py files. Hence thats where the issue lives.

(I have found posts on google for similar problems but no answers...)

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
deven677
  • 191
  • 1
  • 1
  • 3

7 Answers7

21

I had exactly the same issue and couldn't find a definitive answer. Just invalidating caches didn't work for me. The problem lies in the fact that, at some point, __init__.py files got registered as text files and messed up the indexing. I worked out this fix:

  • Preferences > File Types > Text Files.
  • Remove __init__.py from the list of registered patterns. Apply.
  • Wait for your indexes to re-build.
  • (If it's still not working) File > Invalidate caches & restart.
stevenholmes
  • 213
  • 2
  • 8
  • 3
    great detective work. worked for me. I have no idea __init__.py got into the list of text files. I would have had no reason to do it. – Michael Bylstra Sep 13 '13 at 02:52
  • 3
    worked for me, thanks. If I could I would give you +1000 for that answer. I've spent a few hours fighting with this :) Thanks again! – KennyPowers Apr 16 '14 at 11:30
  • This happened to me after I accidentally created a new file with name `__init__` and no extension (i.e. when I selected New -> File, instead of New -> Python File and then just typed the name without extension). That somehow resulted in IDE registering `__init__.py` as a text file.. – nirvana-msu Jul 17 '20 at 21:51
  • 1
    `Preferences > Editor > File Types > Text` – Hzzkygcs Oct 15 '21 at 14:55
13

GOTO File > Settings > Langauges & Frameworks > Python Template languages

Select Django in the Template language drop-down.

Jetbrains Docs > Python Template Languages

Matthew Weber
  • 889
  • 9
  • 22
  • This solution works for me. FYI, on my PyCharm it shows only `Template Languages` instead of `Python Template Languages`. Probably because of version difference. Just so that maybe it help, the default template engine set on my case was Jinja instead of django, no wonder autocomplete for built-in tags (such as `comment` `endcomment` for example) didn't work. Thanks so much! – Rahmat Nazali Salimi Dec 22 '20 at 10:54
12

Nothing here worked for me but Enabling Django support in the settings did:

  1. Settings > Languages & Frameworks > Django
  2. Check of "Enable Django Support"
  3. Fill in the information for your project.
Justin Harris
  • 1,969
  • 2
  • 23
  • 33
2

Trivial solution that worked for me: start a new django project using pycharm project options. Try auto-completing using a django import module. If it works, switch back to your original project and auto-complete should be working fine.

I still don't understand why this works.

sista_melody
  • 49
  • 1
  • 5
2

I worked out this fix:

  1. Go to Preferences > Project:{YourProject} > Python interpreter.
  2. I saw that the field for "Project interpreter" said "2.7...", but I was coding with python 3.4 and my project was created with python 3.4. Replace the version of python in the "Project interpreter" field with the Python version by means of which you have created your Django project. Apply changes and restart PyCharm.
Aurora0001
  • 13,139
  • 5
  • 50
  • 53
Sergey
  • 959
  • 8
  • 10
  • Thanks. This worked for me. I did not have a different interpreter. But upon selecting my version again i noticed Django appear in the list. After invalidating the cache autocomplete then started working. – Craig Foster Apr 28 '18 at 17:44
0

you should just change your project interpreter if it is using anaconda or etc to standard python interpreter which may be located in this path (C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe)

-1
File--> setting--> project:your_project_name --> python interperter

change python interpreter

Shah Vipul
  • 625
  • 7
  • 11