我正在开发一个导入django-imagekit的Django项目;在我的Windows机器上一切正常。然而,在我的Linux-Ubuntu笔记本电脑上,Pycharm在编辑器中识别出该软件包,它在项目的解释器软件包中列出,但是在命令行中无法识别它:
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
它似乎在查看python 2的软件包,但我在这个项目中使用python 3。我尝试了一些方法,例如添加项目变量的路径,但到目前为止,我无法使其工作。
尝试从python 2的shell中导入imagekit:
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
尝试从python 3的shell中导入imagekit:
>>> 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.
答案 0 :(得分:1)
如果您使用的是Python 3,那么您应该使用python3 manage.py check
。使用虚拟环境可能更好,在这种情况下,您可以在运行python manage.py check
之前激活虚拟环境。
导入在Python 3 shell中失败,因为您尚未设置DJANGO_SETTINGS_MODULE
环境变量(有关详细信息,请参阅the docs)。最简单的解决方法是使用Django shell,它会为你处理这个问题。
$ python3 manage.py shell
>>> import imagekit