Best way to make GUIs and Executables in Python for Windows, Mac and Linux

Typically when making a program, I want users to have a familiar experience: a single .EXE file instead of some weird scripts that requires other programs to run. There are a few different ways of accomplishing this using python,. On this page I’ll explain the simplest way I’ve found to get this result, and how to debug if you run into problems with this method.

I wrote about using pySimeplGui for making dead simple GUI interfaces for python code before, and even have a simple example project,. Check those two articles out for that aspect.

As for creating the .EXE file, there are several packages that will allow you to create executables of your python scripts. The reason to do this is that you don’t want end users to have to install python  (simplified usage of your app) or you don’t want anyone seeing the actual python code. The way they work is that they create a self-extracting zip file that has your code, all associated libraries (including DLLs), and most crucially, they have a small bootloader that is a python interpreter executable. There are several available such as cx_Freeze, bbFreeze, Py2App, Py2Exe, Pyinsaller, or you can create your own from scratch  (See a chart comparing them here).

There are some problems with these tools. Several of these are not currently updated. Some don’t allow you to create a “One File” solution, meaning they require additional folders of files, etc. which in my view isn’t a true executable experience from the user perspective.  For some reason, there’s a known issue with some of these which makes the resulting executable get incorrectly flagged as a virus by virus software. There are some ways around this however.

I’ve tried out a few of these and given my recent experience with PySimuleGui, and the features I wanted, I settled on pyinstaller.

PysimpleGUI has its own interface for pysintaller to simplify things called pysinstaller-exemaker.

For some reason, my resulting executables kept getting flagged with false virus warnings on windows. I tested in Virus Total which runs multiple virus scanners at once on your file. Sure enough, lots came back with false positives.

I found two ways to fix this problem. As with most things, there’s the easy way, and then there’s the right way.

The easy way requires specific versions of python and pyinstaller on your machine.

The other way to prevent false positive virus scans is to recompile pyinstaller’s bootloader from scratch. While I did this (and explain it below), it is non-trivial.

The Easy Way: Install Correct Versions:

The easiest way to not have the virus issue is to use the correct versions of pyhton and pyinstaller as described on a no-dead link on yuriss.com.  This Completely solved the problem and is SOOO simple!  Basically you just install the correct version of python (Version 3.7.4) from python.org. If getting the windows version, make sure it is the 64-bit version, other users seem to have had issues with the 32-bit version.

Once this is installed, you can use pip on the command line to install Pyinstaller 3.4:

 pip install pyinstaller==3.4

The Right Way: Recompiling pyinstaller’s bootloader from scratch

This may be needed for future version of python or pyinstaller. Since I already went through the headache of figuring out how to do it, I’m documenting it here.

If using a version of pyinstaller that throws the virus warnings, you can’t just go to the folder PIP installed it to and compile it there. That’d be too easy of course! (This took me a long time to figure out)… If you try to do that, it’ll fail with cryptic errors.  The reason is that the file path is too long.

To overcome this, you have to perform the following steps:

  1. Clone pyinstaller’s source to a folder in your C drive, then
  2. Rebuild the bootloaders,
  3. Install pyinstaller with pip,
  4. Overwrite your pip installation with your newly built bootloader files…

Duh, obvious right?! (omgwtfwhyisthisalwayssohard!?)

Details for the steps are below:

Step 1: Open a powershell as admin and go to C:\\

cd c:\\

Download the source of pyinstaller. This will create its own folder for pyinstall:
>pre>git clone https://github.com/pyinstaller/pyinstaller

Step 2: Cd into the bootloader’s build folder:

cd .\\PyInstaller\\bootloader\\

Then run the script to reinstall. If you get an error telling you there’s no such thing as ./waf then you are in the wrong folder.

python ./waf all

Step 3: Once this is done, you can go back to vscode or wherever and install pyinstaller from pip

pip install pyinstaller

Step 4: Then navigate to python’s site packages, and copy the newly built bootloaders into the appropriate place. NOTE: PyInstaller is case sensitive here, so be careful. We’ll first make a backup of the original bootloader folder:

 mv C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller.bak

Then copy in our freshly compiled bootloader:

mv C:\\PyInstaller\\bootloader C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller

Note: Now to use pysimplegui-exemaker, you must edit your computer’s %PATH environment variable to locate pyinstall. Click the windows key on the keyboard, type “environment” and click to open the first suggestion. This window will pop up that has a button towards the bottom named “Environment Variables” that you must click. In the top window pane, find “path” or “PATH” and doubleclick that line to edit it. You’ll now be able to enter a new value. You want to add the path to where pip installs its scripts.  In my case, I pasted in the following:

 C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\Scripts

Then test it out:

python -m pysimplegui-exemaker.pysimplegui-exemaker #<--If you use pysimplegui's exemaker (NOT a typo, must exactly this way) OR

pyinstall --onefile ./myProgram.py #<-- If you just use pyinstaller directly

Tada! No more virus warnings!!!!

 

Check out my other post about how to replace the default python icon in your executable files.

Turn an old Computer into a Local Fileserver (Mostly Graphical Setup)

We’re running out of space. With photos, astrophotography files, and music recordings and work and just life stuff; we are running low on hard drive space. Over the years we’ve bought a few external USB hard drives but even those aren’t manages well and being portable get moved around a lot. That’s not the safest way to handle hard drives. Unless they are SSDs, they can be damaged by moving them around and such. We needed a better solution. I prefer using GUIs to set things up but will for through the terminal if I have to… I just haven’t used my unix fileserver skills since I worked in IT and my linux is a bit rusty as well.

Googling around you’ll find a ton about setting up a NAS or RAID5 or somesuch, but that’s overkill. I’m not looking to spend any money with this, otherwise I’d just have bought yet another portable drive. I already have a bunch of hard drives full of stuff (and honestly, there are a LOT of duplicates of each file taking up tons of space from the crappy way I store my backups). I want to use those drives and be able to clean out the duplicates to gain space. The first step is to build a basic file server.

I had a linux machine (my workhorse during grad school) that wasn’t being used, and  bunch of old hard drives that I wanted to use/access  It was a variety of drives from old PCs (back to win XP actually, and even older). I had a few external drives that broke so I just plopped them into this machine as well. I didn’t want to format them for a RAID or anything as they were all different sizes and I had data like pics and projects and work stuff from ages ago on them I wanted to keep. The best solution for me was the JBOD (just a bunch of drives) style server. All the NAS examples looked annoying to setup and I’d easily forget what I had done to set them up so JBOD it was.  I couldn’t find any single document explaining how to do all the steps I wanted so I spent a couple of weeks failing in my spare time… breaking and unbreaking my linux fileserver and documenting the working parts here for when I need to work on it again. I prefer doing things with GUI if possible, but I’ll do some things using the terminal because you just have to but I tried to keep it to a minimum.

Installing Linux:

If you don’t already have linux running on this computer, I suggest you install it. It sounds hard, but it is super easy to do actually. Just download any flavor of ubuntu you like (lubuntu, kubuntu, linux mint, cinnamon, etc.) I recommend the versions that say “LTS” as those are supported for 2 years or so. There’s a lot of options here.

Install linux without formatting hard drive or losing data:

  1. Download Wubi. This will let you install linux without killing your windows files or messing anything up. you can always uninstall linux later if you want using this method.

Start clean on either blank hard drive or format an old drive:

  1. Download ubuntu flavor you prefer
  2. Format a USB flash drive that’s at least 2GB (lubutnu and xubtunu) or 4GB (all other flavors).
  3. Use Balena Etcher to burn this ISO file to the USB drive.
  4. Once finished, you can reboot your computer.
  5. Enter the boot menu when you see your computer’s startup logo (before windows logo) by pressing either F10, F11, F12, or the delete key. Different manufacturers have different keys they choose for this.
  6. Select to boot off the USB drive.
  7. Doubleclick the icon on the desktop to install *buntu. Follow the menus or youtube videos on how to install it.
  8. Remove the USB drive and reboot when done.

I was already running ubuntu so I just needed to put all the drives in the computer case. I could have 2 drives that were IDE on the same ribbon cable, and 4 SATA drives on the motherboard of my old linux box. Sadly, that’s all this mobo could accommodate. I see spots for 4 more SATA drive on the PCB but they aren’t populated. I filled all 6 spots with drives, making sure that all the drives were set up as slaves except the bootable one (see another post about how I set that up).  I also had a TON of USB ports on this motherboard as well (literally 6 on the back, 2 on the front and I added 2 more to a breakout cable inside for a total of 10 USB ports). These can add more portable drives and other drives with some of  these awesome USB to IDE/SATA drive cables I have to the server. As I loaded these drives in the bays I made a note as to their serial numbers and capacities. I wrote it on paper, but I also used a sharpie and wrote it on the chassis so I could keep track of what is what and where.

When I turned the machine on, I went into the BIOS and set up the boot order for the hard drives so that my /filesystem always boots first in order of hard drives.  When booting the linux machine, hold the Delete key to enter BIOS. Then go to Advanced BIOS Features.

bios1

Next select “Hard Disk Priority”. Here’s what that looks like:

bios2

Finally, use the arrow keys to select a particular hard drive (the one you want to boot from) and then use the + key to increase its priority until it is number 1:

bios3

Once this is done, Hit F10 to save and exit BIOS. It will reboot the machine into linux as it usually did.

Accessing all the Hard Drives:
Open your ubuntu menu and search for the “disks”  program. This should be part of gnome-system-tools, which is a standard program in ubuntu. If you don’t have it then open your package manager and search for it. Mark it for installation and click the “Apply” button. or open a terminal and enter the following:

sudo apt-get install gnome-system-tools

You can copy that line of text paste it into the terminal using ctrl+shift+v to paste in a terminal.

All your hard drives should be visible in the Disks app. It lays out all the gory details. Serial numbers, model numbers, etc. Click each drive in the left and note what the “mount point” is for them. I have my Filesystem (this is where I installed the linux operating system)  and /home mount points on separate drives. /home is like the user folder in Windows with a folders for Desktop folder, Documents folder, etc. Separating this from the operating system installation file tree allows me to reinstall linux without breaking my user’s documents and settings and Desktop items, etc. I have another post on that and it has served me very well over the years.

For my other drives, they have mount points that start with /mnt  and end in a weird code.  That code is the UUID (hardware ID) for that hard drive and it isn’t very descriptive for humans.  Let’s change that to something helpful. For instance I changed my 1TB drive’s mount point to /mnt/_old_1TB_MyBook

image

Once all the drives have normal names click the link to one of them and it’ll take you to the folder in your file browser. Go up one directory to see all the hard drives. You’ll note the old folders with the UUIDs in there as well as any old names you had mounted in the past, but they are empty. Only the mount points you named in Disks are connected to your hard drives. You can just delete the other empty old mount points. You have to be root to do so. I used the terminal and the command sudo rmdir <folder name> to delete the ones I was CERTAIN were empty old mount points.

All we’ve done here is essentially modify the /etc/fstab file which contains drive mount info. Lots of other tutorials out there can show you how to do this in the terminal if you want to know how.

Now we need to install all the other software we’ll need for the other steps of setting up the server. in your start menu, search for your package manager software. Mine is “Synaptic package manager” and install the following (make sure you check the box next to these entries, select “Mark for Installation” and when all are selected, click the “Apply” button.) Don’t be surprised if some of these are already selected, just ignore them then:

gnome-system-tools

samba

samba-common

python-glade2

system-config-samba

Or you could open a terminal and paste the following which will install them all for you automatically

sudo apt-get install gnome-system-tools samba samba-common python-glade2  system-config-samba

Once these all install you will create your linux users who can access this machine. You need linux users to be able to create Samba users. Samba is the file server software. From your start menu button, search for the app “Users and Groups.” Add whoever needs users for the fileserver.

linuxUsers

Now you can start fiddling with Samba, which is the actual fileserver software. Since there’s no icon to select to load this app, you need to open a terminal (hit ctrl+alt+t) type or paste:

sudo -H system-config-samba

You can copy that line of text paste it into the terminal using ctrl+shift+v to paste in a terminal.

You have to enter your password, but then a window should pop up. The very first time I did this I got the following error:

SystemError: could not open configuration file `/etc/libuser.conf': No such file or directory

ok, so the file doesn’t exist… I’ll simply make a blank file there and try again.

sudo touch /etc/libuser.conf #creates a blank file named libuser.conf in /etc folder
sudo -H system-config-samba

Success! It opened!

samba server config

Now we need to figure out which folders we want to share and how they are set up in linux.

As we saw before, the internal hard drives all have mount points in the /mnt folder. So let’s create a share for that. Click the big plus sign button and make yours match mine below. Be sure to click the “Access” tab and make it open for everyone.

image

Do the same kind of thing for the /media folder. This is where any USB storage devices will mount to by default.

And finally, I have a TON of files on my old desktop and user folder, so I want to make the entire /home folder accessible as well. You have to be careful with this though because if you allow all users (and even guests) edit access to your home folder, they can delete stuff, and snoop to their hearts’ content. Any sensitive data should be locked so that users are required to log in and can only access their files. If for example you wanted each linux user we created earlier to access only their home folders, you’d set that up as /home/adam then you could set the access rights to only allow user “adam” to view it.

Once you’ve set this up, now you must create the Samba users. While in the same application, select Preferences –> Samba users. Here you will add a new user with the same name as each linux user. You will select each user from the “Unix user” dropdown box (this is why we had to create all the users before). You can have the same or different passwords than your linux login. Once you added everyone, close this app. The steps we just took replace the manual process of editing a configuration file for Samba. This is a plain text file stores in /etc/samba called smb.conf and this is usually what most tutorials will do, show you how to edit this file manually. I didn’t have luck with that. Also, you might see tutorials using a tool called gadmin-samba however when I used that it completely hosed up my system hardcore. I had to purge all samba stuff, delete all the files and start from scratch several times to fix the issues.

File/Folder Access Rights:

The final step here that is required is to set the access rights of each folder you want to share. This is important because unless we change these permissions, we will end up only being able to VIEW files and folders on the server but we can’t create new files, paste, move, or delete anything yet. To fix this we need to change the permissions on the folders we set up to share.

We need to open the folders as root or administrator. Depending on the file manager software you might have different ways to do this, but in mine you simple right click ANY folder and choose “open as Administrator” or “Open as root” then I can simply navigate to the filepath “/” If yours doesn’t offer this, then open a terminal and type in the name of your file manager but call it from sudo like one of the following lines:

sudo nautilus /
#or
sudo nemo /
#or
sudo caja /

Right click on the folder of interest, for example “/mnt” and view the Properties. Click the Permissions” tab and make sure that they all show the ability to “Create and delete files.”

But if you’re using the terminal anyway, you can simply use the chmod commands. Here’s the best explanation of how to use it I’ve ever seen. You can make /mnt and /media fully writable like this:

sudo chmod 777 –R /mnt
sudo chmod 777 –R /media

Or you can give others the ability to look at files in your home user folder, but not create or delete like this:

sudo chmod 755 –R /home/adam

And you can block others from your folders and have them only viewable and editable by you like so:

sudo chmod 700 –R /home/adam

On the Windows machine:

Now your fileserver is set up you need to access it. Go on your windows machine and open a file browser window. Right-click on “This PC” and select “Add a Network Location”  Click Next until it asks for the location. Click “Browse” and wait a minute for it to search your network. You should see your fileServer’s name pop up in the list. Click literally any folder under the fileServer and select Next.

fileserver List

Shorten the terribly long name it gives you (I just delete the highlighted part) and you’re done.

name1

It’ll pull in the other accessible folders on that drive as well. Now you should be able to access and edit anything on those folders on the fileserver. To get to this again simply choose the fileserver option in the left-pane of the windows file explorer. If this doesn’t appear, then make sure your server is turned on and is on the same network as your windows machine and repeat the instruction in this section of the post.

remote

The first time you visit something in here it might ask you for a username and password. Just enter one of the Samba username and password combos we created earlier. You can make it save these credentials and never ask you again if you’d like.

Cherry on top – Remote Shutdown of server:

If you don’t want to connect a keyboard, mouse and monitor to the screen just to shutdown the fileserver, you can do so remotely by installing and using SSH. SSH is a way to securely tunnel into the fileserver’s terminal remotely. That way you can send it the standard linux shutdown message from your windows PC when you are done with it for the day.

Open synaptic and you should select the following:

or run the following command:

sudo systemctl status ssh

This will install the needed files for SSH. Next, we need to let the firewall built into linux know you want to allow ssh. In a terminal window run the following:

sudo ufw allow ssh

Now you can go to the windows computer and use putty.

Putty1

Enter your data here (keep the port as 22) and when you click “open” a black command window will pop up asking you who you want to login as. Type your linux username and hit enter and it’ll ask for a password. Now you’re commanding the terminal of the linux machine from windows. You can do literally anything the terminal can handle. You can’t do anything that requires GUIs though this way. That’s fine though because this is enough to send the reboot or shutdown signal. The shutdown takes about a minute before it actually shuts down, so don’ t think it isn’t doing anything, just give it some time.

sudo shutdown
#or
sudo reboot

Remote Desktop:

If you want a graphical environment for controlling your server, there’s 2 options. One is to simply remote Desktop into it.  There are many ways to do this, but the easiest is to simply install and enable the xrdp app on the linux machine and use the built-in RDP app in windows to connect.

Open synaptic package manager and search for xrdp and xorgxrdp.  Mark it for installation and click Apply.

Once this is done, go to the in the App search, look for “System Settings

Go to Sharing and make sure Remote Login is ON.

image

Next, in system settings, click “Network” in the left column and then click the gear button next to your wired (or wireless) connection. This will give you your IP address. It should look similar to your router’s address. Mine’s 192.168.x.x. You’ll need this to connect in windows.

To do all this in the terminal on the linux machine (or through SSH) run the following:

sudo apt install xrdp
sudo systemctl enable xrdp
ifconfig

This last command, ifconfig, will give you a screen showing you the local IP address of the machine. It should match closely to your router’s address. Mine is 192.168.x.x so I look for something that matches that. You’ll use this on the windows machine.

Now on the windows machine, click the start button and type “RDP” and hit enter. Type in the IP address of the linux machine.

I found an easier way. Simply right click on the fileserver in your explorer folder and select “Remote into this machine”

image

When you click connect, you’ll be able to login. I had a little trouble with this connecting. I tried all the selections in the dropdown menu a couple of times and on the 2nd or 3rd try it worked. Once I was logged in I got an error asking me to login to create a color device over and over. After entering my password 3 times I just canceled it and the window stopped popping up. I haven’t done this in like 15 years, but I don’t remember the experience being so crappy. I can access the desktop, but it is displaying a GNOME environment instead of the Mate environment I have installed on the linux machine. Nonetheless I can get do things graphically without issue.

Other Thoughts:

The way I’ve done it is quite insecure. I’m allowing Guests on the network access to create and destroy all the shared files which is kind of dumb… I’ll live with it though because I will only turn this fileserver on if I am immediately accessing the files on it, then immediately turning it off again.  In that short time I hope I don’t get hacked…

Storing all your files in one location is not the best for preservation. You can make a personal cloud like Google Drive, Dropbox, Box, OneDrive, Apple Cloud, etc. by using the Unison program.  It essentially synchronized two folders. These can be on the same computer, one on a portable USB drive, or even across the network. Anything added, modified, or deleted in one folder is automatically synched to the other in both directions.

Windows Networks Issues:

If the network disappeared from the “Networks” thing in Windows 10 but still somehow appears in the sidebar and is still accessible, no prob. Follow these instructions: https://www.wintips.org/fix-windows-10-network-computers-not-showing/#method-5

File Manager Share plugins:

Previously, I had some luck with using my file-brower’s “share” plugin. Nautilus, Nemo, and Caja all have their respective “share” app you can install in apt-get or synaptic eg “caja-share“.  This will allow you to simply right-click on a folder and select “share settings” where you can set this up yourself. The problem with this is that this info is NOT stored in the basic /etc/samba/smb.conf configuration file. In fact, it is nowhere to be found anywhere in that folder.  This is because this plugin stores this info in the following path:

  /var/lib/samba/usershares

To create the Samba users and set up their SAMBA passwords (these can and maybe *should* be different than their linux user passwords)

sudo smbpasswd –a user1
sudo smbpasswd –a user2

Now enable the users

sudo smbpasswd –e user1
sudo smbpasswd –e user2

Check the status of the samba server. You should see “active” somewhere. If not, you hosed it somehow. Tips on how to fix this at the end…

sudo systemctl status smbd

Restarting samba (after every big change to users, folders, etc you should do this. Or you can reboot the machine, which is foolproof method of restarting the server the right way)

sudo systemctl restart smbd
sudo systemctl restart nmbd

or do it directly for smbd and nbmd

sudo smbd stop
sudo nmbd stop
sudo smbd start
sudo nmbd start

Doctoral Dissertation or Master’s Thesis Template

I’ve already mentioned one good tool for research papers, so here’s another that might be helpful.

Most schools have a set format for their dissertations and theses. My school was the same.  They provide a manual showing what they expect your formatting to be.  Many people have trouble with this. You must make a meeting with a specific faculty member in the graduate school and she will take a ruler and measure your margins, and go over in extreme detail the spacing, numbering, etc. of your document. Many many people get rejected and I’ve even talked to people who pay lots of money for other people to format the documents for them.  They don’t provide an exact template for any specific software, but using the google machine can help you find Microsoft Word or LaTeX tempaltes.  Personally, I like LibreOffice it is completely cross-platform (works on windows, mac, and linux systems)  completely free, open source, has lots of great plugins and it lives in the world between Microsoft Windows and LaTeX. It is GUI-based (WYSIWYG, visual) like Microsoft Word, but also much more powerful like LaTeX.   I dislike LaTeX because it is incredibly buggy and you have to program your text documents.  I program other stuff all day long, I am sick of that!

Anyway, I made my own template and write up a very detailed explanation on how to use it and my best tips and tricks for modifying the basic template if you want.  I also have a chart of suggested plugins to make your papers look amazingly good and professional. Again, I used LibreOffice, so it can be used in OpenOffice as well. This is specific to my school, so be sure you double check your school’s documents to see exactly what kind of margins and page numbering they require.  (Note it is in Open document Format and though I haven’t tested it, it *might* work in other programs like Microsoft Word).

Download my template here!

Adam-Atom

Easily get Public Link for Dropbox Files in Linux

I got tired of going to the dropbox website to find the public link for files I put in the Public folder.  On Windows you can simply right click–>get Public link, but I’m running LinuxMint with Dropbox version 2.0.22 which doesn’t support this (among many other things like Pausing a sync…)

Anyway I wrote a script that you can just drag your files onto and it’ll pop up a box showing you the public link.  In Linux, a script alone can’t do this, but a script and a .desktop file can call a script to do it.  Here’s what I got:

Save the following in a text file called “getPublicLink.sh” inside your Dropbox/Public folder.  Make sure it has permission to run (right-click the file–> properties –>permission and check the box to allow it to run)

#!/bin/bash
publicLink=$(dropbox puburl $1)
zenity --info --text $publicLink

Now create a new text file named “Get Public Dropbox Link”.  Paste the following there, be sure to change your username, mine says “adam” yours does not and this will make it not work. Save it in the Dropbox/Public folder and give it permission to execute as well. Here’s the file:

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/home/adam/Dropbox/Public/getPublicLink.sh
Name=My Application
Icon=/usr/share/icons/hicolor/48x48/status/aptdaemon-update-cache.png
#Required for double-click running and Drag-andDrop files into this icon.
#reference: http://linuxcritic.wordpress.com/2010/04/07/anatomy-of-a-desktop-file/
Name[en_US]=Get Public Dropbox Link

 

Now you can  simply drag files from your public folder on top of the “Get Public Dropbox Link” file and a window will pop up showing you the public link. from here, simply copy and paste that link to where ever you need.

Information_903

 

Adam-Atom

A Cheap but Quality Home Surveillance System

Today as I got into my car I realized that someone had broken into it and ransacked my glovebox and center console. I couldn’t nail down exactly when it happened but I couldn’t help but feel violated.  Someone else in my neighborhood also had a break in. I decided to do something about it. I immediately went to the closest store and grabbed a cheap webcam and prepared to make a motion triggered surveillance system. (Man, I love Linux).

Read more