{"id":5503,"date":"2022-11-09T05:02:12","date_gmt":"2022-11-09T05:02:12","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5503"},"modified":"2022-11-09T05:22:43","modified_gmt":"2022-11-09T05:22:43","slug":"pyqt-to-exe","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-to-exe\/","title":{"rendered":"PyQt to EXE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to convert a PyQt application to an executable program (EXE) on Windows<\/p>\n\n\n\n<p>We&#8217;ll convert the <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qstatusbar\/\">Editor program<\/a> to an executable file on Windows using PyInstaller. The following shows the directory structure of the Editor program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">\u251c\u2500\u2500 assets\n|  \u251c\u2500\u2500 editor.png\n|  \u251c\u2500\u2500 exit.png\n|  \u251c\u2500\u2500 new.png\n|  \u251c\u2500\u2500 open.png\n|  \u251c\u2500\u2500 redo.png\n|  \u251c\u2500\u2500 save.png\n|  \u2514\u2500\u2500 undo.png\n\u2514\u2500\u2500 main.py\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>assets<\/code> directory stores all the images used by the program. <\/li>\n\n\n\n<li>The <code>main.py<\/code> stores the program source code.<\/li>\n<\/ul>\n\n\n\n<p>First, activate the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-virtual-environments\/\">virtual environment<\/a> where the PyQt program runs.  <\/p>\n\n\n\n<p class=\"note\">Note that if you don&#8217;t use a virtual environment, PyInstaller may create an output with a big size that include all packages installed.<\/p>\n\n\n\n<p>Second, use the following <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-pip\/\">pip<\/a><\/code> command to install PyInstaller:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">pip install pyinstaller<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PyInstall has a lot of options that are listed <a href=\"https:\/\/pyinstaller.org\/en\/stable\/usage.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">on this page<\/a>. However, in this tutorial, we&#8217;ll show you the most commonly used ones.<\/p>\n\n\n\n<p>Third, use the following <code>pyinstaller<\/code> command to convert the Editor program to an executable file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">pyinstaller main.py --noconsole --add-data \"assets;assets\" --icon=\"assets\/editor.png\" --name editor --noconfirm<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once you run this command successfully, you&#8217;ll see that PyInstaller creates new directories and files including <code>build<\/code>, <code>dist<\/code>, and <code>editor.spec<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">\u251c\u2500\u2500 assets\n|  \u251c\u2500\u2500 editor.png\n|  \u251c\u2500\u2500 exit.png\n|  \u251c\u2500\u2500 new.png\n|  \u251c\u2500\u2500 open.png\n|  \u251c\u2500\u2500 redo.png\n|  \u251c\u2500\u2500 save.png\n|  \u2514\u2500\u2500 undo.png\n\u251c\u2500\u2500 build\n|  \u2514\u2500\u2500 editor\n\u251c\u2500\u2500 dist\n|  \u2514\u2500\u2500 editor\n\u251c\u2500\u2500 editor.spec\n\u2514\u2500\u2500 main.py\n\ndirectory: 5 file: 9<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Inside the <code>dist\/editor<\/code> directory, you&#8217;ll find the <code>editor.exe<\/code> file. To launch the program, you can double-click it:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"496\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-to-EXE.png\" alt=\"PyQt to EXE\" class=\"wp-image-5506\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-to-EXE.png 752w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-to-EXE-300x198.png 300w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/figure>\n\n\n\n<p>How the <code>pyinstaller<\/code> command works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--noconsole<\/code> option hides the console window when the program launches.<\/li>\n\n\n\n<li><code>--add-data \"assets;assets\"<\/code> option copies the <code>assets<\/code> directory to the <code>dist\/editor<\/code> directory so that the program can reference the images and display them correctly. The format of the <code>--add-data<\/code> option is <code>SRC;DEST<\/code>. If you want to copy multiple directories, you can use multiple <code>--add-data<\/code> options.<\/li>\n\n\n\n<li><code>--icon=\"assets\/editor.png\"<\/code> option specifies the icon for the main window.<\/li>\n\n\n\n<li><code>--name editor<\/code> option assigns a name to the program i.e., <code>editor<\/code>. If you ignore this option, PyInstaller will use the Python file as the program&#8217;s name e.g., <code>main<\/code>.<\/li>\n\n\n\n<li><code>--noconfirm<\/code> option will remove the existing <code>build<\/code> and <code>dist<\/code> directories without confirmation if you execute the command again.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Pyinstaller to convert a PyQt program to an executable file.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5503\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-to-exe\/\"\n\t\t\t\tdata-post-title=\"PyQt to EXE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5503\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-to-exe\/\"\n\t\t\t\tdata-post-title=\"PyQt to EXE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>in this tutorial, you&#8217;ll learn how to convert a PyQt application to an executable program on windows<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":38,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5503","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5503"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5503\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}