[Tut] Python to .exe – How to Make a Python Script Executable? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python to .exe – How to Make a Python Script Executable? (/thread-100863.html) |
[Tut] Python to .exe – How to Make a Python Script Executable? - xSicKxBot - 03-13-2023 Python to .exe – How to Make a Python Script Executable? <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"1202016","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","starsonly":"","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </p></div> <p>I have a confession to make. I use Windows for coding Python. </p> <figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="500" height="225" src="https://blog.finxter.com/wp-content/uploads/2023/03/image.gif" alt="" class="wp-image-1202025"/></figure> <p>This means that I often need to run my practical coding projects as Windows <code>.exe</code> files, especially if I work with non-technical clients that don’t know how to run a Python file.</p> <p>In this tutorial, I’ll share my learnings on making a Python file executable and converting them to an <code>.exe</code> so that they can be run by double-click.</p> <h2>PyInstaller</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="163" height="165" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-140.png" alt="" class="wp-image-1202047"/></figure> </div> <p>To make a Python script executable as a <code>.exe</code> file on Windows, use a tool like <code><a rel="noreferrer noopener" href="https://pyinstaller.org/en/stable/" data-type="URL" data-id="https://pyinstaller.org/en/stable/" target="_blank">pyinstaller</a></code>. PyInstaller runs on Windows 8 and newer.</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Pyinstaller</strong> is a popular package that bundles a Python application and its dependencies into a single package, including an <code>.exe</code> file that can be run on Windows without requiring a Python installation.</p> <p>Here are the general steps to create an executable file from your Python script using Pyinstaller:</p> <ol> <li>Install Pyinstaller by opening a command prompt and running the command: <code>pip install pyinstaller</code> or <code>pip3 install pyinstaller</code> depending on your <a href="https://blog.finxter.com/how-to-check-your-python-version/" data-type="post" data-id="1371" target="_blank" rel="noreferrer noopener">Python version</a>.</li> <li>Navigate to the directory where your Python script is located in the command prompt using <code>cd</code> (command line) or <code>ls</code> (PowerShell).</li> <li>Run the command: <code>pyinstaller --onefile your_script_name.py</code>. This command creates a single executable file of your Python script with all its dependencies included.</li> <li>After the command completes, you can find the executable file in a subdirectory called <code>dist</code>.</li> <li>You can now distribute the executable file to users, who can run it on their Windows machines by double-clicking the <code>.exe</code> file.</li> </ol> <h2>What Does the –onefile Option Mean?</h2> <p>The <code>--onefile</code> file specifier is an option for Pyinstaller that tells it to package your Python script and all its dependencies into a single executable file. </p> <p>By default, Pyinstaller will create a directory called <code>dist</code> that contains your script and a set of related files that it needs to run. However, using the <code>--onefile</code> option, Pyinstaller will generate a single <code>.exe</code> file, which is more convenient for the distribution and deployment of the application.</p> <h2>1-Paragraph Summary</h2> <p class="has-global-color-8-background-color has-background">To convert a Python file <code>my_script.py</code> to an executable <code>my_script.exe</code> using Pyinstaller, install Pyinstaller using <code>pip install pyinstaller</code>, navigate to the script directory in the command prompt, run <code>pyinstaller --onefile my_script.py</code>, then locate the executable file in the <code>dist</code> folder.</p> <p>If you want to keep improving your coding skills, check out our free Python cheat sheets!</p> </div> https://www.sickgaming.net/blog/2023/03/12/python-to-exe-how-to-make-a-python-script-executable/ |