Virtual Environments in Python with Anaconda
Tags: webdev, python, beginners, productivity
Published: 2022-05-23 19:12:00

<p></p><p><span style="font-size: 18px;">﻿</span><span style="font-size: 18px;">﻿</span><span style="font-size: 18px;">Writing python code on your local system is fun....Until its time to deploy your project and you find yourself scratching your head wondering why your project contains too many dependancies.</span></p><p><span style="font-size: 18px;">In tutorial, we're going to setup Anaconda, a popular data science toolkit that contains a package manager (conda) and integrates with a lot of popular applications that many python developers use.</span></p><hr><p></p><p><span style="font-size: 24px;">﻿<b>What is Anaconda?</b></span></p><p><span style="font-size: 18px;">﻿Anaconda is a popular, open-source software toolkit that contains many of the tools for the modern day data scientist. Many data scientists consume and analyze data using Python and R programming languages. The purpose of the toolkit is to be the <a href="https://en.wikipedia.org/wiki/Batteries_Included#:~:text=Motto%20of%20the%20Python%20programming,parts%20required%20for%20full%20usability" target="_blank">"batteries included"</a> developer environment for data scientists.</span></p><p></p><hr><p></p><p><span style="font-size: 18px;"><br></span><span style="font-size: 24px;">﻿<b>Installation</b></span></p><p><span style="font-size: 18px;">﻿On your PC, go to&nbsp;<a href="https://www.anaconda.com/products/individual" target="_blank">https://www.anaconda.com/products/individual</a>&nbsp;and download the installer for your system. Go ahead and continue through the installer using the recommended choices.</span></p><p><img src="/media/django-summernote/2021-11-01/9ea4a959-e587-4cce-9d86-743ea1819647.png" style="width: 698px;"></p><p></p><hr><p></p><p><span style="font-size: 18px;">﻿</span><span style="font-size: 24px;">﻿<b>Basic</b>&nbsp;<b>Usage</b></span></p><p><span style="font-size: 18px;">﻿Once installed, if you're on Mac, open terminal. If you're Windows, browse to your programs and open "Anaconda Prompt". You'll see the current environment opened in your new terminal session.</span></p><p></p><pre><p class="p1" style="margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0); white-space: normal;"><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span style="font-size: 14px;">(base) MBP:~ lateefa$</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp;</span></span><span style="font-size: 18px;">﻿</span></p></pre><p></p><p></p><p><span style="font-size: 18px;">﻿Great, our new terminal session is using conda's base environment, lets check out the packages installed in the base environment. In your terminal session, run the following command:</span></p><pre><span style="font-size: 14px;">﻿conda list</span></pre><p><span style="font-size: 18px;">A lot (almost overwhelming) amount of packages are in the base environment. That is a good thing though! For a lot of python scripting and other projects, you have a lot of the necessary packages needed to get to testing.</span></p><p><br></p><hr><p><b style="font-size: 24px;">Preferred Usage</b><br></p><p><span style="font-size: 18px;">﻿Using the base environment is fine for testing, but for most python projects (especially Django) you don't need those extra packages. So lets create another environment using the base package manager, conda! For many Python developers, we may just need to start out with Python (and its standard dependancies) for our new environment. Lets use the conda package manager to create new environment that contains the latest version of Python 3.9 and its standard packages.</span></p><p><span style="font-size: 18px;">In your terminal, run the following command:</span></p><pre><span style="font-size: 14px;">﻿conda create --name mynewpyenv python=3.9</span><span style="font-size: 18px;"><br></span></pre><p><span style="font-size: 18px;">The following output will appear, showing all the packages you''ll be installing into the new environment:</span></p><pre style="margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="s1" style="font-variant-ligatures: no-common-ligatures; font-size: 14px;">The following NEW packages will be INSTALLED:</span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">ca-certificates</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::ca-certificates-2021.10.26-hecd8cb5_2</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">certifi</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::certifi-2021.10.8-py39hecd8cb5_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">libcxx </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::libcxx-12.0.0-h2f01273_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">libffi </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::libffi-3.3-hb1e8313_2</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">ncurses</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::ncurses-6.3-hca72f7f_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">openssl</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::openssl-1.1.1l-h9ed2024_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">pip</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::pip-21.2.4-py39hecd8cb5_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">python </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::python-3.9.7-h88f2d9e_1</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">readline </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::readline-8.1-h9ed2024_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">setuptools </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::setuptools-58.0.4-py39hecd8cb5_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">sqlite </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::sqlite-3.36.0-hce871da_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">tk </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::tk-8.6.11-h7bc2e8c_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">tzdata </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/noarch::tzdata-2021e-hda174b7_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">wheel</span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">xz </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::xz-5.2.5-h1de35cc_0</span></span><br><span class="s1" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; </span><span style="font-size: 14px;">zlib </span><span class="Apple-converted-space" style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size: 14px;">pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3</span></span></pre><p class="p2" style="margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span class="s1" style="font-variant-ligatures: no-common-ligatures;"></span></p><p><span style="font-size: 18px;">Type "Y" and hit enter to accept and download the packages. Now, lets activate the environment. In the same terminal, enter the following command:</span></p><pre><span style="font-size: 14px;">﻿conda activate mynewpyenv</span><span style="font-size: 18px;"><br></span></pre><p><span style="font-size: 18px;">You should see the terminal session activate the new environment like so:</span></p><p></p><pre><span style="font-size: 14px;">﻿</span>(mynewpyenv) MBP:~ lateefa$<span class="Apple-converted-space" style="font-variant-ligatures: no-common-ligatures; font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);">&nbsp;</span></pre><p></p><p><span style="font-size: 18px;">﻿Perfect, now we can use pip to install packages such as django, gunicorn, whitenoise, etc. Also, we can use pip to generate the requirements.txt for our project so we can keep up with the dependancies&nbsp;for our python projects! If you need to change environments, simply enter the following command:</span></p><pre><span style="font-size: 14px;">﻿conda deactivate</span></pre><p><span style="font-size: 18px;">Lastly, I'd recommend&nbsp;that you download the </span><a href="https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf" target="_blank" style="font-size: 18px;">Conda Cheatsheet</a><span style="font-size: 18px;">. It holds the most useful conda commands in a 2 page document. I personally find myself referring to it every time I want to start/manage a new environment.</span></p><hr><p><span style="font-size: 24px;">﻿<b>So...Conda vs. Pipenv vs. Virtualenv</b></span></p><p><span style="font-size: 14px;">﻿</span><span style="font-size: 18px;">﻿Honestly, I don't have anything against using Pipenv or Virtualenv, I just began using Conda/Anaconda in my early days of a developer and started to use it whenever I setup new PCs for development/deployment as a huge feature of Anaconda is it installs Python on the computer. Sure that only saves me about 5-10 minutes, but why not use the toolkit that installs it for me?&nbsp;</span></p><p><span style="font-size: 18px;">Overall, use whichever package manager you like, I simply just prefer Anaconda/Conda.</span><br></p><p></p>