{"id":435,"date":"2010-11-05T21:51:59","date_gmt":"2010-11-06T02:51:59","guid":{"rendered":"http:\/\/www.insomnihack.com\/?p=435"},"modified":"2011-06-01T22:06:56","modified_gmt":"2011-06-02T03:06:56","slug":"python-3-with-virtualenv-and-macports","status":"publish","type":"post","link":"https:\/\/www.insomnihack.com\/?p=435","title":{"rendered":"Python 3 with Virtualenv and Macports"},"content":{"rendered":"<blockquote><p><strong>Update<\/strong>: As of virtualenv 1.6, Python 3 is supported out of the box, so all this is no longer necessary. I touch on it briefly 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>In my <a href=\"http:\/\/www.insomnihack.com\/?p=421\">last post<\/a>, I described how to get all the various versions of Python currently supported by <a href=\"http:\/\/www.macports.org\/\">Macports<\/a> working together on OSX with <a href=\"http:\/\/pypi.python.org\/pypi\/virtualenv\">virtualenv<\/a> and <a href=\"http:\/\/www.doughellmann.com\/projects\/virtualenvwrapper\/\">virtualenvwrapper<\/a>. There was one small problem, though. If you tried to create a new virtual environment with Python 3, you&#8217;d encounter this error:<\/p>\n<p>[text]<br \/>\n$ mkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python3.1 bar<br \/>\nRunning virtualenv with interpreter \/opt\/local\/bin\/python3.1<br \/>\nFile &quot;\/Library\/Python\/2.6\/site-packages\/virtualenv-1.5.1-py2.6.egg\/virtualenv.py&quot;, line 17<br \/>\nexcept ImportError, e:<br \/>\n                  ^<br \/>\nSyntaxError: invalid syntax<br \/>\n[\/text]<\/p>\n<p>This occurs because the <em>except<\/em> syntax has <a href=\"http:\/\/www.python.org\/dev\/peps\/pep-3110\/\">changed<\/a> in Python 3, and <a href=\"http:\/\/pypi.python.org\/pypi\/setuptools\">setuptools<\/a>, <a href=\"http:\/\/pypi.python.org\/pypi\/pip\">pip<\/a>, and virtualenv haven&#8217;t been updated as yet. However, we can still get Python 3 to play nice with virtualenvwrapper. With Brandon Rhode&#8217;s <a href=\"http:\/\/bitbucket.org\/brandon\/virtualenv3\">virtualenv3<\/a> fork and a bit of bash profile arcanum, we can get Python 3 to play nice with virtualenvwrapper and the other versions of Python installed on our system.<\/p>\n<p>First, go grab virtualenv3 from <a href=\"http:\/\/bitbucket.org\">bitbucket<\/a> and install it using Python 3. The example below assumes you&#8217;ve installed Python 3.1 and <a href=\"http:\/\/python_select.darwinports.com\">python_select<\/a> via macports as described in my last post, and that the default Snow Leopard Python 2.6.1 is active.<\/p>\n<p>[text]<br \/>\nsudo python_select python31<br \/>\nhg clone http:\/\/bitbucket.org\/brandon\/virtualenv3<br \/>\ncd virtualenv<br \/>\npython setup.py develop<br \/>\nsudo python_select python26-apple<br \/>\n[\/text]<\/p>\n<p>Now open your bash ~\/.profile in an editor and add the following aliases and functions:<\/p>\n<p>[text]<br \/>\nalias mkve24=&#8217;mkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python2.4&#8242;<br \/>\nalias mkve25=&#8217;mkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python2.5&#8242;<br \/>\nalias mkve26=&#8217;mkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python2.6&#8242;<br \/>\nalias mkve27=&#8217;mkvirtualenv &#8211;no-site-packages &#8211;python=\/opt\/local\/bin\/python2.7&#8242;<br \/>\nfunction mkve31() {<br \/>\n    \/opt\/local\/bin\/python3.1 \/Users\/daledavis\/Source\/virtualenv3\/virtualenv3.py &#8211;no-site-packages $WORKON_HOME\/$@<br \/>\n}<br \/>\n[\/text]<\/p>\n<p>This assumes you have virtualenvwrapper installed with the WORKON_HOME environment variable. Also note that the mkve31 function body is all on one line despite what my skinny WordPress theme might suggest.<\/p>\n<p>Now we should be able to create virtual environments with all the installed Python versions without hang up. Open up a new terminal and try creating a Python 2.7 and Python 3.1 virtual environment. If all is as it should be, you should see something very similar to what I&#8217;ve pasted below.<\/p>\n<p>[text]<br \/>\n$ mkve27 py27<br \/>\nRunning virtualenv with interpreter \/opt\/local\/bin\/python2.7<br \/>\nNew python executable in py27\/bin\/python<br \/>\nInstalling setuptools&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.done.<br \/>\nvirtualenvwrapper.user_scripts Creating \/Users\/daledavis\/VirtualEnvs\/py27\/bin\/predeactivate<br \/>\nvirtualenvwrapper.user_scripts Creating \/Users\/daledavis\/VirtualEnvs\/py27\/bin\/postdeactivate<br \/>\nvirtualenvwrapper.user_scripts Creating \/Users\/daledavis\/VirtualEnvs\/py27\/bin\/preactivate<br \/>\nvirtualenvwrapper.user_scripts Creating \/Users\/daledavis\/VirtualEnvs\/py27\/bin\/postactivate<br \/>\nvirtualenvwrapper.user_scripts Creating \/Users\/daledavis\/VirtualEnvs\/py27\/bin\/get_env_details<br \/>\n(py27)$ mkve31 py31<br \/>\nNew python executable in \/Users\/daledavis\/VirtualEnvs\/py31\/bin\/python<br \/>\nInstalling setuptools&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;done.<br \/>\n(py27)$ python &#8211;version<br \/>\nPython 2.7<br \/>\n(py27)$ workon py31<br \/>\n(py31)$ python &#8211;version<br \/>\nPython 3.1.2<br \/>\n[\/text]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: As of virtualenv 1.6, Python 3 is supported out of the box, so all this is no longer necessary. I touch on it briefly in this post. In my last post, I described how to get all the various versions of Python currently supported by Macports working together on OSX with virtualenv and virtualenvwrapper. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[9,6,8],"_links":{"self":[{"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts\/435"}],"collection":[{"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":0,"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.insomnihack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}