Setting Up Your Python Environment

Sept 24, 2019


Why use a Virtual Environment

To get setup, we are going to install a virtual environment for you to develop in. A virtual environment provides an isolated space for us to install and writing code. This is important for our class (and in general too!) to avoid differences in software versioning and to prevent overwritting important libraries used in your other projects. Further, it helps us debug your code faster and run your code on our own machines!

You need to do three things

  1. Install Miniconda (different on Mac, PC, Linux)
  2. Create a cs398 environment
  3. Install PyTorch


Installing Miniconda 3 (Mac OS)

For this course, we will be using Python3 and a conda virtual environment. If you already have Miniconda3 installed, skip to the environment creation step.

To get started, open up your terminal. We are going to download Miniconda to your home directory: cd there.

cd ~

Then, we will download the file (make sure you are connected to internet).

curl https://repo.continuum.io/miniconda/Miniconda3-4.7.10-MacOSX-x86_64.sh --output Miniconda3-4.7.10-MacOSX-x86_64.sh

icon

At this point, you should have a file named Miniconda3-4.7.10-MacOSX-x86_64.sh in your current directory. Go ahead and run it. (press q if you don't want to read all the terms and conditions.) Press yes when it asks to add conda to your ~/.bash_profile.

bash Miniconda3-4.7.10-MacOSX-x86_64.sh

icon

Once you've installed Miniconda3, you can either use the command below or restart your terminal.

source ~/.bash_profile

You should see (base) floating near the left side of your terminal input.

icon

Installing Miniconda 3 (Linux)

Instructions are identical to the MacOS ones except we download a different Miniconda. Use the following URL.

curl https://repo.continuum.io/miniconda/Miniconda3-4.7.10-Linux-x86_64.sh --output Miniconda3-4.7.10-Linux-x86_64.sh;

Installing Miniconda 3 (Windows)

Please contact Mike Wu if you are using a Windows computer and he can help!

Creating an Environment

At this point, the OS you are using should not matter any more. We are going to make a new virtual environment named cs398 and install a basic suite of Python packages. Note: this might take 10 to 20 minutes (especially the "Solving environment..." step). It will ask you to confirm that you want to install a list of packages.

conda create -n cs398 python anaconda

icon

Ok it should be installed now! When working with virtual environments, remember that when opening a new tab or terminal, you need to activate your environment. In this case, do:

conda activate cs398

You should see (cs398) floating near the left side of your terminal input instead of (base). This tells you that you have successfully activated your new environment. To stop using an environment, use conda deactivate.

icon

Installing PyTorch

Finally, the last thing we need to do is install PyTorch. With Conda, this is very easy to do. Remember to have your proper environment activated before installing!

conda install pytorch torchvision -c pytorch

As a sanity check, start up Python and try to import torch (again, make sure the cs398 environment is active). No errors should appear.

icon

Contents: