Forums

Script Can't See Modules - but Shell Can

I'm running 3.5 in virtenv. Everything works fine - and then I try to run a script. Here's the error:

Traceback (most recent call last):
  File "check_wins.py", line 2, in <module>
    from teamsports.models import Teams, Schedule
  File "/home/mrsaltz/thepub/teamsports/models.py", line 5, in <module>
    from versatileimagefield.fields import VersatileImageField
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/fields.py", line 10, in <module>
    from .files import VersatileImageFieldFile, VersatileImageFileDescriptor
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/files.py", line 11, in <module>
    from .mixins import VersatileImageMixIn
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/mixins.py", line 9, in <module>
    from .datastructures import FilterLibrary
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/datastructures/__init__.py", line 3, in <module>
    from .sizedimage import SizedImage
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/datastructures/sizedimage.py", line 6, in <module>
    from ..settings import (
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/versatileimagefield/settings.py", line 68, in <module>
    None
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/mrsaltz/.virtualenvs/thepub/lib/python3.5/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting VERSATILEIMAGEFIELD_SETTINGS, but settings are not configured. You must eithe
r define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Versatileimagefield works great on my site. And I can run all of these commands within a shell (python manage.py shell).

Any idea why this is happening?

Here you go: http://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

I'm still confused. I'm running all this in my virtualenv. So even if my Bash console is in a virturalenv, I still have to set these variables?

Second, any idea which variables I would set? Would it be the VersativeImageField stuff?

Thank you. Sorry I'm new.

You have to set them in whatever environment you're running in or you need to call settings.configure() in your script. Did you call settings.configure in your script before accessing the settings?

There is no way for me to know what variable you need to set. The only answer I can give there is "the ones that your code needs"

I'm an idiot.

For posterity: If you are running a script within Django,

import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()

To the beginning of your .py file. You can also run it from the shell

SOURCE

python manage.py < yourfile.py

But that didn't work as well for me.

Glad you worked it out!

The normal way to run a script that uses your Django stuff is to write a management command, so that you can do something like

python manage.py myscript

-- note that there's no "<" and no ".py" at the end of the script name. They do require a certain amount of setup, though -- see this page in the Django docs