PyCharm - Django设置在运行配置中受到尊重,但在python控制台中却没有

时间:2016-07-25 12:21:27

标签: python django pycharm

在PyCharms中设置我的django项目时,我无法为django配置python shell。我的项目结构如下:

- mysite_root
    - deployment
        - ...ansible files
    - mysite
        - __init__.py
        - manage.py
        - mysite
            - __init__.py
            - settings.py
            - local_dev_settings.py
        - app1
            - __init__.py
            - ...other files
        - app2
            - __init__.py
            - ...other files

源根设置为mysite_root/mysite 我的运行/调试配置具有环境变量DJANGO_SETTINGS_MODULE=mysite.local_dev_settings&它完美地运作。 在Language & Frameworks --> Django下的项目设置内,配置如下:

- Django project root -- <path to mysite_root/mysite>
- Settings -- mysite/local_dev_settings.py
- Manage script -- manage.py
- Environment variables -- DJANGO_SETTINGS_MODULE=mysite.local_dev_settings

现在,在通过工具启动Python控制台或Manage.py任务时,我收到以下错误消息:

Traceback (most recent call last):
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-4578708c9793>", line 5, in <module>
    if 'setup' in dir(django): django.setup()
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'mysite.local_dev_settings'

编辑:找到问题的解决方案 请参阅以下答案以解决此问题。

1 个答案:

答案 0 :(得分:2)

自己找到了决议。

问题在于构建,执行,部署 - &gt; 控制台 - &gt; Django Console Content&amp;来源根包括在内&amp;因此mysite_root/mysite出现在mysire_root/mysite/mysite之前,因为PyCharm在加载设置时失败了,即使它也应该查看后一个包。禁用选项Add Contents root to PYTHONPATH后,一切正常。 :)