I have .ui, .py and .pyc files generated. Now, when I edit the .py file, how will it reflect changes in the .ui file? How do I connect .the ui and .py files together as QT designer allows only .ui files for running purposes?
Don't edit the generated .py file. Create another .py file for the Python code you need to write. This module should load the UI. This would also be the module to run, if these are the only files in the project. See a tutorial from here: http://diotavelli.net/PyQtWiki/Tutorials
the .py file generated from pyuic is not supposed to be edited by the user, best way it is intended to use is to subclass the class in .py file into your own class and do the necessary changes in your class..
Related
I am creating a python script that should modify itself and be portable.
I can achieve each one of those goals separately, but not together.
I use cx_freeze or pyinstaller to pack my .py to exe, so it's portable; but then I have a lot of .pyc compiled files and I can't edit my .py file from the software itself.
Is there a way to keep a script portable and lightweight (so a 70mb portable python environment is not an option) but still editable?
The idea is to have a sort of exe "interpreter" like python.exe but with all the libraries linked, as pyinstaller allows, that runs the .py file, so the .py script can edit itself or be edited by other scripts and still be executed with the interpreter.
First define your main script (cannot be changed) main_script.py. In a subfolder (e.g. named data) create patch_script.py
main_script.py:
import sys
sys.path.append('./data')
import patch_script
inside the subfolder:
data\patch_script.py:
print('This is the original file')
In the root folder create a spec file e.g. by running pyinstaller main_script.py.
Inside the spec file, add the patch script as a data resource:
...
datas=[('./data/patch_script.py', 'data' ) ],
...
Run pyinstaller main_sript.spec. Execute the exe file, it should print
This is the original file
Edit the patch script to e.g. say:
print('This is the patched file')
Rerun the exe file, it should print
This is the patched file
Note: As this is a PoC, this works but is prone to security issues, as the python file inside the data directory can be used for injection of arbitrary code (which you don't have any control of). You might want to consider using proper packages and update scripts as used by PIP etc.
I am trying to automatically convert Qt designer files with .ui extensions to .py file inPycharm using an external tool created with Pyrcc5.exe script. The arguments are:
$FileName$ -o $FileNameWithoutExtension$.py
After executing the .ui file with the external tool that I created I am getting this error:
The external tool named Pyuic and it's details are here:
I don't want to manually convert each .ui file into .py and then put it into the project directory. A solution to this error would be appreciated.
I have found a very simple solution to this.
Just replace the Pyrcc5.exe with Pyuic5.exe and the put the arguments as this:
-x $FileName$ -o $FileNameWithoutExtension$.py
When a .py file is being edited in IDLE, it can be renamed or deleted while still being able to run, but in other file types such as word files this is not allowed. You can even create a script using the os module to delete itself and then do an action, and that action still works. How is this possible?
When you run python scripts you scripts were loaded into memory. So you can edit your script files.
I wrote a program in Python that includes external files, including images and a GUI .ui file.
I want to convert the Python script to a single executable (.exe) file using PyInstaller. When I try to open the .exe file, the program does not open.
You will need more information on why the application is closing immediately in order to solve your problem. To view the error messages associated with running your executable, run the .exe file from the command prompt: /path/to/app/dist/MyApp.exe. This will allow you to observe any errors that may exist after the app was bundled. If the program fails during an import statement, you may need to add a package to the hiddenimports list in the .spec file. Check the PyInstaller documentation for instructions on modifying the .spec file.
I created all of my python files (for daily file processing) in Jupyter because it makes it easy for me to view what is going on with the data. Once I finalize something, I create a .py file and then I have Windows Task Scheduler run it for me.
At some point I started to edit these .py files in Pycharm here and there, minor changes or tweaks. Now my original .ipynb files are a bit useless and out of date.
Is there anyway to keep these files synced? Or just a best practice that I should file? Right now I don't really know what is changed, so I am literally going to copy the .py files and paste them into the .ipynb files and overwrite it.
Here is the answer to this question in another tread:
Synchronizing code between jupyter/iPython notebook script and class methods
To cut it short you need to use reload function from importlib module:
import babs_visualizations
from importlib import reload
reload(babs_visualizations)
Just a little addition: make sure that you are addressing the function in the form of moldule.function. If you previously imported function by from module import funciton and then reloaded the module the function will not be reloaded!
Cheers.