pyqt gui application packaging for ubuntu - python

I am really new to packaging.
I have developed a music player using pyqt in ubuntu.
It has a gui and it uses sqlite database.
I have looked at distutil.
What I understood is how to place modules and scripts in right place.
What I don't understand is how to set paths for database, config files & log files.
How do I achieve it the way other applications do it in ubuntu by maintainig all this data in '.application_name' folder under home for a user ?
Can anyone suggest a good example application to learn from or point in some direction?

You can use QDir.home() to get the absolute path to a user's home path. You can use this path when generating/accessing your database, config files and log files. For example, on first startup you can do something like:
filePath = QDir.home() + "/.application_name"
if not QDir.exists(filepath)
QDir.mkdir(filepath)
Then you can use filePath when reading/writing to the files from there on out.

Related

How to bundle Python apps with asset dependencies for end users?

I have a Tkinter app that uses images included in the same folder as the .py file. pyinstaller script.py produces an executable that runs but does not open any windows. This is because it is looking for images that don't exist in the same subdirectory. When I copy the important images to the dist folder Pyinstaller creates, the application runs correctly.
However, I would like to have a single executable that I can share with other users that doesn't also require them to have the images stored. The images should be bundled with the software somehow, like how commercial software (usually) doesn't require you to download assets separately from the program itself.
Is there a way to bundle Python programs and the assets they use into single-click applications?
Note that I am using Python 3 on Linux Mint. I am also something of a novice, so don't be surprised if I'm missing something obvious here.
It appears I've solved my own problem.
Instead of having the images included in the same folder as main.py and using the resulting short relative filepath to reach them, install the images in an appropriate space in the system directory tree (I used /home/$USER$/.$PROGRAMNAME$/) and have the program access the files using the absolute path to that directory. This will allow you to copy the program anywhere on your computer you want and have it run without a problem.
However, if you want to share it with someone else, you'll need to also include an installation script that places the assets in the correct directory on their computer.

how to persist/store data in executable compiled with pyinstaller

I need to locally store some basic data the user inputs into my exe. The program was compiled using pyinstaller and was previously making use of the os open method of saving data to txt files. It is my understanding that macOS(my OS although cross-compatibility with Windows would be great) locks executables so that they may not make any file changes. The program currently does save the data, but when the executable is run again the old data is no longer accessible.
It seems obvious that apps store things locally all the time, how can data be persisted specifically within the python/pyinstaller combination?
Apologies if this is a simple question, it definitely seems simple but I can't find documentation for this anywhere.
You can use
os.path.expanduser('~user')
to get the user home directory in a cross-platform manner, see How to find the real user home directory using python?
Your application should have write permissions in the user home directory, so then you can let it create and modify a data file there following How to store Python application data.

Place Text Files in another location - selenium

Currently my program is using txt files (filled with data) that are located in the Desktop. I am going to be sending this out to users and the text files are going to be included in a installer. When installing I don't want these files to crowd the users desktop. Any ideas on this??
Kunwar, this is a somewhat subjective question, so this essentially is a subjective answer.
This is really about packaging. You've not provided any info on what your package lookslike, but in principle, why not just put the text files in the directory with your program?
your_program/inputs/*.txt
Then they will always be available to your tool. You can find the current location of your script within the script itself and build the path to the input file, no matter where the users have stored the script on their machines.

Single EXE to Install Python Standalone Executable for Easy Distribution

I used Pyinstaller to create a standalone exe file for a PyQt project. I did not use the single file option because the exe that is created with the single file option takes too long to open. Therefore, Pyinstaller created a dist folder that contains a bunch of files including my program.exe file. I would now like to create a setup.exe file that will place the contents of my dist folder in the program directory and create a shortcut in the start menu and on the desktop. I want it to be super simple for the user. Maybe like the setup.exe files that you get when you download a program from CNET. I found Inno-setup, which looks promising. However, I do not know if there any special considerations because the program is a standalone program create from a python/PyQt program. Thanks! Anyone have experience with this task? Is there a program everyone is using for this task that I do not know about?
Inno-Setup or NSIS are probably the easiest to use. You just tell them what files to include and where to put them and then they will create a nice installer for you. I wrote a short tutorial on my experiences using InnoSetup that you might find helpful:
http://www.blog.pythonlibrary.org/2008/08/27/packaging-wxpymail-for-distribution/
Note that this tutorial was based around a wxPython app I wrote, but the concepts are the same.

Where to store the configuration files of python applications on Windows

I have a python program that must work on Windows and Linux. There are some configuration options I normally store in a file, in a subdirectory of the program's directory.
For Windows, I converted it to exe and created an Installer for it. And now I have the problem of dealing with the config file.
What is the best place to save the configuration file? I have read that for Windows os.environ['APPDATA']+'myAppName' is the path that must be used. Is it correct? Is it standard? Will it work in all versions of Windows at least from XP (and at least in English and Spanish)?
PD: I am not interested in using ConfigParser. Config file is in my own format and I have working code for reading/writing from it.
Storing settings in the user directory is usually a good idea.
These days, you should probably use something like the appdirs library to find a good path to store your configuration in.
Under most Unices, just store a (preferably dot-prefixed) file in the home directory. Under OS X, you'd want to create a directory for your application in the user's Library folder, and store your files there. Under Windows, APPDATA is a good place to build a directory in for your application. It should work on all Windows localizations, and it looks like it was also available in Windows XP.
platformdirs
There is a better solution now ... better than speculating about what is best on each platform, better than environment variables which may or may not be defined, and even better than appdirs: platformdirs (GitHub, PyPI Snyk).
import platformdirs
appname = 'OurGreatApp'
print(f"User config files should be stored in {platformdirs.user_config_dir(appname)}")
On Linux, it is common to store the configuration file in the users home directory, for instance ~/.myprogramrc. On windows Vista and up, users have a home directory as well (/Users/username) and a would recommend storing your settings there in a subfolder (/Users/useranem/myprogram). Storing the settings in the application folder will generate UAC warnings.
On Windows XP, users do not have a home folder. Some programs make the choice of putting configuration in the 'My Documents' folder which I guess is as good a place as any.

Categories

Resources