{"id":625,"date":"2016-08-04T17:09:24","date_gmt":"2016-08-04T17:09:24","guid":{"rendered":"https:\/\/aciref.rc.fas.harvard.edu\/?p=625"},"modified":"2016-08-04T17:09:24","modified_gmt":"2016-08-04T17:09:24","slug":"a-short-note-on-the-python-virtual-environment","status":"publish","type":"post","link":"https:\/\/aciref.chpc.utah.edu\/?p=625","title":{"rendered":"A short note on the Python Virtual Environment"},"content":{"rendered":"<p>\t\t\t\tIn this post we will discuss the concept of a Virtual Environment in Python.<br \/>\nWe will discuss the following topics:<\/p>\n<ul>\n<li>What is a Virtual Environment (VE)<\/li>\n<li>Setting up a VE<\/li>\n<li>Activation of a VE<\/li>\n<li>Installation of packages within the VE<\/li>\n<li>Deactivation of a VE<\/li>\n<li>Removal of a VE<\/li>\n<\/ul>\n<p><strong>What is a virtual environment?<\/strong><br \/>\nA Python virtual environment is a child\/offshoot of a parent python distribution which allows you to use the packages in the parent distribution as well as to install packages that are only visible to the child distribution (cfr. inheritance in OO).<\/p>\n<p><strong>Setting-up the virtual environment<\/strong><br \/>\nIn the first step, we load the parent python distribution in our environment.<br \/>\nThe parent python distribution (in this example) is python 3.3.6 (built from source).<br \/>\nWithin the parent python distribution an array of packages have been installed: numpy, scipy, matplotlib, etc.<\/p>\n<p><code><span style=\"color: #009933\">[u0253283@dirac ~]$ module load python\/3.3.6\u00a0\u00a0 # Loading python 3.3.6 using LMOD<br \/>\n[u0253283@dirac ~]$ which python3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Check the executable<br \/>\n\/uufs\/chpc.utah.edu\/sys\/installdir\/python\/3.3.6\/bin\/python3<\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">[u0253283@dirac ~]$ python3 --version\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Version of python3<br \/>\nPython 3.3.6<\/span><\/code><\/p>\n<p>The parent python distribution contains the <em>pyvenv<\/em> executable which is used to create the virtual environment:<br \/>\n<code><span style=\"color: #009933;\">[u0253283@dirac ~]$ which pyvenv\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Check the version of pyvenv<br \/>\n\/uufs\/chpc.utah.edu\/sys\/installdir\/python\/3.3.6\/bin\/pyvenv<\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">pyvenv --system-site-packages ~\/MyVenv<\/span><\/code><br \/>\nThe newly created virtual environment resides in the directory $HOME\/MyVenv. The flag &#8216;&#8211;system-site-packages&#8217; grants access to the parent site-packages dir.<\/p>\n<p><strong>Activation of the virtual environment<\/strong><br \/>\nIn order to use the virtual environment, we first need to unload the parent python distribution. In a subsequent step, we need to activate the new virtual environment.<br \/>\n<code><span style=\"color: #009933;\">module unload python\/3.3.6\u00a0\u00a0 # Unload the parent distribution <\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">cd ~\/MyVenv<br \/>\nsource bin\/activate<br \/>\n[u0253283@dirac MyVenv]$ source bin\/activate<br \/>\n(MyVenv) [u0253283@dirac MyVenv]$<\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">(MyVenv) [u0253283@dirac MyVenv]$ which python3\u00a0 # Check the distribution<br \/>\n~\/MyVenv\/bin\/python3<\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">(MyVenv) [u0253283@dirac MyVenv]$ python3 --version<br \/>\nPython 3.3.6<\/span><\/code><\/p>\n<p>In the following snippet of code (vide infra) we check whether the child distribution can indeed use the numpy functionality from the parent distribution:<br \/>\n<code><span style=\"color: #009933;\">python3<br \/>\n&gt;&gt;&gt;import numpy as np<br \/>\n&gt;&gt;&gt;np.__path__<br \/>\n['\/uufs\/chpc.utah.edu\/sys\/installdir\/python\/3.3.6\/lib\/python3.3\/site-packages\/numpy']<\/span><\/code><\/p>\n<p><code><span style=\"color: #009933;\">&gt;&gt;&gt;a = np.arange(10)<br \/>\n&gt;&gt;&gt;a**3<br \/>\narray([\u00a0 0,\u00a0\u00a0 1,\u00a0\u00a0 8,\u00a0 27,\u00a0 64, 125, 216, 343, 512, 729])<\/span><\/code><\/p>\n<p><strong>Installation of packages within the virtual environment<\/strong><\/p>\n<p>In this section we explain how to install packages in the newly created virtual environment. We will install 2 packages using &#8216;<em>pip install<\/em>&#8216; (Example 1) and &#8216;<em>setup.py<\/em>&#8216; (Example 2).<\/p>\n<p>Note that the pip executable is NOT installed in the newly created ~\/MyVenv\/bin directory.<br \/>\nHowever, the pip executable can be easily installed in the Virtual Environment as follows:<br \/>\n<code><span style=\"color: #009933;\">(MyVenv) [u0253283@dirac ~]$ python3 -m pip install -U pip <\/span><\/code><\/p>\n<p>We will now show how to do some simple installations in our virtual environment:<\/p>\n<ul>\n<li>Example 1: Installation of qit using pip:<br \/>\n<code><span style=\"color: #009933;\">(MyVenv) [u0253283@dirac ~]$ pip install qit<br \/>\n(MyVenv) [u0253283@dirac ~]$ python3<br \/>\nPython 3.3.6 (default, Jan 20 2015, 11:39:02)<br \/>\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux<br \/>\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.<br \/>\n&gt;&gt;&gt; import qit<br \/>\n&gt;&gt;&gt; qit.__path__<br \/>\n['\/uufs\/chpc.utah.edu\/common\/home\/u0253283\/MyVenv\/lib\/python3.3\/site-packages\/qit']<\/span><\/code><\/li>\n<li>Example 2: Installation of sunpy using setup.py<br \/>\n# Requirements:<\/p>\n<ul>\n<li>* [Python](http:\/\/www.python.org)<\/li>\n<li>* [Astropy](http:\/\/astropy.org) (1.0.0)<\/li>\n<li>* [NumPy](http:\/\/numpy.scipy.org\/)<\/li>\n<li>* [SciPy](http:\/\/www.scipy.org\/)<\/li>\n<li>* [Matplotlib](http:\/\/matplotlib.sourceforge.net\/) (1.1+)<\/li>\n<li>* [suds-jurko](https:\/\/bitbucket.org\/jurko\/suds)<\/li>\n<li>* [pandas](http:\/\/pandas.pydata.org\/) (0.10.0+)<\/li>\n<li>* [beautifulsoup4](http:\/\/www.crummy.com\/software\/BeautifulSoup\/)<\/li>\n<li>* [sqlalchemy](http:\/\/www.sqlalchemy.org\/)<\/li>\n<\/ul>\n<p><code><span style=\"color: #009933;\">module load git<br \/>\ncd ~<br \/>\ngit clone https:\/\/github.com\/sunpy\/sunpy.git sunpy_src<br \/>\ncd sunpy_src<br \/>\npython3 setup.py build<br \/>\npython3 setup.py install<br \/>\ncd ~ ; rm -rf sunpy_src <\/span><\/code><\/p>\n<p>Note that we are not required to use the &#8211;prefix flag, because<br \/>\nthe user has by default access to the subdirectories within the virtual<br \/>\nenvironment.<\/p>\n<p><code><span style=\"color: #009933;\"># Test of the installation:<br \/>\npython3<br \/>\n&gt;&gt;&gt; import sunpy<br \/>\n&gt;&gt;&gt; sunpy.__path__<br \/>\n['\/uufs\/chpc.utah.edu\/common\/home\/u0253283\/MyVenv\/lib\/python3.3\/site-packages\/sunpy-0.8.dev7032-py3.3.egg\/sunpy']<br \/>\n&gt;&gt;&gt; import sunpy.data<br \/>\n&gt;&gt;&gt; sunpy.data.download_sample_data()<br \/>\n&gt;&gt;&gt; import sunpy.data.sample<br \/>\n&gt;&gt;&gt; import sunpy.map<br \/>\n&gt;&gt;&gt; aia = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE)<br \/>\n&gt;&gt;&gt; aia.peek()<\/span><\/code><\/li>\n<\/ul>\n<p><strong>Deactivation of the Virtual Environment<\/strong><br \/>\nWe can leave the Virtual Environment easily by invoking &#8216;<em>deactivate<\/em>&#8216;:<br \/>\n<code><span style=\"color: #009933;\">(MyVenv) [u0253283@dirac ~]$ deactivate<br \/>\n[u0253283@dirac ~]$<\/span><\/code><\/p>\n<p><strong>Removal of the Virtual Environment<\/strong><br \/>\nWe can get rid of the Virtual Environment by deleting the directory:<br \/>\n<code><span style=\"color: #009933;\">[u0253283@dirac ~]$ rm -rf ~\/MyVenv<\/span><\/code>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will discuss the concept of a Virtual Environment in Python. We will discuss the following topics: What is a Virtual Environment (VE) Setting up a VE Activation of a VE Installation of packages within the VE Deactivation of a VE Removal of a VE What is a virtual environment? A Python [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":643,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[30,37,39],"class_list":["post-625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-experience","tag-python","tag-venv","tag-virtual-environment"],"_links":{"self":[{"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=625"}],"version-history":[{"count":0,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/posts\/625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=\/wp\/v2\/media\/643"}],"wp:attachment":[{"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aciref.chpc.utah.edu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}