{"id":4861,"date":"2024-03-23T06:22:24","date_gmt":"2024-03-23T06:22:24","guid":{"rendered":"https:\/\/sparksupport.com\/blog\/?p=4861"},"modified":"2024-06-12T05:04:43","modified_gmt":"2024-06-12T05:04:43","slug":"ship-with-confidence-protect-your-python-code-with-cython","status":"publish","type":"post","link":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/","title":{"rendered":"Ship with Confidence: Protect Your Python Code with Cython"},"content":{"rendered":"<h2><b>Distributing Your Python Code as an Executable with Cython<\/b><\/h2>\n<p><b>Ever wondered how to keep your Python code private when distributing software?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">While obfuscation tools like Pyarmor add some protection, they can&#8217;t fully prevent someone from deciphering your code. Here&#8217;s where Cython comes in.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Cython is a compiler that lets you convert Python code into a compiled extension, like a <\/span><span style=\"font-weight: 400;\">.so<\/span><span style=\"font-weight: 400;\"> file. This compiled extension can be included with your software and protects your original Python source code.<\/span><\/p>\n<p><b>Here&#8217;s how to use Cython to create a distributable executable:<\/b><\/p>\n<h3><b>1. Install Cython:<\/b><\/h3>\n<pre>Bash\r\n\r\npip install cython<\/pre>\n<h3><b> 2. Convert your Python script:<\/b><\/h3>\n<p>This file tells Cython which Python files to convert. Create a new file named setup.py with the following content, replacing cube.pyx with your file name:<\/p>\n<pre>Python\r\nfrom setuptools import setup\r\nfrom Cython.Build import cythonize\r\n\r\nsetup(\r\n    ext_modules = cythonize(\"cube.pyx\")\r\n)\r\n<\/pre>\n<h3><b>4. Build the Cython extension:<b><\/b><\/b><\/h3>\n<p>Run the following command in your terminal:<\/p>\n<pre>Bash\r\npython setup.py build_ext --inplace\r\n<\/pre>\n<p>This command will generate two new files:<\/p>\n<ul>\n<li>A .c file containing the compiled C code.<\/li>\n<li>A .so file (or .pyd on Windows) containing the compiled extension.<\/li>\n<\/ul>\n<h4>Compiling the .c file (optional):<\/h4>\n<p>If you want a standalone executable, you can compile the generated .c file using a C compiler like GCC. This will create an executable file containing your Python code.<\/p>\n<h4>Using the compiled extension:<\/h4>\n<p>To use the compiled extension in another Python script, simply import it like a regular module. For example:<\/p>\n<pre>Python\r\nfrom cube import cube\r\nprint(\"9 cubed =\", cube(9))\r\n<\/pre>\n<h4>Remember:<\/h4>\n<ul>\n<li>Place the .so file in the same directory as your Python script that uses it.<\/li>\n<\/ul>\n<p>By following these steps, you can distribute your Python code as a compiled extension, protecting your source code while still providing a functional program to your clients.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Distributing Your Python Code as an Executable with Cython Ever wondered how to keep your Python code private when distributing software? While obfuscation tools like<\/p>\n","protected":false},"author":1,"featured_media":4978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Ship with Confidence: Protect Your Python Code with Cython -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ship with Confidence: Protect Your Python Code with Cython -\" \/>\n<meta property=\"og:description\" content=\"Distributing Your Python Code as an Executable with Cython Ever wondered how to keep your Python code private when distributing software? While obfuscation tools like\" \/>\n<meta property=\"og:url\" content=\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-23T06:22:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-12T05:04:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"445\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"SparkSupport\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"SparkSupport\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\"},\"author\":{\"name\":\"SparkSupport\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/person\/b359b1e8bc00b1d71637775f13a9ec44\"},\"headline\":\"Ship with Confidence: Protect Your Python Code with Cython\",\"datePublished\":\"2024-03-23T06:22:24+00:00\",\"dateModified\":\"2024-06-12T05:04:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\"},\"wordCount\":268,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\",\"url\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\",\"name\":\"Ship with Confidence: Protect Your Python Code with Cython -\",\"isPartOf\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png\",\"datePublished\":\"2024-03-23T06:22:24+00:00\",\"dateModified\":\"2024-06-12T05:04:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage\",\"url\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png\",\"contentUrl\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png\",\"width\":800,\"height\":445,\"caption\":\"Protect Your Python Code with Cython (1)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/beta.sparksupport.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ship with Confidence: Protect Your Python Code with Cython\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#website\",\"url\":\"https:\/\/beta.sparksupport.com\/blog\/\",\"name\":\"SparkSupport Blog\",\"description\":\"SparkSupport Blogs\",\"publisher\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/beta.sparksupport.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#organization\",\"name\":\"SparkSupport\",\"url\":\"https:\/\/beta.sparksupport.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2019\/08\/cropped-logo-1.jpg\",\"contentUrl\":\"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2019\/08\/cropped-logo-1.jpg\",\"width\":216,\"height\":44,\"caption\":\"SparkSupport\"},\"image\":{\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/person\/b359b1e8bc00b1d71637775f13a9ec44\",\"name\":\"SparkSupport\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g\",\"caption\":\"SparkSupport\"},\"url\":\"https:\/\/beta.sparksupport.com\/blog\/author\/spark_wp_admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ship with Confidence: Protect Your Python Code with Cython -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/","og_locale":"en_US","og_type":"article","og_title":"Ship with Confidence: Protect Your Python Code with Cython -","og_description":"Distributing Your Python Code as an Executable with Cython Ever wondered how to keep your Python code private when distributing software? While obfuscation tools like","og_url":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/","article_published_time":"2024-03-23T06:22:24+00:00","article_modified_time":"2024-06-12T05:04:43+00:00","og_image":[{"width":800,"height":445,"url":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png","type":"image\/png"}],"author":"SparkSupport","twitter_card":"summary_large_image","twitter_misc":{"Written by":"SparkSupport","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#article","isPartOf":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/"},"author":{"name":"SparkSupport","@id":"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/person\/b359b1e8bc00b1d71637775f13a9ec44"},"headline":"Ship with Confidence: Protect Your Python Code with Cython","datePublished":"2024-03-23T06:22:24+00:00","dateModified":"2024-06-12T05:04:43+00:00","mainEntityOfPage":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/"},"wordCount":268,"commentCount":0,"publisher":{"@id":"https:\/\/beta.sparksupport.com\/blog\/#organization"},"image":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage"},"thumbnailUrl":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/","url":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/","name":"Ship with Confidence: Protect Your Python Code with Cython -","isPartOf":{"@id":"https:\/\/beta.sparksupport.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage"},"image":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage"},"thumbnailUrl":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png","datePublished":"2024-03-23T06:22:24+00:00","dateModified":"2024-06-12T05:04:43+00:00","breadcrumb":{"@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#primaryimage","url":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png","contentUrl":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2024\/03\/Protect-Your-Python-Code-with-Cython-1.png","width":800,"height":445,"caption":"Protect Your Python Code with Cython (1)"},{"@type":"BreadcrumbList","@id":"https:\/\/beta.sparksupport.com\/blog\/ship-with-confidence-protect-your-python-code-with-cython\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/beta.sparksupport.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ship with Confidence: Protect Your Python Code with Cython"}]},{"@type":"WebSite","@id":"https:\/\/beta.sparksupport.com\/blog\/#website","url":"https:\/\/beta.sparksupport.com\/blog\/","name":"SparkSupport Blog","description":"SparkSupport Blogs","publisher":{"@id":"https:\/\/beta.sparksupport.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/beta.sparksupport.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/beta.sparksupport.com\/blog\/#organization","name":"SparkSupport","url":"https:\/\/beta.sparksupport.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2019\/08\/cropped-logo-1.jpg","contentUrl":"https:\/\/beta.sparksupport.com\/blog\/wp-content\/uploads\/2019\/08\/cropped-logo-1.jpg","width":216,"height":44,"caption":"SparkSupport"},"image":{"@id":"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/beta.sparksupport.com\/blog\/#\/schema\/person\/b359b1e8bc00b1d71637775f13a9ec44","name":"SparkSupport","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d86cc3c0d188e40e99abec16795ed658b95c89ab15427bd7254315e8115b40b?s=96&d=mm&r=g","caption":"SparkSupport"},"url":"https:\/\/beta.sparksupport.com\/blog\/author\/spark_wp_admin\/"}]}},"_links":{"self":[{"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/posts\/4861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/comments?post=4861"}],"version-history":[{"count":0,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/posts\/4861\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/media\/4978"}],"wp:attachment":[{"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/media?parent=4861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/categories?post=4861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beta.sparksupport.com\/blog\/wp-json\/wp\/v2\/tags?post=4861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}