Skip to content

Instantly share code, notes, and snippets.

@whoiscarlo
Last active March 13, 2024 12:12
Show Gist options
  • Save whoiscarlo/9f87df53b86085719e24dff350aede5b to your computer and use it in GitHub Desktop.
Save whoiscarlo/9f87df53b86085719e24dff350aede5b to your computer and use it in GitHub Desktop.
Convert PyQt4 to PyQt5
import os, re, stat, sys, shutil
from PyQt5 import QtWidgets, QtCore
qtWidgets_modules = dir(QtWidgets)
qtCore_modules = dir(QtCore)
def fixFile(line_list):
line_generator = iter(line_list)
changed = False
num = -1
for each in line_generator:
num += 1
if 'from PyQt4 import' in each.strip():
line_list[num] = 'from PyQt5 import QtWidgets, QtGui, QtCore\n'
changed = True
continue
result = re.search('QtGui\.(\w+)', each)
if result and result.group(1) in qtWidgets_modules:
line_list[num] = each.replace('QtGui', 'QtWidgets')
changed = True
if result and result.group(1) in qtCore_modules:
line_list[num] = each.replace('QtGui', 'QtCore')
changed = True
if changed:
return ''.join(line_list)
def convert(main_path):
for dirs, folders, files in os.walk(main_path):
for each in [x for x in files if x.endswith('.py')]:
file_path = os.path.join(dirs, each).replace(os.sep, '/')
os.chmod(file_path, stat.S_IWRITE)
with open(file_path, 'r+') as out_file:
line_list = out_file.readlines()
fixed_lines = fixFile(line_list)
if fixed_lines:
## Copy and rename file into a folder name old_qt4 so source is not lost
dir_name, file_name = os.path.split(file_path)
new_location = os.path.join(dir_name, 'old_qt4', file_name)
shutil.copy(file_path, new_location)
## Rewrite file with changes from Qt4 to Qt5
with open(file_path, 'w+') as out_file:
out_file.write(fixed_lines)
if __name__ == "__main__":
## Get directory path from user
folder_path = raw_input('Where is the directory that needs to be converted:')
## Check if path is a directory
if os.path.isdir(folder_path) :
convert(main_path=folder_path)
print('All ".py" files in %s have been converted to Qt5')
else:
print('File path: %s does not exists'%(folder_path))
@nicman23
Copy link

nicman23 commented Nov 1, 2020

please add sys

and change main_path = 'C:/' to main_path = sys.argv[1]

@whoiscarlo
Copy link
Author

whoiscarlo commented Nov 1, 2020

@nicman23 - K, it's been updated. Thanks for the feedback!

@ParsaGhs
Copy link

How can I use it?

@whoiscarlo
Copy link
Author

Hey @ParsaGhs -

I updated the script to be a bit more transparent. Run the file in a python terminal and it will ask you to put in the folder directory of the files you want to convert to Qt5. If the path exists and the python files it finds contain Qt4 it will convert them and copy the old file into a folder called old_qt4.

Let me know if you run into any issues

  • Carlo

@jiten597
Copy link

I am getting FileNotFoundError.
image

@jade1siriani
Copy link

raw_input is for python 2.x. If you're using python 3 and above replace "raw_input" with "input"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment