PyCharm 在django程序中单独运行py文件
使用PyCharm开发django程序,发现如果不在命令行而在IDE的django项目中直接运行django程序,发现报错,程序如下:
def main(): from people.models import Blog blog = Blog() blog.name = 'blog1' blog.tagline = 'tagline1' blog.save() if __name__ == '__main__': main() print('Done......')
抛出异常如下:
C:\python\python.exe C:/Users/Administrator/PycharmProjects/mydjango/mytest.py Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 10, in <module> main() File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 3, in main from people.models import Blog File "C:\Users\Administrator\PycharmProjects\mydjango\people\models.py", line 4, in <module> class Person(models.Model): File "C:\Users\Administrator\PycharmProjects\mydjango\people\models.py", line 5, in Person name = models.CharField(max_length=30) File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\db\models\fields\__init__.py", line 1043, in __init__ super(CharField, self).__init__(*args, **kwargs) File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\db\models\fields\__init__.py", line 166, in __init__ self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 53, in __getattr__ self._setup(name) File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 39, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Process finished with exit code 1
经上网查找,增加代码处理如下:
import os import django os.environ.setdefault('DJANGO_SETTING_MODULE', 'mydjango.settings') django.setup() ''' version = django.get_version() print(version) version = (int(version[0]), int(version[2:])) print(version) if version >= (1,7): django.setup() ''' def main(): from people.models import Blog blog = Blog() blog.name = 'blog1' blog.tagline = 'tagline1' blog.save() if __name__ == '__main__': main() print('Done......')
运行后发现还是异常,此时是LOGGING_CONFIG没有设置:
C:\python\python.exe C:/Users/Administrator/PycharmProjects/mydjango/mytest.py Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 5, in <module> django.setup() File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\__init__.py", line 22, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 53, in __getattr__ self._setup(name) File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 39, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Process finished with exit code 1
经过查找,终于发现将配置添加到pycharm中的python配置中,可以正常运行程序,配置项的key为DJANGO_SETTINGS_MODULE,value为django的项目名称加点号加settings,我的项目名称为mydjango,所以value为mydjango.settings,具体如下图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
2016-12-19 jquery checkbox的相关操作——全选、反选、获得所有选中的checkbox