When running the code, I got following error:
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
The source code is like following:
from PySide.QtGui import *
This is because in AboutWindow.py file, the GitFtpWindow identifier is a package, but not a class.
To fix this problem, we need modify AboutWindow.py file like following:
from GitFtpWindow import GitFtpWindow
Today I want to build a python script to EXE file using PyInstaller. But after running PyInstaller, I got following error
No module named pefile
The full stack trace is
Traceback (most recent call last):
File "D:\\PyInstaller\\pyinstaller.py", line 14, in <module>
from PyInstaller.__main__ import run
File "D:\PyInstaller\PyInstaller\__main__.py", line 21, in <module>
File "D:\PyInstaller\PyInstaller\building\build_main.py", line 34, in <module>
from .api import PYZ, EXE, COLLECT, MERGE
File "D:\PyInstaller\PyInstaller\building\api.py", line 38, in <module>
from PyInstaller.utils.win32 import winmanifest, icon, versioninfo, winresource
File "D:\PyInstaller\PyInstaller\utils\win32\versioninfo.py", line 17, in <module>
ImportError: No module named pefile
Starting from PyInstaller 3.2, a new module named pefile is introduced. This module is used to work with Windows binary file (EXE file, also called PE file). Before PyInstaller 3.2, PE related operation is done using its own module, like setting PE header and DOS header)
Assume the Python installation direcotry is C:\Python27.
Open Command Prompt window, and navigate to C:\Python27\Scripts
Then install pefile module using following command
pip install pefile
(pip is built into latest Python distribution, 2.7.12 for now)
Recently I built a GUI application using PySide, and do some background calculation using multiprocessing. Using Python interpreter to run this application works smoothly. But if I freeze it to Windows executable file (exe) and run it, guess what, I got two main windows showing up!
After searching a while, I found following code should be added
It should be placed right after:
if __name__ == '__main__':
The freeze_support() is mainly used to pass initialization data from parent process to newly created process using pipe, including modules, process name, current working directory, etc. In Unix-based system this function is not needed, because fork will do these things for us (multiprocessing.Process will call os.fork() in start method on Linux)
the low-level implementation is in Lib/multiprocessing/forking.py and a wrapper in Lib/multiprocessing/__init__.py.
By reading the source code, we can see Python will detect –multiprocessing-fork in command line arguments to determine whether current process is child process or not. And the last command line argument is the pipe file handle. The data in main process is serialized using pickle, then pass to child process using pipe.
In C or C++, a DateTime value is often represented by time_t type, it’s a UNIX timestamp format, which is number of seconds elapsed since 1970-1-1. This type is defined as long (4 bytes long) on 32 bit machine, and long long (8 bytes long) on 64 bit machine.
And Datetime value has one important trait:
In short time the high byte won’t change.
Suppose a DateTime value is DDCCBBAA, so in memory it will be AA BB CC DD (in little endian), the highest byte (DD) won’t change in short time, and the second highest byte (CC) don’t change much. This is because the low 2 bytes can represent 65536 seconds, which is about 18 hours, that means after every 18 hours the second highest byte will only increase by 1. And the highest byte only change after 18*256 hours (nearly 192 days) passed by.
Next we will see how to use Python to convert bytes array to timestamp value, and display it as readable format
import struct, datetime
timestamp = struct.unpack('<L', data[4:8])
print datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
The data variable contains bytes needed work on, after some observing and investigation, we think byte 4 to byte 8 is the DateTime value. Then we use struct.unpack function to convert the bytes to timestamp value, ‘<L’ means little endian and long type. Next we will convert timestamp value to human-readable format.
Note that time_t doesn’t contain timezone information, you need discover timezone at other places.
When you want to display a warning message (like “File doesn’t exist”) to user, using message box is a good choice. Following code is how to create a message box in PySide:
msg_box = QMessageBox()
QMessageBox is the message window class, it’s defined in PySide.QtGui module. setText method will set the information text, and exec_ method will display the message box and waiting for user’s operation.
In this post we will enable Python CGI for Hostgator
Create a .htaccess file and put following contents in this file:
AddHandler cgi-script .py
This will make the web server to interprete .py file as Python CGI script.
Next create a python file (named test.py here) with following code:
print 'Content-Type: text/html\n\n'
And set this file’s permission setting so that Owner, Group and Public have “execute” permission for this file (Or set 755 permission simply)
Visit http://server_address/app_path/test.py, you will see a page with “hello” printed.
The first two lines of code is necessary, otherwise you will get a “500 Internal Server Error”.
There are two methods in Python to make the process wait some seconds:
e = threading.Event()
e.wait(5) #make the process to wait for 5 seconds
time.sleep(2) #let the program to sleep for 2 seconds
In default the QTableView will enable edit mode if user double clicks a table cell.
Following code will disable the edit mode:
In some old versions of Qt, QAbstractView will be used instead of QAbstractItemView. But in all versions of PySide, QAbstractView is not defined so QAbstractItemView need be used.