{"id":1968,"date":"2016-01-12T11:30:05","date_gmt":"2016-01-12T10:30:05","guid":{"rendered":"http:\/\/www.lookingatnothing.com\/?p=1968"},"modified":"2016-01-10T15:11:39","modified_gmt":"2016-01-10T14:11:39","slug":"setting-up-a-python-development-environment-on-mac-os-x","status":"publish","type":"post","link":"https:\/\/lookingatnothing.com\/index.php\/archives\/1968","title":{"rendered":"Setting up a Python development environment on Mac OS X"},"content":{"rendered":"<p>Right before the Christmas holiday, my new Mac arrived at work. This computer will be used mainly as a test and development environment for the software projects, and so needed a very flexible installation of Python. This took quite a while to get right, so here I will document the exact steps required to get it to this state.\u00a0<!--more--><\/p>\n<figure id=\"attachment_1970\" aria-describedby=\"caption-attachment-1970\" style=\"width: 300px\" class=\"wp-caption alignright\"><a href=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python.png\" rel=\"attachment wp-att-1970\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1970\" src=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python-300x300.png\" alt=\"Hatchling orange ghost morph ball python. CC-licensed image from: https:\/\/commons.wikimedia.org\/wiki\/File:Orange_Ghost_%28Hypomelanistic%29_Ball_Python.jpg\" width=\"300\" height=\"300\" srcset=\"https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python-300x300.png 300w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python-150x150.png 150w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1970\" class=\"wp-caption-text\">Figure 1: Hatchling orange ghost morph ball python. CC-licensed image from: https:\/\/commons.wikimedia.org\/wiki\/File:Orange_Ghost_%28Hypomelanistic%29_Ball_Python.jpg<\/figcaption><\/figure>\n<p>When developing software, people can discover\u00a0all kinds of strange bugs that are related to particular software versions or computer platform combinations. For us to debug these, we need to be able to replicate any such\u00a0combination. Therefore, I needed an installation which would allow me to select a given version of Python, and select particular versions of python modules to be combined with that (however if all you want to do is to install Python, the following is probably overkill).<\/p>\n<p>Enabling this flexibility comes at the cost of increased complexity, but in the end I arrived at a decent workspace that allows the above pick-and-choosing of versions for any given Pythonic task. From this point on, this post will get a bit more technical than usual, but it remains well within the domain of the small-angle scatterer! That said, there are superfluous items\u00a0in the following instructions, and there are probably ways to make this process more efficient, so feel free to leave comments with improvements!<\/p>\n<p>For the following, all lines following the &#8220;$&#8221; (string) character are to be typed into a terminal window (which you get when opening the &#8220;terminal&#8221; app). The characters &#8220;~\/&#8221; denote your user home directory, in my case this is the directory &#8220;\/Users\/brian&#8221;. While I understand from some (anonymous) beamline managers that some (anonymous) beamline users may be a bit hesitant to use\u00a0any text-based interface, I do recommend playing around with these a bit. After a while, you will find it easy to be able to do some tasks in there which can be replicated <em>every single time<\/em> just by copy-pasting some commands. Anyway, without further ado, the installation guidelines for\u00a0a fresh install of OS X 10.11:<\/p>\n<ul>\n<li>Install some &#8220;essential&#8221; OS X software, in this order: GPG Suite, OpenSCAD, SourceTree, Tor Browser, mactex, Xcode (the latter via the App store, currently version 7.2, build 7C68).<\/li>\n<li>modify (or create) ~\/.bash_profile to include the following text:<\/li>\n<\/ul>\n<pre>export LC_ALL=en_GB.UTF-8\nexport LAN=en_GB.UTF-8\nexport ARCHFLAGS=\"-arch x86_64\"\n# export PATH=\/usr\/local\/bin:$PATH # (no longer necessary, already correct)\ntest -f ~\/.bashrc &amp;&amp; source ~\/.bashrc<\/pre>\n<ul>\n<li>modify (or create) ~\/.bashrc to include the following text:<\/li>\n<\/ul>\n<pre>export PIP_REQUIRE_VIRTUALENV=true\n# define a \"global pip\" function to use outside virtualenv:\ngpip(){\n\u00a0 \u00a0 PIP_REQUIRE_VIRTUALENV=\"\" pip \"$@\"\n}\ntest -f \/usr\/local\/bin\/virtualenvwrapper.sh &amp;&amp; source \/usr\/local\/bin\/virtualenvwrapper.sh\nif which pyenv &gt; \/dev\/null ; then eval \"$(pyenv init -)\"; fi<\/pre>\n<ul>\n<li>Update the running terminals with the new settings using:<\/li>\n<\/ul>\n<pre>$ source ~\/.bash_profile<\/pre>\n<ul>\n<li>Install Homebrew for OSX software package management, and configure:<\/li>\n<\/ul>\n<pre>$ ruby -e \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/master\/install)\"\n$ brew doctor\n$ brew tap homebrew\/science\n$ brew tap homebrew\/python\n$ brew tap homebrew\/versions\n$ brew update\n$ brew upgrade<\/pre>\n<ul>\n<li>Install Pyenv which allows the installation of isolated versions of Python<\/li>\n<\/ul>\n<pre>$ brew install pyenv<\/pre>\n<ul>\n<li>Install the Xcode command-line tools (click the highlighted option on the dialog that pops up):<\/li>\n<\/ul>\n<pre>$ xcode-select --install<\/pre>\n<ul>\n<li>Install a Python environment before we can continue (2.7.11 is the most modern python2 version):<\/li>\n<\/ul>\n<pre>$ pyenv install --list\n$ pyenv install 2.7.11\u00a0\n$ pyenv local 2.7.11<\/pre>\n<ul>\n<li>Now we can install virtualenv, which allows us to separate the python module versions:<\/li>\n<\/ul>\n<pre>$ gpip install virtualenv<\/pre>\n<ul>\n<li>And the package that manages the mix of python versions with the mix of module versions (note: don&#8217;t do the &#8220;auto-activate&#8221; thing):<\/li>\n<\/ul>\n<pre>$ brew install pyenv-virtualenv<\/pre>\n<ul>\n<li>Now we can set up a custom environment ideal for running McSAS. First we install the required software for the graphics back-end (pyside 1.2.2 (we need the post install script which has been removed from later versions of pyside)), and then we set up a modern python2 environment (2.7.11) called &#8220;mcsas&#8221;.<\/li>\n<\/ul>\n<pre>$ brew install cmake qt mercurial pandoc\n$ brew linkapps\n$ pyenv virtualenv 2.7.11 mcsas\n$ pyenv activate mcsas\n$ pip install numpy scipy matplotlib\n$ pip install -Iv pyside==1.2.2\u00a0\n$ python ~\/.pyenv\/versions\/mcsas\/bin\/pyside_postinstall.py -install<\/pre>\n<ul>\n<li>You can check the module versions that are installed for each of the modules using:<\/li>\n<\/ul>\n<pre>$ pip freeze<\/pre>\n<ul>\n<li>which returns:<\/li>\n<\/ul>\n<pre>cycler==0.9.0\nmatplotlib==1.5.0\nnumpy==1.10.2\npyparsing==2.0.7\nPySide==1.2.2\npython-dateutil==2.4.2\npytz==2015.7\nscipy==0.16.1\nsix==1.10.0\nwheel==0.24.0<\/pre>\n<ul>\n<li>You see that there are several modules installed that we did not explicitly install ourselves. These are installed automatically when you install a given module, because that module needs it. (This list can be stored in a text file for easy reinstallation later on if desired). We can now set up a separate Python environment for\u00a0running python (jupyter) notebooks:<\/li>\n<\/ul>\n<pre>$ pyenv virtualenv 2.7.11 notebook2\n$ pyenv activate notebook2\n$ pip install scipy numpy matplotlib pandas ipython jupyter h5py\u00a0\n$ ipython notebook<\/pre>\n<ul>\n<li>\n<figure id=\"attachment_1971\" aria-describedby=\"caption-attachment-1971\" style=\"width: 300px\" class=\"wp-caption alignright\"><a href=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact.png\" rel=\"attachment wp-att-1971\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1971\" src=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact-300x232.png\" alt=\"An iPython notebook running Python 3\" width=\"300\" height=\"232\" srcset=\"https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact-300x232.png 300w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact-768x593.png 768w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact-1024x791.png 1024w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/TFact.png 1062w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1971\" class=\"wp-caption-text\">Figure 2: An iPython notebook running Python 3<\/figcaption><\/figure>\n<p>Or a notebook using\u00a0python 3 (see Figure 2):<\/li>\n<\/ul>\n<pre>$ pyenv install 3.5.1\n$ pyenv virtualenv 3.5.1 notebook3\n$ pyenv activate notebook3\n$ pip install scipy numpy matplotlib pandas ipython jupyter h5py\n$ ipython notebook<\/pre>\n<p>In all of these cases, each python installation and module configuration lives separate from each other, which makes reinstallation, upgrading and testing very easy. As mentioned, this is overkill if all you want to do is to install a single Python, but it is very useful for making sure that every program gets exactly the environment\u00a0it likes best.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Right before the Christmas holiday, my new Mac arrived at work. This computer will be used mainly as a test and development environment for the <a class=\"mh-excerpt-more\" href=\"https:\/\/lookingatnothing.com\/index.php\/archives\/1968\" title=\"Setting up a Python development environment on Mac OS X\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":2,"featured_media":1970,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/lookingatnothing.com\/wp-content\/uploads\/2016\/01\/600px-Orange_Ghost_Hypomelanistic_Ball_Python.png","jetpack_shortlink":"https:\/\/wp.me\/p1gZ2v-vK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/1968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/comments?post=1968"}],"version-history":[{"count":4,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/1968\/revisions"}],"predecessor-version":[{"id":1988,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/1968\/revisions\/1988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/media\/1970"}],"wp:attachment":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/media?parent=1968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/categories?post=1968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/tags?post=1968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}