I'm having trouble importing a resource file. I'm using pyqt4 with monkey studio and I am trying to import a png image. When I run the program I get an import error like
ImportError: No module named icon_rc
I know that I have to compile it using pyrcc4 but I don't understand how to do this can anybody help please. It would be very helpful to have an answer that fully explains how to compile the resource file so I can import it.
Open cmd (or terminal on *nix) and run
pyrcc4 -py3 F:\computing\Payrollv22\icon.qrc -o icon_rc.py
It compiled the file successfully and I was able to import the py file into my project and run it with no problem.
There really isn't much to explain here, you have a resource file (e.g. icon.qrc), then you call pyrcc4 -o icon_rc.py icon.qrc which will create a module icon_rc.py which you then can import in your project.
It's all documented here.
In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py
We need to convert that qrc file into python file and then it can be imported to your code
its because when you also used pyuic5 to convert your UI to py, the resource file name from the UI sticks.
then use
Pyrcc5 input_file.qrc -o icons.py
remove from main_script.py
import icon_rc
and use
import icons
the when calling the actual icons from the icons module, you have to look at your qrc file prefix.
< RCC >
< qresource
prefix = "ico5" >
< file > plugin.png < / file >
< / qresource >
< / RCC >
if prefix is ico5
then you load icons with
QtGui.QIcon(":/ico5/plugin.png")
and if prefix is , lets say,
<RCC>
<qresource prefix="icons">
then its:
QtGui.QIcon(":/icons/plugin.png")
you could try with pyside as well like:
--- pyside-rcc -o input.qrc output.py
I am working with executable files that are to be included in the main node file. But the problem is that although I can define the path in my PC and open a text file with or execute the file but it is not universal with my other team members which are part of the team. The have to change the code after pulling from git.
I have the following commands:
os.chdir( "/home/user/epsilon/epsilon_catkin_ws/src/knowledge_source/sense_manager/pddl_files/")
os.system("./ff -p /home/user/epsilon/epsilon_catkin_ws/src/knowledge_source/sense_manager/pddl_files/ -o domain.pddl -f problem.pddl >
solution_path = "/home/user/epsilon/epsilon_catkin_ws/src/knowledge_source/sense_manager/pddl_files/solution.txt" solution_detail.txt")
Since the path are unique with my laptop, it will require changes by everyone. IS there a way to make the path definition universal? (The commands are part of the service call that is present in that node). I am using rosrun to run the node
I tried the following pattern but it does not work:
os.chdir( "$(find knowledge_source)/sense_manager/pddl_files/")
Do I need to do something extra to make this work?
knowledge_source is the name of the package
Any recommendations?
I think you are looking for rospkg and os combination to get the path to a file inside a ros package.
import rospkg
import os
conf_file_path = os.path.join(rospkg.RosPack().get_path('your_ros_pkg'), 'your_folder_in_the_package', 'your_file_name')
I recently updated to 2.10 GIMP from 2.8, and it seems that none of my plug-ins that imported custom (relative) *.py files work anymore. I dissected the code and it's not the contents of the "path_tools.py" file, it's just the act of trying to import a custom module (even if it's an empty file it still won't work). Without this line of code it shows up in GIMP just fine:
from path_tools import *
The plugin and path_tools.py are both in the same folder
\AppData\Roaming\GIMP\2.10\plug-ins
I tried using explicit relative by adding periods
from .path_tools import *
from ..path_tools import *
from ...path_tools import *
etc.
I tried turning the file into a plug-in by adding an empty GIMP effect at the bottom of the file, to see if GIMP is just ignoring my file somehow
def path_tools(img, drw):
return
register(
"python-fu-path-tools",
"Path Tools",
"",
[...]
And by opening GIMP in command prompt
"c:\program files\gimp 2\bin\gimp-2.10" --verbose --console-messages
It just gives me a bunch of this kind of message for every plugin that won't load:
Querying plug-in: 'C:\Users\ \AppData\Roaming\GIMP\2.10\plug-ins\path_tools.py'
c:\program files\gimp 2\bin\gimp-2.10: LibGimpBase-WARNING: gimp-2.10: gimp_wire_read(): error
but those seem like warnings, especially since there is this actual error for another plug-in that wont load, for similar reasons I'm guessing:
GIMP-Error: Unable to run plug-in "export-ALL.py"
Is it related to these warnings at the top?
Parsing 'c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp'
GIMP-Warning: Bad interpreter referenced in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp: python
GIMP-Warning: Bad binary format string in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp
I just don't know what's going on, or what to do. Is there a directory that I can put the file in that it will be visible to the plugins? Because it does work to import regular modules like 'sys', 'os'.
(Sorry if this is a really stupid question, I've only used Python for GIMP plugins.)
It seems that the Python interpreter embedded with Gimp 2.10 uses / as a path separator whereas Gimp 2.10 uses \ on Windows.
The issue is discussed here.
Creating a file named C:\Program Files\GIMP 2\32\lib\python2.7\sitecustomize.py with the following content seems to fix the issue.
import sys
class Issue1542:
def __del__ (self):
if len (sys.argv[0]):
from os.path import dirname
sys.path[0:0] = [dirname (sys.argv[0])]
sys.argv = Issue1542 ()
You can hack the running file's containing directory's path into the sys.path before importing the module:
import os
import sys
sys.path.append(os.path.dirname(__file__))
import path_tools
Is there a way to convert a ui formed with qtDesigner to a python version to use without having an extra file?
I'm using Maya for this UI, and converting this UI file to a readable python version to implement would be really great!
You can use pyuic4 command on shell:
pyuic4 input.ui -o output.py
For pyqt5 you can use
pyuic5 xyz.ui > xyz.py
or
pyuic5 xyz.ui -o xyz.py
If you are using windows, the PyQt4 folder is not in the path by default, you have to go to it before trying to run it:
c:\Python27\Lib\site-packages\PyQt4\something> pyuic4.exe full/path/to/input.ui -o full/path/to/output.py
or call it using its full path
full/path/to/my/files> c:\Python27\Lib\site-packages\PyQt4\something\pyuic4.exe input.ui -o output.py
The question has already been answered, but if you are looking for a shortcut during development, including this at the top of your python script will save you some time but mostly let you forget about actually having to make the conversion.
import os #Used in Testing Script
os.system("pyuic4 -o outputFile.py inpuiFile.ui")
Quickest way to convert .ui to .py is from terminal:
pyuic4 -x input.ui -o output.py
Make sure you have pyqt4-dev-tools installed.
I'm not sure if PyQt does have a script like this, but after you install PySide there is a script in pythons script directory "uic.py". You can use this script to convert a .ui file to a .py file:
python uic.py input.ui -o output.py -x
You don't have to install PyQt4 with all its side features, you just need the PyQt4 package itself. Inside the package you could use the module pyuic.py ("C:\Python27\Lib\site-packages\PyQt4\uic") to convert your Ui file.
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py -help
update python3: use pyuic5 -help # add filepath if needed. pyuic version = 4 or 5.
You will get all options listed:
Usage: pyuic4 [options] <ui-file>
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p, --preview show a preview of the UI instead of generating code
-o FILE, --output=FILE
write generated code to FILE instead of stdout
-x, --execute generate extra code to test and display the class
-d, --debug show debug output
-i N, --indent=N set indent width to N spaces, tab if N is 0 [default:
4]
-w, --pyqt3-wrapper generate a PyQt v3 style wrapper
Code generation options:
--from-imports generate imports relative to '.'
--resource-suffix=SUFFIX
append SUFFIX to the basename of resource files
[default: _rc]
So your command will look like this:
C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py test_dialog.ui -o test.py -x
You could also use full file paths to your file to convert it.
Why do you want to convert it anyway? I prefer creating widgets in the designer and implement them with via the *.ui file. That makes it much more comfortable to edit it later. You could also write your own widget plugins and load them into the Qt Designer with full access. Having your ui hard coded doesn't makes it very flexible.
I reuse a lot of my ui's not only in Maya, also for Max, Nuke, etc.. If you have to change something software specific, you should try to inherit the class (with the parented ui file) from a more global point of view and patch or override the methods you have to adjust. That saves a lot of work time. Let me know if you have more questions about it.
I got some errors when I try to convert UI to PY and finally I found this solution.
First of all, if you couldn't find the pyuic5.bat file copy this code and paste it on your cmd:
C:\Users\Monster>cd Desktop
C:\Users\Monster\Desktop>python -m PyQt5.uic.pyuic -x trial.ui -o trial.py
And the problem has been solved easily!
Update for anyone using PyQt5 with python 3.x:
Open terminal (eg. Powershell, cmd etc.)
cd into the folder with your .ui file.
Type:
"C:\python\Lib\site-packages\PyQt5\pyuic5.bat" -x Trial.ui -o trial_gui.py
for cases where PyQt5 is not a path variable. The path in quotes " " represents where the pyuic5.bat file is.
This should work!
For Ubuntu it works for following commands;
If you want individual files to contain main method to run the files individually, may be for testing purpose,
pyuic5 filename.ui -o filename.py -x
No main method in file, cannot run individually... try
pyuic5 filename.ui -o filename.py
Consider, I'm using PyQT5.
open cmd in directory where you have your file and type:
pyuic5 –x "filename".ui –o "filename".py
I've ran into the same problem recently. After finding the correct path to the pyuic4 file using the file finder I've ran:
C:\Users\ricckli.qgis2\python\plugins\qgis2leaf>C:\OSGeo4W64\bin\pyuic4 -o ui_q gis2leaf.py ui_qgis2leaf.ui
As you can see my ui file was placed in this folder...
QT Creator was installed separately and the pyuic4 file was placed there with the OSGEO4W installer
In case that you are using Pyside6, there is a 'uic' binary file (in windows, an exe file) in /Lib/site-packages/PySide6/
I'm trying to compile my first application that uses PyQt. It compiles successfully, but if I try to run, it says:
WindowsError: [Error 2] : 'C:/Users/GHOSTM~1/AppData/Local/Temp/_MEI8722\\icons'
It says where is original error. It is in line where I set icons:
action = QtGui.QAction(QtGui.QIcon(':/data/%s' % icon), name, parent)
As you can see I import icon from resource module, which was generated with pyrcc4.exe from qrc file. Original qrc file contains such information:
<RCC>
<qresource>
<file>../data/dictionary.png</file>
<file>../data/perseus.png</file>
<file>../data/settings.png</file>
<file>../data/help.png</file>
<file>../data/quit.png</file>
</qresource>
</RCC>
Python module was generated successfully, original script works good. Am I doing smth wrong?