Virtual Environments in Python

Separating your dependencies heyylateef | Oct 27 2021

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.

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.


What is Anaconda?

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 "batteries included" developer environment for data scientists.



Installation

On your PC, go to https://www.anaconda.com/products/individual and download the installer for your system. Go ahead and continue through the installer using the recommended choices.


Basic Usage

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.

(base) MBP:~ lateefa$ 

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:

conda list

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.



Preferred Usage

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.

In your terminal, run the following command:

conda create --name mynewpyenv python=3.9

The following output will appear, showing all the packages you''ll be installing into the new environment:

The following NEW packages will be INSTALLED:
  ca-certificates    pkgs/main/osx-64::ca-certificates-2021.10.26-hecd8cb5_2
  certifi            pkgs/main/osx-64::certifi-2021.10.8-py39hecd8cb5_0
  libcxx             pkgs/main/osx-64::libcxx-12.0.0-h2f01273_0
  libffi             pkgs/main/osx-64::libffi-3.3-hb1e8313_2
  ncurses            pkgs/main/osx-64::ncurses-6.3-hca72f7f_0
  openssl            pkgs/main/osx-64::openssl-1.1.1l-h9ed2024_0
  pip                pkgs/main/osx-64::pip-21.2.4-py39hecd8cb5_0
  python             pkgs/main/osx-64::python-3.9.7-h88f2d9e_1
  readline           pkgs/main/osx-64::readline-8.1-h9ed2024_0
  setuptools         pkgs/main/osx-64::setuptools-58.0.4-py39hecd8cb5_0
  sqlite             pkgs/main/osx-64::sqlite-3.36.0-hce871da_0
  tk                 pkgs/main/osx-64::tk-8.6.11-h7bc2e8c_0
  tzdata             pkgs/main/noarch::tzdata-2021e-hda174b7_0
  wheel              pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1
  xz                 pkgs/main/osx-64::xz-5.2.5-h1de35cc_0
  zlib               pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3

Type "Y" and hit enter to accept and download the packages. Now, lets activate the environment. In the same terminal, enter the following command:

conda activate mynewpyenv

You should see the terminal session activate the new environment like so:

(mynewpyenv) MBP:~ lateefa$ 

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 for our python projects! If you need to change environments, simply enter the following command:

conda deactivate

Lastly, I'd recommend that you download the Conda Cheatsheet. 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.


So...Conda vs. Pipenv vs. Virtualenv

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? 

Overall, use whichever package manager you like, I simply just prefer Anaconda/Conda.

About The Lab

Like the content posted on this site? Need help with another web project? Want to give some feedback? Feel free to contact me via email or social media!

Know more!
DigitalOcean Referral Badge