在python和时区问题中启动python [英] Launching python within python and timezone issue

查看:58
本文介绍了在python和时区问题中启动python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的django应用制作启动程序脚本,而且似乎以某种方式将时区切换为GMT(默认值为+2),并且使用该脚本时每个日期时间都晚了两个小时.可能是什么原因造成的?

I had to make a launcher script for my django app, and it seems it somehow switches the timezone to GMT (default being +2), and every datetime is two hours behind when using the script. What could be causing that?

这是我使用的启动脚本:

Here is the launcher script that I use:

#!/usr/bin/env python

import os
import subprocess
import shlex
import time

cwd = os.getcwd()

p1 = subprocess.Popen(shlex.split("python manage.py runserver"),
        cwd=os.path.join(cwd, "drugsworld"))
p2 = subprocess.Popen(shlex.split("python coffee_auto_compiler.py"),
        cwd=os.path.join(cwd))

try:
    while True:
        time.sleep(2)
except KeyboardInterrupt:
    p1.terminate()
    p2.terminate()

如果我手动运行 python manage.py runserver ,则时区为+2.但是,如果我使用此脚本,则时区设置为GMT.

If I manually run python manage.py runserver, the timezone is +2. If, however, I use this script, the timezone is set to GMT.

推荐答案

嗯.Python尊重TZ环境变量...您并未在脚本中对其进行更改,因此它等同于在shell上运行它.

Hmm. Python respects the TZ environment variable... you're not changing it in your script, so it should be equivalent to running it at the shell.

我经常明确设置时区.特别是在Django中,您可以在settings.py文件(TIME_ZONE)中进行设置.更通用的python是:

I often set the time zone explicitly. In Django specifically, you can set this in the settings.py file (TIME_ZONE). More general python is:

os.environ['TZ']="America/New_York"
time.tzset()

我想如果您在设置文件中设置时区,问题将会消失.

I imagine if you set the timezone in your settings file, the problem will go away.

这篇关于在python和时区问题中启动python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆