使用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,具体如下图所示:

 
 
 
 

PyCharm 在django程序中单独运行py文件的更多相关文章

  1. django 中单独执行py文件修改用户名

    Python文件代码 import os import django # 在environ字典里设置默认Django环境,'xxxx.settings'指Django项目的配置文件 os.enviro ...

  2. Pycharm 的设置--参数设置(运行.py文件带参数,例如argument)

    程序运行时,如果需要输入参数,如下图中程序代码: 在生成exe后,程序在控制台下运行时格式为: 这种情况在调试程序时,如果只是在Pycharm环境中简单执行“Run”菜单下的“Run”命令,会出现以下 ...

  3. python -m 命令单独运行一个文件,怎么解决单独运行文件报错?

    依旧是续上篇解决为什么项目能运行,单独文件不能运行. 依旧是python3先发下目录结构,依旧是cmd运行,不要弄pycharm开始运行,否则有些错误就发现不了! 项目下面有pac1文件夹,pac1下 ...

  4. 命令行运行py文件报错

    起因 今天用ubuntu 终端运行py文件报了个错,找不到模块? 我切换回pycharm中运行,运行一切正常 解决 在报错模块中,插入绝对路径 import sys sys.path.append(' ...

  5. 『德不孤』Pytest框架 — 14、Pytest中的conftest.py文件

    目录 1.conftest.py文件介绍 2.conftest.py的注意事项 3.conftest.py的使用 4.不同位置conftest.py文件的优先级 5.conftest.py中Fixtu ...

  6. python2中的__init__.py文件的作用

    python2中的__init__.py文件的作用: 1.python的每个模块的包中,都必须有一个__init__.py文件,有了这个文件,我们才能导入这个目录下的module. 2.__init_ ...

  7. Python NLTK——代码重用,F5运行py文件cmd闪退,invalid syntax

    打开IDLE,对代码进行保存(Ctrl+S)后,代码都是可以运行的. 但是打开文件就会弹出cmd并闪退,截了好几次图发现报的是Invalid syntax的错. 后来发现应该在IDLE中新建一个fil ...

  8. bat脚本运行py文件失败(一闪而过)

    简单记录下问题及原因,方便回顾. 问题 通过 bat 脚本运行 py 文件时,终端一闪而过,没能成功运行. 查证后发现问题出在编码上: 首先检查下bat文件编码格式(推荐 notepad++ ) 打开 ...

  9. Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

    上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...

随机推荐

  1. Windows Server 2012 R2搭建IIS服务器

    1-单击宫格菜单的第一个“服务器管理器”: 2 2-在“快速启动(Q)”子菜单下,单击“2 添加角色和功能”: 3 3-点击左边“安装类型”,然后单击“基于角色或基于功能的安装”,再单击“下一步(N) ...

  2. Nodejs安装使用,以及不错的Nodejs或者JS资料整理

    先按照这个教程来学习:Node.js教程 - 菜鸟教程网 在mac上使用brew安装了nodejs,中间还是用到了先下载到cache目录的方法. 但是后来发现这样按照的node,没有安装npm. 找到 ...

  3. IP数字,数字IP

    DECLARE @ip VARCHAR(60)='113.118.138.159'DECLARE @ip_int BIGINT=[dbo].[f_IP2Int](@ip) SELECT [IPstar ...

  4. Android -- 利用Broadcast开启Service

    Broadcast和Service都是Android四大组建之一的. 这里的广播是动态的,自己注册的一个广播. 这种最典型的用法就是利用开机广播,然后再起自己的服务,也就是在Android手机中做到开 ...

  5. 如何模拟登陆添加了CSRF保护的网站

    上次写了篇文章,内容是如何利用WebClient模拟登陆CSRF控制的网站,回复中有人还是不理解,现在另开一篇,打算说说用Python如何来登陆. 开写之前,先说下为什么webrequest不行,常规 ...

  6. android学习的网站收集

    1. http://mob.com/#/index 提供分享等统一解决方案 2. http://bbs.apkbus.com/explore/ 这个类似的quroa问答模块,覆盖不错.就是人气,稍差. ...

  7. [Grunt] Cleaning your build folder with grunt-contrib-clean

    Grunt will clean up your build with the grunt-contrib-clean to make sure that no artifacts from prev ...

  8. 流编辑器sed

    sed与grep一样,都起源于老式的ed编辑器,因其是一个流编辑器(stream editor)而得名.与vi等编辑器不同,sed是一种非交互式编辑器(即用户不必参与编辑过程),它使用预先设定好的编辑 ...

  9. Some web Address

    1.可视化算法(Data Structure Visualizations) https://www.cs.usfca.edu/~galles/visualization/Algorithms.htm ...

  10. 关于SO_REUSEADDR的使用说明~

    参考WINDOWS   网络编程技术         1.   可以对一个端口进行多次绑定,一般这个是不支持使用的:     2.   对于监听套接字,比较特殊.如果你定义了SO_REUSEADDR, ...