0

i am using pycharm with django. When i do the runserver command, my project starts up and everything is fine.

if i use the pycharm run command - that green arrow at the top - then i get problems.

The problems are:

runnerw.exe C:\development\python\python.exe manage.py runserver 127.0.0.1:8000

Traceback (most recent call last):

File "manage.py", line 11, in import settings

File "C:\development\PycharmProjects\dumpstown\settings.py", line 185, in add_to_builtins('gravatar.templatetags.gravatar')

File "C:\development\python\lib\site-packages\django\template\base.py", line 1017, in add_to_builtins builtins.append(import_library(module))

File "C:\development\python\lib\site-packages\django\template\base.py", line 963, in import_library

raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))

django.template.base.InvalidTemplateLibrary: ImportError raised loading

gravatar.templatetags.gravatar: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Process finished with exit code 1

And stem from my use of add_to_builtins here: (this is in the settings.py file)

#gravatar stuff here.
add_to_builtins('gravatar.templatetags.gravatar')

I know this is the problem, because if i remove this line in the settings.py file? everything works fine.

Is there a way to remedy this problem for pycharm?

bharal
  • 15,461
  • 36
  • 117
  • 195

2 Answers2

2

You need to set your Django settings module in Settings | Django Support | Settings

http://www.jetbrains.com/pycharm/webhelp/django-support.html

UPDATED

The problem with django-gravatar is that it's templatetags import django.contrib.auth.models.User which relies on settings module while setting module is loaded in Django by DJANGO_SETTINGS_MODULE environment variable, which is set internally by execute_manager function call in manage.py, which is executed AFTER import of settings. So when you use undocumented add_to_builtins feature just in settings.py at that point you have no DJANGO_SETTINGS_MODULE env variable set. So that is not a problem of PyCharm, but a problem of unset environment variable and usage of undocumented Django feature add_to_builtin.

When I ran the same project from Unix console I get the same error.

Probably you have DJANGO_SETTINGS_MODULE set in your environment if it works from console. So to make it work in PyCharm you need to set up the variable in Django run configuration. You can read here about that (see Environment variable section).

Dmitry Trofimov
  • 7,371
  • 1
  • 30
  • 34
0

As i answered here: https://stackoverflow.com/a/11299516/1061426

pycharm is broken and doesn't work with add_to_builtins. The two obvious solutions are:

  1. don't use pycharm, use some free django plugins for eclipse, or old school text editing?

  2. use pycharm, just don't use add_to_builtins. This is the route i've gone down - it is annoying fixing all the template's to import a module, but it was a lot simpler in my case than the hassle of porting across to a new IDE.

Community
  • 1
  • 1
bharal
  • 15,461
  • 36
  • 117
  • 195