{"id":97,"date":"2013-06-18T23:12:24","date_gmt":"2013-06-19T06:12:24","guid":{"rendered":"https:\/\/www.falatic.com\/?p=97"},"modified":"2013-06-20T22:55:44","modified_gmt":"2013-06-21T05:55:44","slug":"howto-getting-started-with-python-development-in-windows-with-msvsgcc-cython-etc","status":"publish","type":"post","link":"https:\/\/www.falatic.com\/index.php\/97\/howto-getting-started-with-python-development-in-windows-with-msvsgcc-cython-etc","title":{"rendered":"Howto: Getting started with Python development in Windows with MSVS\/GCC, Cython, etc."},"content":{"rendered":"<p dir=\"ltr\"><em>Note: this is directed at Python 2.7 developers, but the same principles should apply to Python 3.x as well.<\/em><\/p>\n<p dir=\"ltr\">First, a shout-out for the\u00a0<a href=\"http:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/\" target=\"_blank\">Unofficial Windows Binaries for Python<\/a> site: as noted below you can get the binaries for many commonly used packages simply by downloading and installing them, with no need to compile.<\/p>\n<p dir=\"ltr\">However, when it comes to things like Cython that NEED a working compiler at some point you&#8217;ll have to set one up. I wrote this post to help you with that, hopefully saving you many hours of frustration!<\/p>\n<p>Conclusions first: properly configuring Windows to compile for Python\/Cython takes a little effort but the hardest part is getting the details in one place (which this post is intended to do). The total setup time if you go the full Windows SDK\/Microsoft Visual Studio Express\/MinGW route isn&#8217;t that long &#8211; less than 30 minutes with a decent connection on a fast machine, with the majority of it spent in the download\/install phases.<\/p>\n<p>I DO suggest you install Microsoft Visual Studio Express 2008 and the\u00a0Windows SDK. As for MinGW, I strongly suspect there are limits to what you can ultimately do with it in this context but it&#8217;s presented here as an option for the adventurous (and it&#8217;s rather nice to have GCC handy in general!)<\/p>\n<p dir=\"ltr\"><!--more--><\/p>\n<hr \/>\n<p dir=\"ltr\">Windows isn&#8217;t the easiest system to work with, especially when you want to go the FOSS development route. In the case of Python 2.7 development involving things like Cython or many pip installs, if you don&#8217;t have the right Microsoft compilers installed (<a href=\"http:\/\/mail.python.org\/pipermail\/python-dev\/2012-August\/121410.html\" target=\"_blank\">why?<\/a>) you&#8217;ll most likely see the dreaded &#8220;error: Unable to find vcvarsall.bat&#8221; at some point&#8230; usually the moment you REALLY don&#8217;t have time for it. What to do?<\/p>\n<p dir=\"ltr\">One solution is to install Microsoft Visual Studio 2008 itself (at least the Express version, as well as the Windows SDK &#8211; it&#8217;s evident now that the SDK alone will NOT suffice). I&#8217;ve outlined that path below.<\/p>\n<p dir=\"ltr\">Another possible solution is to use GCC via MinGW, but you&#8217;ll need to jump through a few hoops to do it (and <a href=\"http:\/\/wiki.cython.org\/64BitCythonExtensionsOnWindows\" target=\"_blank\">it won&#8217;t work with Cython for x64 builds of Python<\/a>!) I can&#8217;t say for certain how far this approach will get you, but it worked for me when I tried it for 32-bit build tests.\u00a0YMMV&#8230; it&#8217;s probably not the most stable way to go for linking with a code-base originally built via MSVS.<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing the Windows SDK<\/strong><\/p>\n<p dir=\"ltr\">I&#8217;ve read that you can do everything with the Windows SDK alone, <a href=\"http:\/\/wiki.cython.org\/64BitCythonExtensionsOnWindows\" target=\"_blank\">at least for Cython<\/a> (also <a href=\"http:\/\/stackoverflow.com\/questions\/11072521\/install-python-modules-with-visual-studio-2008-on-windows-x64\" target=\"_blank\">here<\/a> and <a href=\"http:\/\/stackoverflow.com\/questions\/8088208\/how-can-i-get-compile-an-extension-using-cython\" target=\"_blank\">here<\/a>, among other places). So, <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=8279\" target=\"_blank\">get the\u00a0Windows SDK 7.1<\/a>\u00a0(IMHO) and install it. Note that you should only need the Developer tools, which clock in at 1.1 GB (Documentation and Samples take up an extra 3.5 GB!)<\/p>\n<p dir=\"ltr\" style=\"padding-left: 30px;\">You may get the following error: <em>SDKSetup &#8211; Another running instance of this application was detected. Only one instance of this application may be running at any time.<\/em><\/p>\n<p style=\"padding-left: 30px;\">Close that error window and you get the Program Compatibility Assistant. I&#8217;m quite used to closing that thing out of habit and searching Google in frustration, which I did, and found zero answers to this problem. I searched running processes &#8211; nothing. I ran Process Monitor &#8211; nothing stood out. I don&#8217;t have another instance installed. WTH?<\/p>\n<p style=\"padding-left: 30px;\">After a reboot and a few more failed attempts, I accidentally chose the &#8220;Reinstall using recommended settings&#8221; option. Suddenly, it worked! I have NO idea what special setting Windows &#8220;chose&#8221; for me (there&#8217;s no way to readily see that) but it worked.<\/p>\n<p>At this point enjoy your favorite soothing beverage and\/or blood pressure medication while the installation proceeds at the speed of a turtle crossing a busy highway. Poor turtle!<\/p>\n<hr \/>\n<p dir=\"ltr\">All done? Good! I created a little <em>build.cmd<\/em> script in the folder with my code that has the following (&#8220;::&#8221; is the same thing as &#8220;REM&#8221; in batch\/cmd files). You may need to adjust the paths slightly from what&#8217;s shown, and comment\/uncomment the appropriate bits (the SDK path may vary, for example):<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n::@echo off\r\n\r\n:: Selects the compiler AND Python27 target this works with! (x86 or x64)\r\n::set MYTGT=x64\r\nset MYTGT=x86\r\n\r\n:: The SDK you actually installed\r\nset MYSDK=v7.1\r\n\r\n:: I've made basic installs of Python with distribute, pip and cython in\r\n:: folders C:\\Python27_x86 and C:\\Python_x64 (so as not to collide with\r\n:: my usual Python at C:\\Python27)\r\n:: We set the path accordingly\r\nPATH=C:\\Python27_%MYTGT%;C:\\Python27_%MYTGT%\\Scripts;%PATH%\r\n\r\n:: Replicate cmd option \/E:ON (command extensions - haven't needed yet)\r\n::setlocal EnableExtensions\r\n\r\n:: Replicate cmd option \/V:ON (delayed expansion - required)\r\nsetlocal EnableDelayedExpansion\r\n\r\n:: Run SetEnv as the SDK cmd shell does (needs to be the same tgt as Python's:\r\ncall &quot;C:\\Program Files\\Microsoft SDKs\\Windows\\%MYSDK%\\Bin\\SetEnv.cmd&quot; \/%MYTGT% \/release\r\n\r\n:: I've seen this setting mentioned for other projects but haven't had to use it myself\r\n::set MSSdk=1\r\n\r\n:: Select the proper SDK for Python 2.7 (6.0A versus a later one - VERY important!)\r\nset DISTUTILS_USE_SDK=1\r\n\r\necho.\r\necho -- Compiler and Python version check\r\necho.\r\necho Compilation target: %TARGET_PLATFORM%-%TARGET_CPU% for Python27_%MYTGT%\r\necho.\r\npython -c &quot;import sys;print('Python '+sys.version.replace('\\n','')+' on '+sys.platform)&quot;\r\n\r\n:: Your build commands go below, e.g.:\r\n\r\necho.\r\necho -- Building it\r\necho.\r\n\r\n::del *.pyd\r\n::python setup.py build_ext --inplace\r\n\r\necho.\r\necho -- Testing it\r\necho.\r\n\r\n::python testhello.py\r\n\r\necho.\r\necho -- Done!\r\necho.\r\npause\r\n\r\n<\/pre>\n<p dir=\"ltr\">If running that worked (even with no code to compile it&#8217;ll at least turn green and mention that it&#8217;s using SDK v6.0A) then congratulations: you should be ready to go on! We hope!<\/p>\n<p dir=\"ltr\">Note: If you try to run these commands directly in a command prompt it&#8217;ll likely *fail*. Either ensure you started cmd.exe with the\u00a0&#8220;\/V:ON&#8221; option, or start one from where you are at by typing\u00a0&#8220;cmd.exe \/V:ON&#8221;. Why? Because &#8220;setlocal EnableDelayedExpansion&#8221; ONLY works in a batch file, that&#8217;s why.<\/p>\n<p dir=\"ltr\">You&#8217;ll need to perform these steps before installing any package that requires compilation (such as Cython) or \u00a0building your code. You could make some shortcuts so you can just click and have an environment ready to use.<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing Microsoft Visual Studio Express 2008 <del>(<em>might not be necessary)<\/em><\/del> (oh yeah, it&#8217;s necessary!)<\/strong><\/p>\n<p dir=\"ltr\">Though some of what I&#8217;ve read suggested MSVS is not required, to avoid the missing <em>vcvarsall.bat<\/em> problem you still need to install MSVS &#8220;Express&#8221; 2008 as found in (of all things) <a href=\"http:\/\/stackoverflow.com\/questions\/15318560\/visual-c-2008-express-download-link-dead\" target=\"_blank\">a closed thread on StackOverflow<\/a>. You can find the official installer at\u00a0<a href=\"http:\/\/go.microsoft.com\/?linkid=7729279\" target=\"_blank\">http:\/\/go.microsoft.com\/?linkid=7729279<\/a><\/p>\n<p dir=\"ltr\">Skip the SQL part of the install to save time and space.<\/p>\n<p dir=\"ltr\">Have another beverage&#8230; <del>it&#8217;s gonna be a while<\/del>. Actually, it was blazingly fast on this machine. Still&#8230; refreshing!<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Dealing with despair<\/strong><\/p>\n<p dir=\"ltr\"><em>At this point you may be considering wiping your system and installing some version of Linux (or possibly buying an entirely new piece of hardware simply to run a hardware-locked Linux-like commercial OS). But, you&#8217;re a trouper and you hung on this long &#8211; you may yet be rewarded! (And despite all this, knowing how to build in Windows is a handy if esoteric skill).<\/em><\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing MinGW\/GCC<\/strong><\/p>\n<p dir=\"ltr\">Going the GCC route, or thinking about it?\u00a0Install <a href=\"http:\/\/tdm-gcc.tdragon.net\/\" target=\"_blank\">the\u00a032-bit version of\u00a0TDM-GCC<\/a>\u00a0(the 32\/64-bit version will NOT work).<\/p>\n<p dir=\"ltr\">When using newer versions of MinGW, you need to edit <em>C:\\Python27\\Lib\\distutils\\cygwinccompiler.py<\/em> and remove all refs to <strong>-mno-cygwin<\/strong> (this is an obsolete option in GCC 4.7)<\/p>\n<p dir=\"ltr\">To actually use MinGW32&#8217;s gcc instead of MSVS with Python, you&#8217;ll need to create <em>C:\\Python27\\Lib\\distutils\\distutils.cfg<\/em> with the contents:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\n&#x5B;build]\r\n\r\ncompiler = mingw32\r\n\r\n<\/pre>\n<p dir=\"ltr\">(If later you decide to use MSVS, be sure to rename or remove this file if it&#8217;s present. You probably should reinstall Python and your packages using MSVS at that point to ensure it&#8217;s consistent.)<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing Python 2.7<\/strong><\/p>\n<p dir=\"ltr\">Install <a href=\"http:\/\/www.python.org\/download\/\" target=\"_blank\">Python 2.7.x from the main site<\/a>. I had three versions installed for the purpose of all this testing: the x86 one in\u00a0<em>C:\\Python27_x86<\/em>, the x64 one in <em>C:\\Python_x64<\/em>, and my &#8220;default&#8221; version in <em>C:\\Python27<\/em>. that was really just a copy of one of the other two. You will likely just have one version installed (32- or 64-bit) so you&#8217;ll adjust the script above accordingly.<\/p>\n<p dir=\"ltr\">People seem to use the 32-bit version quite a lot&#8230;. the one with &#8220;X86-64&#8221; by it is the 64-bit version (note that the 64-bit version will identify itself as AMD64 when you run it. <a href=\"https:\/\/en.wikipedia.org\/wiki\/X86-64\" target=\"_blank\">Why so many ways to say the same thing, you ask?<\/a>)You can always use the 32-bit version, while the 64-bit one only works with a 64-bit OS. Also, 64-bit modules sometimes have less support so keep that in mind as well. Not sure? Do what I did and build for both! \ud83d\ude42<\/p>\n<p dir=\"ltr\">Another random note: <em><a href=\"http:\/\/en.wikipedia.org\/wiki\/Itanium\" target=\"_blank\">IA64 is NOT another name for x64\/X86-64\/AMD64<\/a> and it&#8217;s totally outside the scope of what I&#8217;m presenting here.<\/em> If you have to ask you probably aren&#8217;t using it (is anyone anymore?) I mention it only because I got confused by it back when I was using MSVS 2008 more regularly and it led to a lot of confusion. If you are confused now just forget this little paragraph.<\/p>\n<p dir=\"ltr\">There. All better!<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing Pip<\/strong><\/p>\n<p dir=\"ltr\">The easy way: Install the <a href=\"http:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#pip\" target=\"_blank\">unofficial WIndows binary<\/a> that matches your version of Python!<\/p>\n<p dir=\"ltr\">The harder but eminently fulfilling way (based on <a href=\"http:\/\/stackoverflow.com\/a\/9038397\/760905\" target=\"_blank\">this comment<\/a>):<\/p>\n<p dir=\"ltr\">Save <a href=\"http:\/\/python-distribute.org\/distribute_setup.py\" target=\"_blank\">http:\/\/python-distribute.org\/distribute_setup.py<\/a> as\u00a0<em>C:\\Python27\\Scripts\\distribute_setup.py<\/em>\u00a0(you may need to create that Scripts directory).\u00a0Python ought to be in your path, as should this Scripts folder. Set your system or user path correctly or ensure it&#8217;s set during your command prompt sessions thus:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">set PATH=%PATH%;C:\\Python27;C:\\Python27\\Scripts<\/pre>\n<p dir=\"ltr\">Run the script:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">distribute_setup.py<\/pre>\n<p dir=\"ltr\">Now install pip:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">easy_install pip<\/pre>\n<hr \/>\n<p dir=\"ltr\"><strong>Installing Cython<\/strong><\/p>\n<p dir=\"ltr\">The easy way:\u00a0Install the\u00a0<a href=\"http:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#cython\" target=\"_blank\">unofficial WIndows binary<\/a>\u00a0that matches your version of Python!<\/p>\n<p dir=\"ltr\">The harder but more rewarding way:<\/p>\n<p dir=\"ltr\">THIS is where that Windows SDK you installed becomes important.<\/p>\n<p dir=\"ltr\"><span style=\"color: #ff0000;\"><strong>First, run the setenv and other commands as you did above to test the SDK!<\/strong><\/span><\/p>\n<p dir=\"ltr\">Then:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">pip install cython<\/pre>\n<p dir=\"ltr\">It should &#8220;just work&#8221;, compiling and installing without failures.<\/p>\n<p dir=\"ltr\">Remember: if you&#8217;re using 32-bit python you need to be using the 32-bit x86 settings! 64-bit? Use the 64-bit x64 settings! Got a raft of &#8220;unresolved external symbol&#8221; errors? You probably chose the wrong one, or forgot to choose one.<\/p>\n<p dir=\"ltr\">I tried it on both 32- and 64-bit versions of Python 2.7.5 (selecting the proper target for each). It worked. I was pretty amazed. A beverage was forthcoming!<\/p>\n<hr \/>\n<p dir=\"ltr\"><strong>Finally, create and compile something with Cython<\/strong>, such as\u00a0<a href=\"http:\/\/docs.cython.org\/src\/userguide\/tutorial.html\" target=\"_blank\">the &#8220;helloworld&#8221; example from the Cython tutorial<\/a>.<\/p>\n<p dir=\"ltr\"><em>(Remember the &#8220;handy&#8221; build.cmd file I mentioned above? This would be a great time to use it!)<\/em><\/p>\n<p dir=\"ltr\">Note that the compiled version ends in .pyd NOT .dll (typo in the docs)<\/p>\n<hr \/>\n<p><strong>More resources:<\/strong><\/p>\n<p dir=\"ltr\"><a href=\"https:\/\/twitter.com\/raymondh\">https:\/\/twitter.com\/raymondh<\/a> (Subscribe!)<\/p>\n<p dir=\"ltr\"><a href=\"http:\/\/wiki.cython.org\/InstallingOnWindows\" target=\"_blank\">http:\/\/wiki.cython.org\/InstallingOnWindows<\/a><\/p>\n<p dir=\"ltr\"><a href=\"http:\/\/stackoverflow.com\/questions\/6034390\/compiling-with-cython-and-mingw-produces-gcc-error-unrecognized-command-line-o\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/6034390\/compiling-with-cython-and-mingw-produces-gcc-error-unrecognized-command-line-o<\/a><\/p>\n<p dir=\"ltr\"><a href=\"http:\/\/stackoverflow.com\/questions\/4750806\/how-to-install-pip-on-windows\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/4750806\/how-to-install-pip-on-windows<\/a><\/p>\n<p dir=\"ltr\"><a href=\"http:\/\/stackoverflow.com\/questions\/17185389\/how-would-i-get-the-detailed-version-of-my-python-interpreter-via-a-one-liner\">http:\/\/stackoverflow.com\/questions\/17185389\/how-would-i-get-the-detailed-version-of-my-python-interpreter-via-a-one-liner<\/a>\u00a0(thanks, poolie!)<\/p>\n<!-- wpsso rrssb get buttons: buttons on archive option not enabled -->\n","protected":false},"excerpt":{"rendered":"<p>Note: this is directed at Python 2.7 developers, but the same principles should apply to Python 3.x as well. First, a shout-out for the\u00a0Unofficial Windows Binaries for Python site: as <a href=\"https:\/\/www.falatic.com\/index.php\/97\/howto-getting-started-with-python-development-in-windows-with-msvsgcc-cython-etc\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[86],"tags":[102,32,101,100,117],"class_list":["entry","author-marty","has-more-link","post-97","post","type-post","status-publish","format-standard","category-software-and-hardware-development","tag-cython","tag-gcc","tag-msvs","tag-python","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}