Search code examples
pythondjangopackagedjango-imagekit

Python package recognized in Pycharm, not in terminal


I'm developing a Django project which imports django-imagekit; everything works fine on my Windows machine. On my Linux-Ubuntu laptop though, Pycharm recognizes the package in the editor, it's listed in the project's interpreter's packages but it's not recognized from the command line:

    simon@Simon-Swanky:~/PycharmProjects/tcspt$ python manage.py check
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/simon/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/simon/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
    django.setup()
  File "/home/simon/.local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/simon/.local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/simon/.local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/simon/PycharmProjects/tcspt/assetmanage/models.py", line 3, in <module>
    from imagekit.models import ProcessedImageField
ImportError: No module named imagekit.models

It seems to be looking in python 2's packages but I'm using python 3 for this project. I tried a few things like adding the path to the project variables but so far I can't get it to work.

Trying to import imagekit from python 2's shell:

Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import imagekit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named imagekit

Trying to import imagekit from python 3's shell:

>>> import imagekit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/imagekit/__init__.py", line 2, in <module>
    from . import conf
  File "/usr/local/lib/python3.5/dist-packages/imagekit/conf.py", line 5, in <module>
    class ImageKitConf(AppConf):
  File "/usr/local/lib/python3.5/dist-packages/appconf/base.py", line 74, in __new__
    new_class._configure()
  File "/usr/local/lib/python3.5/dist-packages/appconf/base.py", line 100, in _configure
    value = getattr(obj._meta.holder, prefixed_name, default_value)
  File "/home/simon/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/home/simon/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting IMAGEKIT_DEFAULT_CACHEFILE_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Solution

  • If you are using Python 3, then you should be using python3 manage.py check. It might be better to use a virtual environment, in which case you would activate the virtual environment before running python manage.py check.

    The import fails in the Python 3 shell because you have not set the DJANGO_SETTINGS_MODULE environment variable (see the docs for more info). The easiest fix is to use the Django shell, which takes care of this for you.

    $ python3 manage.py shell
    >>> import imagekit