开发者

Importing a Python Module inside Custom Django Templatetags

开发者 https://www.devze.com 2023-02-17 19:26 出处:网络
I\'m using virtualenv with my Python Django installation. Here is my directory structure: project/ dev_environ/

I'm using virtualenv with my Python Django installation.

Here is my directory structure:

project/
    dev_environ/
        lib/
            python2.6/
                site-packages/
                    ...
                    django/
                    titlecase/   # <-- The titlecase module
                    PIL/
                    ...
        bin/
            ...
            python  # <-- Python
            ...
        include/

    django_project/
        localsite/
            templatetags/
                __init__.py
                smarttitle.py    # <-- My templatetag module
        foo_app/
        bar_app/
        settings.py
        manage.py

If I start my Django shell and attempt to import titlecase everything is fine, because titlecase is in the sys.path at dev_environ/lib/python2.6/site-packages/titlecase.

$:django_project cwilcox$ ../dev_environ/bin/python manage.py shell
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import titlecase  # <-- No Import Error!
>>> 

I can even do an import titlecase inside my settings.py file without error.

However when I try to import titlecase in my templatetag library smarttitle.py I get an ImportError.

smarttitle.py is as follows.

from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
from titlecase import titlecase as _to_titlecase

@register.filter
@stringfilter
def smarttitle(value):
    return _to_titlecase(value)
smarttitle.is_safe = True

Not only that but I can even import titlecase inside the view that renders the template that tries to {% load smarttitle %} and there's no error.

My Django dev server is started with...

../dev_environ/bin/python manage.py runserver

In Summary:

I can import the titlecase module anywhere except insid开发者_如何学Pythone this templatetag library, where it throws an ImportError! What gives?! Any ideas?


EDIT: I tried first running source dev_environ/bin/activate to switch my shell env to my virtualenv, but that didn't help--I'm still getting the ImportError inside my templatetag module. I was already calling the proper python binary manually.


As stated in the comments, you need to activate your virtualenv by doing source bin/activate (or just . bin/activate) before running the devserver, even if you are already accessing the right Python executable.


I know that this is too old, but I get a similar problem today.

The problem seems to be using the same name for the app and the module, so when it tries to import it could fail looking for the desired module or function in the wrong place.

I recommend you to give different names to the django app or module.


This is not a fix, but just to establish that we are looking at the same problem/bug:

If you change the import in smarttitle.py to

from YOURPROJECT.titlecase import titlecase as _to_titlecase

it will work with 'runserver' but it will fail on production (in my case uwsgi/nginx)

0

精彩评论

暂无评论...
验证码 换一张
取 消