{"id":421,"date":"2010-09-22T08:39:03","date_gmt":"2010-09-22T13:39:03","guid":{"rendered":"http:\/\/www.insomnihack.com\/?p=421"},"modified":"2011-06-01T22:03:30","modified_gmt":"2011-06-02T03:03:30","slug":"multiple-python-versions-on-osx-with-virtualenv-and-macports","status":"publish","type":"post","link":"http:\/\/www.insomnihack.com\/?p=421","title":{"rendered":"Multiple Python Versions on OSX with Virtualenv and Macports"},"content":{"rendered":"<blockquote><p><strong>Update<\/strong>: I&#8217;ve recently switched from Macports to Homebrew. I describe my new configuration in <a title=\"Multiple Python Versions on OSX with Virtualenv and Homebrew\" href=\"http:\/\/www.insomnihack.com\/?p=442\">this post<\/a>.<\/p><\/blockquote>\n<p>Here&#8217;s a rundown of how I get multiple versions of Python working on OSX. I&#8217;m running Snow Leopard on an older Macbook Pro that can\u00a0 only run in 32-bit mode. If you&#8217;ve  got a newer 64-bit Mac, your mileage may vary. In the next month or so, work will be  providing me with a new <a href=\"http:\/\/www.apple.com\/imac\/\">iMac<\/a>. If I  run into any issues using this technique on that 64-bit machine, I&#8217;ll  update this post.<\/p>\n<h2>Installing Macports<\/h2>\n<p>If you don&#8217;t already have it installed, grab the DMG file for <a href=\"http:\/\/www.macports.org\/\">Macports<\/a>. Verify that you have <a href=\"http:\/\/developer.apple.com\/technologies\/tools\/xcode.html\">Xcode<\/a> installed, and optionally, the <em>X11 User<\/em> package if you plan to install ports that use X-Windows (X11 User is installed by default on Leopard and Snow Leopard).<\/p>\n<h2>Installing Python<\/h2>\n<p>Now that you&#8217;ve got Macports installed, its time to install the versions of Python you want to work with. I&#8217;ll be describing the steps you&#8217;ll take at the Terminal to get all versions of Python that Macports supports installed, but you could just as easily use a Macports GUI like <a href=\"http:\/\/porticus.alittledrop.com\/\">Porticus<\/a> to install these packages. You might also only want a subset of these packages installed.<\/p>\n<div id=\"attachment_425\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.insomnihack.com\/wp-content\/uploads\/porticus_python.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-425\" class=\"size-medium wp-image-425\" title=\"Porticus\" src=\"http:\/\/www.insomnihack.com\/wp-content\/uploads\/porticus_python-300x240.png\" alt=\"Installing Macports via Porticus\" width=\"300\" height=\"240\" srcset=\"http:\/\/www.insomnihack.com\/wp-content\/uploads\/porticus_python-300x240.png 300w, http:\/\/www.insomnihack.com\/wp-content\/uploads\/porticus_python.png 724w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-425\" class=\"wp-caption-text\">Installing Macports via Porticus<\/p><\/div>\n<p>From a shell prompt, enter the following commands.<\/p>\n<p>[text]<br \/>\nsudo port -v selfupdate<br \/>\nsudo port install python24<br \/>\nsudo port install python25<br \/>\nsudo port install python26<br \/>\nsudo port install python27<br \/>\nsudo port install python31<br \/>\nsudo port install python_select<br \/>\n[\/text]<\/p>\n<p>At the time his post was written, executing these commands would install Python 2.4.6, 2.5.5, 2.6.6, 2.7, and 3.1.2, along with python_select into \/opt\/local\/bin. Running python_select with the -l option from the Terminal should result in something like this.<\/p>\n<p>[shell]<br \/>\n[dale@DaleBook ~]$ python_select -l<br \/>\nAvailable versions:<br \/>\ncurrent none python24 python25 python26 python26-apple python27 python31<br \/>\n[\/shell]<\/p>\n<p>On Snow Leopard, you&#8217;ll see a python26-apple option like above. On Leopard, you&#8217;ll see python25-apple. This is the system Python installation.<\/p>\n<h2>Installing Virtualenv<\/h2>\n<p>If you haven&#8217;t done so already, now it is time to install <a href=\"http:\/\/pypi.python.org\/pypi\/virtualenv\">virtualenv<\/a> and <a href=\"http:\/\/www.doughellmann.com\/projects\/virtualenvwrapper\/\">virtualenvwrapper<\/a> into your system Python. Now, you don&#8217;t have to install them into your main Python. You could switch to another one of the installed versions with python_select, but then you&#8217;d have to remember to switch to that version before using your virtual environments, and you might encounter configuration issues with editors. I just find installing these into the system Python results in less headaches.<\/p>\n<p>Both of these are installable via Macports, easy_install, or <a href=\"http:\/\/pypi.python.org\/pypi\/pip\">pip<\/a>. I&#8217;ll stick to the easy_install route, although I tend to favor pip when working with the virtual environments themselves.<\/p>\n<p>[shell]<br \/>\nsudo easy_install virtualenv<br \/>\nsudo easy_install virtualenvwrapper<br \/>\n[\/shell]<\/p>\n<p>Pick a location to store your virtual environments in (I use ~\/Envs), then add an export statement to your ~\/.profile file to point WORKON_HOME to this location. See the virtualenvwrapper <a href=\"http:\/\/www.doughellmann.com\/docs\/virtualenvwrapper\/\">docs<\/a> for more information on configuring this if you run into problems.<\/p>\n<h2>Create and Use a Virtual Environment<\/h2>\n<p>Now lets try it all out. Let&#8217;s create a virtual environment running Python 2.7. From the Terminal, enter the following:<\/p>\n<p>[text]<br \/>\nmkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python2.7 py27<br \/>\n[\/text]<\/p>\n<p>This should result in a new virtual environment based on Python 2.7. By changing the path specified with the &#8211;python option, you&#8217;ll be able to create a clean install of any of the versions of Python supported by Macports. Again, see the virtualenvwrapper documentation for more information on how to switch between and manage your virtual environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: I&#8217;ve recently switched from Macports to Homebrew. I describe my new configuration in this post. Here&#8217;s a rundown of how I get multiple versions of Python working on OSX. I&#8217;m running Snow Leopard on an older Macbook Pro that can\u00a0 only run in 32-bit mode. If you&#8217;ve got a newer 64-bit Mac, your mileage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[9,6,8],"_links":{"self":[{"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts\/421"}],"collection":[{"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421"}],"version-history":[{"count":0,"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}