Pycharm& Django:ImportError:没有名为' local_settings'

时间:2018-05-22 17:27:55

标签: python django pycharm jupyter-notebook

在Jupyter笔记本中

我正在使用jupyter notebook。当我从这个命令启动jupyter notebook时,我可以运行我的脚本。

python manage.py shell_plus --notebook

python版本是 3.5.0(默认,2016年9月14日,05:13:11) [GCC 4.2.1兼容的Apple LLVM 7.3.0(clang-703.0.31)] 它工作正常。

Pycherm中使用Jupyter笔记本

现在我想在Pycharm中使用jupyter。

import os, sys
import dateutil.parser
import django
MYPROJECT = 'django/project/path'
sys.path.insert(0, MYPROJECT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_settings.py")
django.setup()

这个python版本也是3.5.0(默认,2016年9月14日,05:13:11) [GCC 4.2.1兼容的Apple LLVM 7.3.0(clang-703.0.31)]

所以我认为这与Django cannot import LOCAL settings

的问题不同

我在pycherm jupyter中遇到了这个错误。为什么我得到" ImportError:没有名为' local_settings'"?的模块 如果你知道如何解决这个问题。请帮帮我!!

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-18-d97420db067c> in <module>()
      7 sys.path.insert(0, MYPROJECT)
      8 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_settings.py")
----> 9 django.setup()
     10 from django.conf import settings
     11 import main.management.commands.mws as mws

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/__init__.py in setup(set_prefix)
     17     from django.utils.log import configure_logging
     18 
---> 19     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
     20     if set_prefix:
     21         set_script_prefix(

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in __getattr__(self, name)
     54         """Return the value of a setting and cache it in self.__dict__."""
     55         if self._wrapped is empty:
---> 56             self._setup(name)
     57         val = getattr(self._wrapped, name)
     58         self.__dict__[name] = val

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in _setup(self, name)
     41                 % (desc, ENVIRONMENT_VARIABLE))
     42 
---> 43         self._wrapped = Settings(settings_module)
     44 
     45     def __repr__(self):

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in __init__(self, settings_module)
    104         self.SETTINGS_MODULE = settings_module
    105 
--> 106         mod = importlib.import_module(self.SETTINGS_MODULE)
    107 
    108         tuple_settings = (

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/__init__.py in import_module(name, package)
    124                 break
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 
    128 

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _gcd_import(name, package, level)

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load(name, import_)

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ImportError: No module named 'local_settings.py'; 'local_settings' is not a package

我尝试了什么

我在settings.py

上添加了这一行
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))

try:
    from local_settings import *
except ImportError:
    pass

我的local_settigns就是这个。

from .settings import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

但我收到了这个错误。

   ---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-10-93d2cfaa1e85> in <module>()
      8 sys.path.insert(0, MYPROJECT)
      9 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_settings.py")
---> 10 django.setup()
     11 from django.conf import settings
     12 import main.management.commands.mws as mws

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/__init__.py in setup(set_prefix)
     17     from django.utils.log import configure_logging
     18 
---> 19     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
     20     if set_prefix:
     21         set_script_prefix(

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in __getattr__(self, name)
     54         """Return the value of a setting and cache it in self.__dict__."""
     55         if self._wrapped is empty:
---> 56             self._setup(name)
     57         val = getattr(self._wrapped, name)
     58         self.__dict__[name] = val

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in _setup(self, name)
     41                 % (desc, ENVIRONMENT_VARIABLE))
     42 
---> 43         self._wrapped = Settings(settings_module)
     44 
     45     def __repr__(self):

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/conf/__init__.py in __init__(self, settings_module)
    104         self.SETTINGS_MODULE = settings_module
    105 
--> 106         mod = importlib.import_module(self.SETTINGS_MODULE)
    107 
    108         tuple_settings = (

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/__init__.py in import_module(name, package)
    124                 break
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 
    128 

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _gcd_import(name, package, level)

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load(name, import_)

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ImportError: No module named 'local_settings.py'; 'local_settings' is not a package

1 个答案:

答案 0 :(得分:0)

我通过这段代码解决了这个问题。只需在脚本上运行。

import os, sys, django
sys.path.append('/my/project/path')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') #if you have local_settings.py it's all so works well.
django.setup()

也许问题来自 sys.path.insert(0, MYPROJECT)