Python 包在 Pycharm 中被识别,而不是在终端中
Python package recognized in Pycharm, not in terminal
我正在开发一个导入 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.
如果您使用的是 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
我正在开发一个导入 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.
如果您使用的是 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