[英]Python package recognized in Pycharm, not in terminal
I'm developing a Django project which imports django-imagekit; 我正在开发一个导入django-imagekit的Django项目; everything works fine on my Windows machine.
在Windows计算机上,一切正常。 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:
但是,在我的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
It seems to be looking in python 2's packages but I'm using python 3 for this project. 似乎正在使用python 2的软件包,但我在此项目中使用的是python 3。 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的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
Trying to import imagekit from python 3's shell: 尝试从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.
If you are using Python 3, then you should be using python3 manage.py check
. 如果您使用的是Python 3,则应该使用
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
. 使用虚拟环境可能会更好,在这种情况下,您将在运行
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). 由于尚未设置
DJANGO_SETTINGS_MODULE
环境变量(请参阅文档以获取更多信息),因此在Python 3 Shell中导入失败。 The easiest fix is to use the Django shell, which takes care of this for you. 最简单的修复方法是使用Django shell,它会为您解决这一问题。
$ python3 manage.py shell
>>> import imagekit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.