{"id":4439,"date":"2020-01-03T17:54:16","date_gmt":"2020-01-03T17:54:16","guid":{"rendered":"https:\/\/blog.ssdnodes.com\/blog\/?p=4439"},"modified":"2025-05-18T12:39:17","modified_gmt":"2025-05-18T12:39:17","slug":"ml-1","status":"publish","type":"post","link":"https:\/\/www.ssdnodes.com\/blog\/ml-1\/","title":{"rendered":"Machine Learning Setup &#8211; Part 1: Python Virtual Environments"},"content":{"rendered":"<h2><span style=\"font-weight: 400;\">Introduction<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Before you get started with your journey of Machine Learning, it is important to understand the underlying technology that you will be using. In most cases, this will involve using TensorFlow with Python.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ML workloads are quite compute intensive, this is why TensorFlow is written in C++. The Python interface is meant for you to use TensorFlow more effectively and easily without getting bogged down by C++ implementations details. Similar interfaces exist for JavaScript and a few other languages.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We will be using Python for our setup. Also, TensorFlow comes with a GPU specific implementation meant to take advantage of Nvidia GPUs, to keep things simple we will ignore that as well.<\/span><\/p>\n<h2>SSDNodes TensorFlow Template!<\/h2>\n<p><span style=\"font-weight: 400;\">If you are already comfortable with it, or want to jump straight into running Python snippets, try out one of our TensorFlow templates at <a href=\"https:\/\/www.ssdnodes.com\/\">SSDNodes<\/a> which will get you started with a Ubuntu 18.04 environment preconfigured with Jupyter Labs and TensorFlow. There are a lot of advantages in offloading your ML workload to our cloud, a few include:\u00a0\u00a0<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\">Blazing fast internet connection to download huge sets of training data.<\/span><\/li>\n<li>Tonnes of memory and compute at low low prices! It will burn through the data before you are done with your coffee break.<\/li>\n<li>Freedom to break things. If you screwup, just click reinstall and you have a clean slate to start over again!<\/li>\n<\/ol>\n<h2><span style=\"font-weight: 400;\">Python and its packaging problem<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Python is one of the easiest languages to pickup, with readable syntax and projects like TensorFlow that have real world use cases you would be hard pressed to find a better language to begin your journey into computing.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, some of its success has resulted in a lot of similar projects and confusion surrounding them. Some unintended consequences. Below are a few:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Incompatible versions like Python 3 and Python 2<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Multiple revisions of Python 3 and the differences between them.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Setups having multiple installations of Python. For example, from Anaconda, miniconda, base OS, etc.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Differently named package managers, e.g, pip and pip3.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Obsolete versions of pip being shipped by OS vendors.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">In this post, we would explore the optimum balance between stability and having the most cutting edge version of Python\/Pip possible.\u00a0<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">This is tested on Ubuntu 18.04 &amp; Debian 10 but similar principles can be applied to other Linux distros.\u00a0\u00a0<\/span><\/p><\/blockquote>\n<h2><span style=\"font-weight: 400;\">Virtual Environments<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">To begin with, we will stick to the version Python3 that gets shipped with your OS. In case of Ubuntu 18.04, this is going to be version 3.6 at the time of this writing. Next we need to minimize our footprint on system-wide installations. We use Virtual Environments for this.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Virtual environments help us install Pip, and other Python packages inside a single local directory, and easily\u00a0 use it for our scripts, instead of cluttering your entire system with unknown\/conflicting packages.\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Like everything in Python community there are a myriad of implementations of this idea like virtualenv, pyenv, venv. But we will just use venv, which is officially supported and very easy to use.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To achieve this we will need only one extra package. On Debian-based systems this would be <code>python3-venv<\/code> and we won\u2019t need any version of system-wide pip, TensorFlow or any other Python specific package.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><code>$ sudo apt-install python3-venv<\/code><\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Using Virtual Environments<\/span><\/h2>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">To <\/span><b>create <\/b><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a virtual environment, pick a name for your virtual environment, like <code>newenv<\/code>, and simply use the command:<br \/>\n<\/span><\/span><code>$ python3 -m venv newenv<\/code><br \/>\nThis command will create a directory <code>newenv<\/code> inside your current directory. I usually create one inside the directory of my project, but you can create it anywhere, and use it anywhere.<\/li>\n<li style=\"font-weight: 400;\">To <b>activate <\/b>it, simply run:<br \/>\n<code>$ source .\/newenv\/bin\/source<br \/>\n<\/code>For Windows and other Operating systems the command might be slightly different, but the effect will be still the same. The prompt will change to show you are in a different environment, i.e, <code>(newenv) $<br \/>\n<\/code>Once that is done, you can start using it.<\/li>\n<li style=\"font-weight: 400;\">Now you can <b>use<\/b>\u00a0it, you will notice that now you have pip for managing Python packages, even if you don\u2019t have it installed system-wide:<br \/>\n<code>(newenv)$ pip3 --version<\/code><br \/>\n<code>pip 18.1 from \/home\/r\/newenv\/lib\/python3.7\/site-packages\/pip (python 3.7)<br \/>\n<\/code><\/li>\n<li style=\"font-weight: 400;\">You can upgrade pip, install TensorFlow and install TensorFlow as:<br \/>\n<code>(newenv)$ python3 -m pip --upgrade install pip<\/code><br \/>\n<code>(newenv)$ pip install tensorflow jupyterlab\u00a0<\/code><br \/>\nWith the virtual environment activated you can Python scripts using the simple <code>python yourscript.py<\/code> and they will make use of packages inside the <code>newenv<\/code> folder.<\/li>\n<li style=\"font-weight: 400;\">And once you are done with it, you can simply <code>deactivate<\/code> the environment:<br \/>\n<code>(newenv)$ deactivate<br \/>\n<\/code>Delete the <code>newenv<\/code> directory to get rid of all the libraries, pacakges and other cruft that was installed.<\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400;\">Tip<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Before deleting a virtual environment, activate it and run <code>$ pip freeze &gt; requirements.txt<\/code> and grab the list of all the packages (with proper version number) into requirements.txt file.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Later can install the packages into a new environment by simply running:<br \/>\n<code>(newenv)$ pip install -r requirements.txt<\/code><br \/>\nT<\/span><span style=\"font-weight: 400;\">his would prevent your application from breaking because of any significant changes in the packages or their API.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Conclusion<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The name of the game is to type less, get organized, and get more done. Everything from requirements.txt to virtual environments helps you get rid of unnecessary setup. Learn about your setup only once and rinse and repeat!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Before you get started with your journey of Machine Learning, it is important to understand the underlying technology that you will be using. In most cases, this will involve using TensorFlow with Python. ML workloads are quite compute intensive, this is why TensorFlow is written in C++. The Python interface is meant for you  &#8230;<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18],"tags":[213],"class_list":["post-4439","post","type-post","status-publish","format-standard","hentry","category-devops","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/4439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/comments?post=4439"}],"version-history":[{"count":2,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/4439\/revisions"}],"predecessor-version":[{"id":12993,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/4439\/revisions\/12993"}],"wp:attachment":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media?parent=4439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/categories?post=4439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/tags?post=4439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}