Handle custom Django settings module

Instead of using a standard settings.py, I use a settings folder with an __init__.py, which imports settings conditionally depending on my deployment environment.  This seems to have rendered PyCharm unable to detect many of my settings.  Does PyCharm attempt to actually import settings.py or to parse it?  It seems to me that doing the former would allow it to handle most nonstandard settings setups, as long as things don't get too crazy.  But besides some conditional importing, my settings are all just regular variables at the end of the day, and my settings don't depend on any non-standard library modules, so it seems like it should be workable.
4 comments
No, PyCharm does not attempt to import the settings module, and we don't plan to do so for the moment. But we do plan to make our parsing more flexible to handle different cases occurring in users' projects.
0
I'm also affected by this issue, I'd love if PyCharm could interpret my settings.py, that imports from specific profiles given the environment. (And I don't think it's unusual to do this in Django projects, but it certainly doesn't help that there is no official, standardised way of doing so)

However, I've found a workaround that may work for you, too:

In Project Settings > Django Support, instead of choosing your __init__.py, choose your production.py profile, or equivalent. Then at least PyCharm is actually able to retrieve your settings for template paths, databases etc.

Then for all Django Run Configurations, make sure to add "–settings=myproject.settings" to Additional Options, so that you can run with the correct settings module.

Hope that helps.
0
Just noticed that there's two dashes that prefix settings=myproject.settings, but they're not showing up.
0
Thanks, that may help some.  I'm definitely looking forward to a better option though.  Given how much the editor already needs to know about Python, it seems like importing the module directly (rather than parsing through Django's configuration framework) should be straightforward.  As tricky as my setup is, I can navigate to my project folder, run the interpreter, and execute "import settings" with no trouble at all.
0

Please sign in to leave a comment.