Modern Python Projects Transcripts
Chapter: Let's build an app
Lecture: Create a Mac app with PyInstaller
0:00 Since I'm using a Mac. Let's use the pyinstaller to Create a Mac application first. Later,
0:08 I will switch the windows and we will do the same for a Windows application.
0:13 First, make sure you have created and activated the virtual environment and that you have run. Python setup.py install.
0:22 I have done this in the previous lessons, so I don't have to do this, but there is no harm in running it again. Next, we have to install pyinstaller.
0:30 The safest bet is to Install it in the same virtual environment where we keep
0:35 our application. So inside the virtual environment run pip install pyinstaller. If you go to the pyinstaller documentation,
0:52 you will see that the basic usage is quite simple. You just run pyinstaller command and you provide the path to your main script.
1:01 So, in our case, it's the gui.py file. So let's try this. You will see a lot of output and it's probably going
1:19 to fail. That's because we are using pyenv and pyenv by default doesnt install some development packages that pyinstaller requires,
1:30 but luckily we have this helpful message saying that we should rebuild our Python with enable shared on Linux or enable framework option on Mac OS.
1:41 So we have to either install one of the existing Python versions or install a new one. Using this enable framework option,
1:50 I've decided to install a new one. And since I already have the 3.90 and 3.86 I will insult the 3.85, to install
2:16 a Python version using pyenv with this enable framework we have to run command like
2:21 that. This will set the Python configure options for the current command to also include the enable framework command. So let's give it a try.
2:35 Remember, if you're on linux, just replace enable framework with enable-shared. Now that we have this new Python version,
2:44 we have to switch to using it and recreate our virtual environment and reinstall all the packages again. Let's run pyinstaller again.
3:17 This time it should work, no errors, That's a good sign. So now if we look inside our current folder, you will see that we have two new folders.
3:32 We have the build folder where all the building happens and we have the disk folder
3:37 that contains our application. pyinstaller has also created this gui.spec file where we can specify some options for building,
3:47 but we're not going to touch it, first let's go inside the build folder.
3:56 There is a bunch of files that pyinstaller is using when building your application. But one file here is actually quite important.
4:04 There is this warn-gui.txt file that contains all the warnings from the build
4:10 process. In our case, we got a bunch of warnings that some optional dependencies are missing. Hopefully, it won't cause any problems.
4:21 But if in the future you run into some problems and you can't build your application
4:25 this is the first file, that you should check before you start debugging.
4:31 Let's go back, inside that dist folder we can find the final product of pyinstaller our Python application. It's simply called gui.
4:49 Let's try to open it from Finder. We can find it on this list because it's one of a few files in this folder that contains the terminal icon.
5:11 It takes a few seconds, but it seems to be working. Let's test it with some URLs. Cool, so it seems to be working fine.
5:33 One thing that I don't like by default is that we have all those files here
5:37 in this folder, that we have to include with our application. So we can rerun pyinstaller with the --onefile option,
5:46 and it will pack all those files together into one file. Let me first remove this dist and build folders.
6:00 And also the gui.spec, wrong folder. dist. Okay.
6:30 And now, inside dist folder, we have a single gui binary that contains all the dependencies. Let's give it a try. oops without the dot,
6:55 actually, with the .com. Now it's working, so we can run this simple program on our computer, But we could actually do this even before,
7:06 So, let's test it on a brand new Mac OS operating system that shouldn't even have latest Python installed.