简体   繁体   中英

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

I have the two following files:

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

#! /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. Why is this happening and how can I fix it?

Rename datetime.py to something else and remove directory __pycache__ , if any. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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