44

Our projects are usually structured:

/project-name
  /src
    /django-project-name
      etc..
readme.md
requirements.txt

If I open /project-name instead of /django-project-name PyCharm underlines my imports saying they can't find and it tries to reference imports as src.django-project-name.app_name.models.Thing which can't actually be found when you run Django.

How can I get it to work the same as when I mount /djang-project-name where it gets these things right?

Community
  • 1
  • 1
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179

3 Answers3

100

I fixed it by going to File -> Preferences -> Project Structure selecting the /django-project-name in the tree and clicking sources to add it.

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
  • 8
    You'll also need to enable Django support (settings -> Languages & Frameworks -> Django -> Enable Django Support). – thebjorn Dec 28 '14 at 20:45
  • 4
    The PyCharm documentation clearly states that marking a directory as a "sources" directory will result in that directory being added to PYTHONPATH- but this clearly and repeatedly does not work for me (source: https://www.jetbrains.com/pycharm/help/project-structure-dialog.html) – Brandon Kuczenski Nov 19 '15 at 23:41
1

I had an issue with PyCharm not finding the templates folder for an existing Django project. So I created a test project and saw that the template folder setting is only added when setting up the project (it should be in project-root/.idea/project-name.iml)

Copying the same setting and changing the folder to the correct one fixed the issue for me.

Community
  • 1
  • 1
victor n.
  • 658
  • 1
  • 7
  • 14
0

Python requires an __init__.py file in each directory that is a module or part of the project, in pycharm you can add extra paths via preferences

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • The _ _ init _ _.py file is no longer mandatory since Python 3.3. Moreover it indicates the interpreter to create a single directory package which shadows sys.path and may lead to import errors if you're not aware of it. See PEP420: https://www.python.org/dev/peps/pep-0420/#specification – robsn Jul 23 '15 at 12:44