{"id":182087,"date":"2024-03-13T06:00:00","date_gmt":"2024-03-13T05:00:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=182087"},"modified":"2026-02-06T08:25:34","modified_gmt":"2026-02-06T07:25:34","slug":"cx-freeze-compile-and-distribute-your-python-program","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/cx-freeze-compile-and-distribute-your-python-program","title":{"rendered":"Cx Freeze: compile and distribute your Python program"},"content":{"rendered":"\n.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}<p><strong>Cx Freeze is a Python program that transforms a script into a stand-alone executable on Linux, MacOS or Windows. Making a program executable, i.e. compiling the code, is important for easy distribution.<\/strong><\/p>\t\t\n\t\t<p><strong>Cx Freeze<\/strong> is the program that compiles the Python script and all its dependencies into an executable file.<\/p><p><em>So how does Cx Freeze work?<\/em><\/p><p><em>Why use it?<\/em><\/p><p><em>How do you install and use it?<\/em><\/p><p>Answers to your questions in this article.<\/p>\t\t\n\t\t\t<h3>How does Cx Freeze work?<\/h3>\t\t\n\t\t<p>A <strong>Python script<\/strong> depends on several modules. It needs the libraries and files of the Python system. Cx Freeze analyzes the script&#8217;s dependencies to create a stand-alone executable.<\/p><p>The name <strong>Cx Freeze<\/strong> is used because this program &#8220;freezes&#8221; the <a href=\"https:\/\/liora.io\/en\/python-the-most-popular-language\">Python script<\/a> and its dependencies in an executable file.<\/p><p>The program analyzes the dependencies to identify the imported Python modules, resources and external libraries required to run the file.<\/p><p>It then creates a virtual environment in which to copy the necessary files and modules.<\/p><p>Finally,<strong> Cx Freeze<\/strong> creates the stand-alone executable: no external Python installation is required.<\/p><p>&nbsp;<\/p><p>?Related articles:<\/p><table dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" data-sheets-root=\"1\"><colgroup><col width=\"656\"><\/colgroup><tbody><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Folium: Discover the open source Python library &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/folium-discover-the-open-source-python-library\"><a href=\"https:\/\/liora.io\/en\/folium-discover-the-open-source-python-library\" target=\"_blank\" rel=\"noopener\">Folium: Discover the open source Python library <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Matplotlib: Master Data Visualization in Python &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\"><a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\" target=\"_blank\" rel=\"noopener\">Matplotlib: Master Data Visualization in Python <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Crash Course: Get started &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\"><a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\" target=\"_blank\" rel=\"noopener\">Python Crash Course: Get started <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Mastering Machine Learning in Python: Data-Driven Success &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\"><a href=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\" target=\"_blank\" rel=\"noopener\">Mastering Machine Learning in Python: Data-Driven Success <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Programming for Beginners - Episode 3&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\"><a href=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\" target=\"_blank\" rel=\"noopener\">Python Programming for Beginners &#8211; Episode 3<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Django: All about the Python web development framework&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\"><a href=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\" target=\"_blank\" rel=\"noopener\">Django: All about the Python web development framework<\/a><\/td><\/tr><\/tbody><\/table>\t\t\n\t\t\t<h3>Why use it?<\/h3>\t\t\n\t\t<p>The <strong>Python program cx_Freeze<\/strong> offers several advantages (it exists for a reason!). Need to share a Python script? Your colleague or partner may find it tedious to install Python and all its dependencies.<\/p><p>The <strong>cx_Freeze program<\/strong> allows you to bypass this installation thanks to a single executable file that&#8217;s easy to distribute.<\/p><p>Another strong point is the confidentiality of the <strong>Python script.<\/strong> You don&#8217;t need to disclose the source code: by creating an executable, you simply share the file rather than your code.<\/p><p>Finally,<strong> cx_Freeze<\/strong> is easy for developers to use. Although you&#8217;ll need to test the executable to make sure it&#8217;s working properly, creating it is quick and intuitive.<\/p>\t\t\n\t\t\t\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=&#8221;.svg&#8221;]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/09\/cx_freeze.jpg\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Learn how to use cx_Freeze<\/a><\/div><\/div>\n\n\t\t\t<h3>How do I install and use Cx Freeze?<\/h3>\t\t\n\t\t<p>You can install the latest version of <strong>cx_Freeze using the pip command:<\/strong><\/p><pre style=\"padding-left: 80px\">pip install --upgrade cx_Freeze<\/pre><p>Some operating systems may require you to use <strong>Python3 and pip3<\/strong> rather than other versions.<\/p><p>You can also use the pipenv command to install and update cx_Freeze :<\/p><pre style=\"padding-left: 80px\">pipenv install cx_Freeze<br>pipenv update cx_Freeze<\/pre><p>If you&#8217;re using Anaconda, it&#8217;s best to install cx_Freeze via the conda command:<\/p><pre style=\"padding-left: 80px\">conda install -c conda-forge cx_freeze<\/pre><p>Once <strong>cx_Freeze<\/strong> has been installed, we need to create a python file to compile the program: we&#8217;ll call it setup.py. The code is as follows (modify the parameters if you wish to customize the executable):<\/p><p><strong>import sys<\/strong><br><strong>from cx_Freeze import setup, Executable<\/strong><\/p><p># Dependencies are automatically detected, but may need to be adjusted.<br>build_exe_options = {<br>&#8220;excludes&#8221;: [&#8220;tkinter&#8221;, &#8220;unittest&#8221;],<br>&#8220;zip_include_packages&#8221;: [&#8220;encodings&#8221;, &#8220;PySide6&#8221;],<br>}<\/p><p># base=&#8221;Win32GUI&#8221; should only be used with the Windows GUI app <br>base = &#8220;Win32GUI&#8221; if sys.platform == &#8220;win32&#8221; else None<\/p><p>setup(<br>name=&#8221;guifoo&#8221;,<br>version=&#8221;0.1&#8243;,<br>description=&#8221;My GUI application!&#8221;,<br>options={&#8220;build_exe&#8221;: build_exe_options},<br>executables=[Executable(&#8220;guifoo.py&#8221;, base=base)],<br>)<\/p><p>Replace <strong>&#8220;guifoo&#8221;<\/strong> and <strong>&#8220;guifoo.py&#8221;<\/strong> with the file you wish to transform.<\/p><p>Next, run the command:<\/p><pre style=\"padding-left: 80px\">python setup.py build<\/pre><p>This command will create a subfolder called &#8220;build&#8221; with another subfolder beginning with the letters &#8220;exe.&#8221; and ending with the identification of the platform or <strong>your version of Python.<\/strong> This allows multiple platforms to be created without problems.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/09\/cx_freeze1.jpg\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t<h3>Cx_Freeze: the Python program in a nutshell<\/h3>\t\t\n\t\t<p>The <strong>cx_Freeze program<\/strong> is a great asset for developers: it compiles code and makes it executable on its own. The program identifies the modules required to execute the file and copies them into a virtual environment.<\/p><p><strong>Cx_Freeze<\/strong> offers simplified distribution, unrivalled portability and source code confidentiality. To install and use Cx_Freeze, simply follow the user guide above.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Start a Data training<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px} Cx Freeze is a Python program that transforms a script into a stand-alone executable on Linux, MacOS or Windows. Making a program executable, i.e. compiling the code, is important for easy distribution. Cx Freeze is the program that compiles the Python script and all its dependencies into an [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":182090,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-182087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=182087"}],"version-history":[{"count":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182087\/revisions"}],"predecessor-version":[{"id":205999,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182087\/revisions\/205999"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/182090"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=182087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=182087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}