{"id":148410,"date":"2023-10-20T10:46:50","date_gmt":"2023-10-20T14:46:50","guid":{"rendered":"http:\/\/www.bu.edu\/tech\/?page_id=148410"},"modified":"2024-11-26T10:52:09","modified_gmt":"2024-11-26T15:52:09","slug":"virtualenv","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/python-installs\/virtualenv\/","title":{"rendered":"Installs with Python"},"content":{"rendered":"<p><a name=\"top\"><\/a><br \/>\n<strong>Sections<\/strong><\/p>\n<ul>\n<li><a href=\"#venv\">Using Virtual Environments<\/a>\n<ul>\n<li><a href=\"#create\">Create virtualenv<\/a><\/li>\n<li><a href=\"#activate\">Activate virtualenv<\/a><\/li>\n<li><a href=\"#install\">Install into virtualenv<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#export\">Exporting and Distributing Virtual Environments<\/a><\/li>\n<li><a href=\"#using\">Using the &#8211;system-site-packages flag<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a name=\"venv\"><\/a><\/p>\n<h2>Using Virtual Environments<\/h2>\n<div class=\"highlight-yellow\"><strong>Note<\/strong>: The recommended location for virtualenvs is in the <code>\/projectnb<\/code> space of any project. The home directory is not recommended due to <span>strict space and file number quotas<\/span>.<\/div>\n<p><a name=\"create\"><\/a><\/p>\n<h3>Create virtualenv<\/h3>\n<p>Load the Python module version you wish to use and create a new virtual environment named <code><b>mynewnev<\/b><\/code> in your <code>\/projectnb<\/code> space.<\/p>\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load python3\/3.10.12<\/span>\r\n<span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">virtualenv<\/span> <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv<\/span>\r\n<span class=\"output\">New python executable in  <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv\/<\/span>bin\/python<\/span>\r\n<span class=\"output\">Installing setuptools, pip, wheel...done.<\/span><\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><a name=\"activate\"><\/a><\/p>\n<h3>Activate virtualenv<\/h3>\n<p>Activate <code><b>mynewev<\/b><\/code>. Note that once activated, the command prompt begins with <code>(mynewenv)<\/code> signifying you are inside the virtual environment.<\/p>\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">source<\/span> <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv<\/span>\/bin\/activate\r\n<span class=\"prompt\">(mynewenv) [rcs@scc1 ~]<\/span> <span class=\"command\"><\/span><\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><a name=\"install\"><\/a><\/p>\n<h3>Install into virtualenv<\/h3>\n<p>Install the Python package of interest into your virtualenv. In this example, we are installing the <a href=\"https:\/\/htseq.readthedocs.io\/en\/latest\/\">HTSeq<\/a> into <code><b>mynewenv<\/b><\/code>.<\/p>\n<pre><code class=\"code-block\"><span class=\"prompt\">(mynewenv) [rcs@scc1 ~]<\/span> <span class=\"command\">pip install htseq<\/span>\r\n<span class=\"output\">Collecting htseq\r\nCollecting numpy\r\nCollecting pysam\r\nInstalling collected packages: numpy, pysam, htseq\r\nSuccessfully installed htseq-0.12.4 numpy-1.19.1 pysam-0.16.0.1<\/span><span class=\"output\"><\/span><\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2><a name=\"export\"><\/a>Exporting and Distributing Virtual Environments<\/h2>\n<p>It may be useful to export an existing virtualenv to another workspace or share with others outside SCC Project (a.k.a. Unix group). If you would like to export or share the virtualenv with collaborators that cannot be given Unix group-read\/execute permissions, follow the instructions below.<\/p>\n<h4>To be followed by the user exporting the virtualenv.<\/h4>\n<ol>\n<li>Load a Python module for the version you wish to use.\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load python3\/3.10.12<\/span>\r\n<span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">source <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv\/<\/span>bin\/activate<\/span><\/code><\/pre>\n<\/li>\n<li>Export list of packages via <code><b>pip<\/b><\/code> into a text document named <b>requirements.txt<\/b>. Some example packages in this environment are HTSeq, Numpy, and Pysam.\n<pre><code class=\"code-block\"><span class=\"prompt\">(mynewenv) [rcs@scc1 ~]<\/span> <span class=\"command\">pip freeze --local &gt; requirements.txt<\/span>\r\n<span class=\"prompt\">(mynewenv) [rcs@scc1 ~]<\/span> <span class=\"command\">cat requirements.txt<\/span>\r\n<span class=\"output\">HTSeq==0.12.4\r\nnumpy==1.19.1\r\npysam==0.16.0.1\r\n...<\/span><\/code><\/pre>\n<\/li>\n<li>Deactivate the current virtual environment.\n<pre><code class=\"code-block\"><span class=\"prompt\">(mynewenv) [rcs@scc1 ~]<\/span> <span class=\"command\">deactivate<\/span>\r\n<span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\"><\/span><\/code><\/pre>\n<\/li>\n<\/ol>\n<h4>To be followed by the user re-creating the exported virtualenv.<\/h4>\n<ol>\n<li>Build a new virtualenv called <code><b>mynewenv_copy<\/b><\/code> and activate it.\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">virtualenv &lt;\/span <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv_copy<\/span>\r\n<span class=\"output\">New python executable in <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv_copy\/<\/span>bin\/python<\/span>\r\n<span class=\"output\">Installing setuptools, pip, wheel...done.<\/span>\r\n<span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">source <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewnev_copy\/<\/span>bin\/activate<\/span>\r\n<span class=\"prompt\">(mynewnev_copy) [rcs@scc1 ~]<\/span> <span class=\"command\"><\/span><\/span><\/code><\/pre>\n<\/li>\n<li>Install everything from the <b>requirements.txt<\/b> file.\n<pre><code class=\"code-block\"><span class=\"prompt\">(mynewnev_copy) [rcs@scc1 ~]<\/span> <span class=\"command\">pip install -r requirements.txt<\/span>\r\n<span class=\"output\">Collecting htseq\r\nCollecting numpy\r\nCollecting pysam\r\nInstalling collected packages: numpy, pysam, htseq\r\nSuccessfully installed htseq-0.12.4 numpy-1.19.1 pysam-0.16.0.1<\/span><\/code><\/pre>\n<\/li>\n<li>Test that the expected packages were installed and work in the new virtualenv. In this example, we are specifically testing the HTSeq package.\n<pre><code class=\"code-block\"><span class=\"prompt\">(mynewnev_copy) [rcs@scc1 ~]<\/span> <span class=\"command\">which htseq-count<\/span>\r\n<span class=\"output\"><span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mysecondenv\/<\/span>bin\/htseq-count<\/span>\r\n<span class=\"prompt\">(mynewnev_copy) [rcs@scc1 ~]<\/span> <span class=\"command\">python<\/span>\r\n<span class=\"output\">Python 3.10.12 (default, May 21 2023, 14:57:43) \r\n[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.<\/span>\r\n<span class=\"prompt\">&gt;&gt;<\/span> <span class=\"command\">import HTSeq<\/span><\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a name=\"using\"><\/a><\/p>\n<h2>Using the &#8211;system-site-packages flag<\/h2>\n<p>When creating a virtualenv, you can add the <code><b>--system-site-packages<\/b><\/code> flag to have the existing Python packages in the SCC Python module also installed in your new virtualenv. This may be beneficial as this will automatically install many of the commonly used Python packages (NumPy, SciPy, etc.) into your virtualenv. If you would like to create an empty virtualenv, skip this option.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Load the Python module for the version you wish to use.\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load python3\/3.10.12<\/span><\/code><\/pre>\n<\/li>\n<li>Create a virtualenv named <code><b>mynewenv_pack<\/b><\/code> in your \/projectnb space.\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">virtualenv --system-site-package<\/span> <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv_pack<\/span>\r\n<span class=\"output\">New python executable in <span class=\"placeholder\">\/projectnb\/yourprojectname\/venvs\/mynewenv_pack\/<\/span>bin\/python<\/span>\r\n<span class=\"output\">Installing setuptools, pip, wheel...done.<\/span><\/code><\/pre>\n<\/li>\n<li>Add Jupyter and\/or Spyder if you need them. These need to be reinstalled into the virtualenv if you are using the <code>--system-site-package<\/code> option for them to work as expected.\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~] <\/span><span class=\"command\">source mynewenv_pack\/bin\/activate<\/span>\r\n<span class=\"prompt\">(<span class=\"placeholder\">mynewenv_pack<\/span>)[rcs@scc1 ~]<\/span> pip install --force jupyter notebook spyder<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a href=\"#top\">Back to top<\/a><\/p>\n<div style=\"float: right;\">\n<p id=\"last-modified-timestamp\" style=\"margin: 0;\">Last updated: Loading&#8230;<\/p>\n<\/div>\n<p><script>\r\ndocument.addEventListener('DOMContentLoaded', function() {\r\n    \/\/ Get the content of the meta tag\r\n    var lastUpdatedContent = document.querySelector('meta[name=\"last-updated\"]').content;\r\n    \r\n    \/\/ Parse the content into a Date object\r\n    var lastUpdatedDate = new Date(lastUpdatedContent);\r\n    \r\n    \/\/ Format the date\r\n    var formattedDate = lastUpdatedDate.toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric' });\r\n    \r\n    \/\/ Update the HTML element with the formatted date\r\n    document.getElementById('last-modified-timestamp').innerHTML = 'Last updated: ' + formattedDate;\r\n});\r\n<\/script><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Sections Using Virtual Environments Create virtualenv Activate virtualenv Install into virtualenv Exporting and Distributing Virtual Environments Using the &#8211;system-site-packages flag &nbsp; Using Virtual Environments Note: The recommended location for virtualenvs is in the \/projectnb space of any project. The home directory is not recommended due to strict space and file number quotas. Create virtualenv Load&#8230;<\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":147620,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148410"}],"collection":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/comments?post=148410"}],"version-history":[{"count":33,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148410\/revisions"}],"predecessor-version":[{"id":155062,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148410\/revisions\/155062"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/147620"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/media?parent=148410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}