Workshop - Virtual Environments
Original Message:
Hey y'all! I am going to host a small workshop where I cover the basics of making a virtual environment, both using the traditional Python venv
as well as conda
environments. In fewer than 60 minutes, you will learn:
Why we use virtual environments
How to initialize a virtual environment in
venv
andconda
from scratchDemystify the
requirements.txt
andenvironment.yml
filesExporting/sharing an environment for others to use
Meeting: 12/6/2021 @ 5:30PM
Invite:
Ryan Henry Papetti is inviting you to a scheduled Zoom meeting.This meeting was created in a non-BAA environment and is not intended for the discussion of healthcare, health education, or health data research.Topic: ACIC Fall 2021 Virtual Environment Workshop
Time: Dec 6, 2021 05:30 PM ArizonaJoin Zoom Meeting
Join our Cloud HD Video Meeting One tap mobile
+16027530140,,83974342417# US (Phoenix)
+16699006833,,83974342417# US (San Jose)Dial by your location
+1 602 753 0140 US (Phoenix)
+1 669 900 6833 US (San Jose)
+1 253 215 8782 US (Tacoma)
+1 346 248 7799 US (Houston)
+1 646 876 9923 US (New York)
+1 301 715 8592 US (Washington DC)
+1 312 626 6799 US (Chicago)
Meeting ID: 839 7434 2417
Find your local number: Video Conferencing, Web Conferencing, Webinars, Screen Sharing Join by SIP
83974342417@zoomcrc.comJoin by H.323
162.255.37.11 (US West)
162.255.36.11 (US East)
115.114.131.7 (India Mumbai)
115.114.115.7 (India Hyderabad)
213.19.144.110 (Amsterdam Netherlands)
213.244.140.110 (Germany)
103.122.166.55 (Australia Sydney)
103.122.167.55 (Australia Melbourne)
149.137.40.110 (Singapore)
64.211.144.160 (Brazil)
149.137.68.253 (Mexico)
69.174.57.160 (Canada Toronto)
65.39.152.160 (Canada Vancouver)
207.226.132.110 (Japan Tokyo)
149.137.24.110 (Japan Osaka)
Meeting ID: 839 7434 2417Join by Skype for Business
undefined
Why We Use Virtual Environments
A totally common question (and one I had) is why do we need a virtual environment when I can just give someone my Python script(s)/repository? Two reasons why this will not work.
They likely don’t have the same dependencies that you do to run your code
If they do, they may not be right versions or compatible with their OS
One of the most embarrassing things to happen to someone who codes is to realize what they did one one thing will not work on another.
These are where virtual environments come in. In short, virtual environments are specified locations on your machine that allow you to control the libraries and packages, down to an exact version, that you are using for your code. These libraries and packages are called dependencies.
We mainly use virtual environments for two reasons:
To make it easier to reproduce exact development environments for others
To make it easier to reproduce exact development environments for yourself
We will cover how to make virtual environments in the two most common virtual environment software: Python’s own venv
and Anaconda’s conda
.
How to initialize a virtual environment in
venv
andconda
from scratch
Using venv
and conda
are not all that different from each other, minus the installation and set up.
Python’s venv
Let’s begin with venv
. All that’s required is your Python’s version is greater than 3.3. In this version, Python shipped out venv
as part of Python. How cool!
In a terminal, navigate to a directory of your choice. For this, I will name it virtual-env-demo
. Once you are in that directory, we can make a new virtual environment with the command python -m venv <YOUR NAME HERE>
. I’ll call mine myfirstvirtualenv
. Afterwards, enter that directory. Peep the screenshot below.
Now that we are in the virtual environment’s directory, it is time for us to activate it. We activate this environment by, in the directory named after the virtual environment, run source bin/activate
. This runs a bash command that activates the environment. Peep the screenshot below.
Once the name of the virtual environment is on the left-side in parentheses, you have activated your environment. What does this mean?
Any time you run
pip install
, it will ONLY install in this environment (at the spot of creation)It also means you have NO access to previous python packages or libraries you have installed before.
For this assignment, let’s just install most recent versions of seaborn
, matplotlib
, numpy
, and pandas
Notice the versions at the bottom. We will get back to those.
But congrats! You made a virtual environment using venv
. Let’s deactivate and move onto conda
. To deactivate your virtual environment, just type deactivate
.
Using conda
The conda
virtual environment manager comes from a larger service known as Anaconda
. For this I will assume you have never used conda
before. In order to install conda
, here is what I would do. Go to Miniconda - Anaconda Installing conda — conda 25.5.2.dev17 documentation and see if there is ample documentation. For certain Unix OS, you can do the following:
Go to your home directory
Run
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
to downloadRun
bash Miniconda3-latest-Linux-x86_64.sh
to run the Anaconda installer. There is a path at the bottom of your installation. NOTE THAT.Export the path of the conda install into
/.bash_profile
Run
conda init bash
Open a new terminal. If you see
base
on the left side in parentheses, you did it!If you do not see it, try typing
conda activate
Example of conda activate
When you see (base) you are in the base environment (go figure). Let’s make a new one called myfirstcondaenv
. To do so, the command is conda create -n myfirstcondaenv
. Once you accept and finish the installation, go ahead and activate your environment with conda activate myfirstcondaenv
. Peep the screenshot below.
Demystify the
requirements.txt
andenvironment.yml
files