简体   繁体   English

尽管代码不同,但两个不同的文件仍提供相同的输出-PyQT5和Python

[英]Two different files giving same output despite having different codes - PyQT5 and Python

I have the two following files: 我有以下两个文件:

datetime.py datetime.py

#! /usr/bin/python3.6

from PyQt5.QtCore import QDate, QTime, QDateTime, Qt

now = QDate.currentDate()

print(now)
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))

datetime = QDateTime.currentDateTime()

print(datetime)
print(datetime.toString())

time = QTime.currentTime()

print(time)
print(time.toString(Qt.DefaultLocaleLongDate))
print("\n")
print("UTC Time: " + datetime.toUTC().toString(datetime.offsetFromUtc()))

It was running fine at the beginning, however, upon running it after a couple of times, I'm getting this result: 一开始它运行良好,但是经过几次运行后,我得到了以下结果:

PyQt5.QtCore.QDate(2018, 8, 19)
2018-08-19
Sunday, August 19, 2018
PyQt5.QtCore.QDateTime(2018, 8, 19, 21, 47, 36, 885)
Sun Aug 19 21:47:36 2018
PyQt5.QtCore.QTime(21, 47, 36, 885)
9:47:36 PM MDT


UTC Time: Mon Aug 20 03:47:36 2018 GMT
Segmentation fault (core dumped)

The segmentation fault wasn't there at the beginning, it just started to pop up. 分割错误一开始并不存在,它只是开始弹出。

Then I created a new file: 然后我创建了一个新文件:

daysto.py daysto.py

#! /usr/bin/python3.6

from PyQt5.QtCore import QDate

xmas1 = QDate(2017, 12, 25)
xmas2 = QDate(2018, 12, 25)
daysToAnniversary1 = QDate(2018, 3, 3)
daysToAnniversary2 = QDate(2019, 3, 3)

now = QDate.currentDate()

daysPassed = xmas1.daysTo(now)

print(daysPassed)

The result I'm seeing is: 我看到的结果是:

PyQt5.QtCore.QDate(2018, 8, 19)
2018-08-19
Sunday, August 19, 2018
PyQt5.QtCore.QDateTime(2018, 8, 19, 21, 47, 36, 885)
Sun Aug 19 21:47:36 2018
PyQt5.QtCore.QTime(21, 47, 36, 885)
9:47:36 PM MDT


UTC Time: Mon Aug 20 03:47:36 2018 GMT
Segmentation fault (core dumped)

So basically, I'm running two different files, but I still see the same output - which is weird because the second file is supposed to just give me a number. 所以基本上,我正在运行两个不同的文件,但是我仍然看到相同的输出-这很奇怪,因为第二个文件应该只是给我一个数字。 I copied the file to my home directory and it worked fine. 我将文件复制到我的主目录,并且工作正常。 However, as long as I'm in that directory, it's not working. 但是,只要我在该目录中,它就不会起作用。 I'm on Ubuntu 18 and my these files are chmod +x ed. 我在Ubuntu 18上,这些文件都是chmod +x ed。 Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决?

Rename datetime.py to something else and remove directory __pycache__ , if any. datetime.py重命名为其他名称,并删除目录__pycache__ (如果有)。 datetime is a standard Python module, but your namesake file is being imported instead of it, thus causing the duplicate output and the further crash. datetime是标准的Python模块,但是您导入的是同名文件而不是文件,因此导致输出重复并进一步崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM