Module 3. Setting up Python on your PC

We are going to need multiple tools for the workshop. This module guides you through installing all of them. Do not worry if you do not understand all the following steps and concepts. You will get familiar with it soon enough.

Installing Anaconda

Anaconda is a package manager (a program that helps you install all the things you need, and that keeps it organized) which is made for Python. To install Anaconda, do the following:

  • Download the Python 3.7 version installer suitable for your operating system from https://www.anaconda.com/download/
  • If you are using a Windows computer, you will need to know if your computer is built on a 32-bit or 64-bit architecture. You can find this in the About your PC section of System Settings. If you are uncertain how to find this, you can follow these instructions.
  • If you are using a Mac made after 2007 you will have a 64-bit architecture.
  • Run the installer.

Setting up a virtual environment

Different projects might need to use different versions of Python. We will, therefore, set up a “virtual environment” that we can use for executing Python code. This will also enable us to access the packages we want and ensure they do not conflict.

  • Launch Anaconda-Navigator
  • You will find the Anaconda-Navigator in Launchpad on a Mac and in the start menu in Windows.
  • Go to the Environments tab on the left-hand side of the screen.

  • Click the Create button at the bottom of the screen. The dialogue seen in the following figure will appear:
  • Give the environment a name of your choosing (for example phys_py37).
  • Select the check-box for Python and select 3.7 from the dropdown. This is the version of Python we are going to use.
  • Ensure R is unchecked. R is an entirely separate programming language focussed on statistics applications.
  • Select Create.

You have now made your own environment. It may appear below your “base (root)” environment, which is the global environment on your computer. Other applications may be using this global environment, so if we install packages here, problems may arise. By keeping a separate virtual environment, you ensure this does not happen.

Installing packages

The functionality of programming languages is often separated into libraries. In Python, these libraries are called packages

that we can download and access to make our work easier.

Using the Conda package manager

 The package manager in Anaconda is called Conda. We can now install the packages we need in our newly made environment using Conda. Do as follows:

  • Ensure the environment we just created is active.
  • It should be highlighted in the Environments inspector as seen in the picture below.
  • If it is not active, click on the environment to activate it. Changing environments can take some seconds, so be patient.

  • Select Not installed from the dropdown to show all the packages Anaconda knows about that are not installed in your environment already.
  • Search for matplotlib. When it appears, select the checkbox for the package and hit Apply. If you cannot see it at first, click Update Index.

  • Select Apply in the dialogue that appears to confirm the installation of the dependencies, and wait until the package is installed.
  • You can also install scipy in the same way, as we will need it later.

Using the pip package manager

We are going to use a package called Arduino-Python3 as we progress in the course. This package is not available through the package manager in Anaconda. To install it, we need to use another package manager called pip. It works well together with Conda.

To install Arduino-Python3 with pip, do the following:

  • Go to the environments tab of Anaconda.
  • Click the play button next to the Python environment you set up earlier. Click Open Terminal.

You will get a Terminal/Command Line window, which is referencing the Conda environment you launched it from.

  • Type pip install arduino-python3 followed by return.
  • pip will set to work and give you a message indicating if the installation was successful.
  • If the package was successfully installed, close the Terminal/Command Line window and return to Anaconda. If you encountered an error, ask for help.

After having installed the package, my terminal window looks like this:

Getting started with Spyder

Spyder is a scientific Python development environment that makes it easy to write and run Python code.

Installation

 You can install it from within Anaconda as follows:

  • Go to the Home tab on the upper left side of the Anaconda window.
  • Ensure the Applications on dropdown on the top of the screen is set to the environment phys_py37 that we just created.
  • Find Spyder and click Install. Wait until the installation is completed.

Use

 To start working with Spyder, do the following:

  • Again, ensure the Applications on dropdown on the top of the screen is set to the environment phys_py37 that we just created (or whichever environment you want to run code in).
  • Hit “Launch” on Spyder.
  • You are now greeted with the Spyder interface. It consists of three parts:
  1.    Code editor: where you write your Python code
  2.    Directory: where you store and access your files
  3.    Console: where you run and view the output from your code

After you have written some code, click the green play button at the top of the screen.

Set the directory path (circled in image above) to where you want your code to be saved.

Modules

  1. Buying Parts
  2. Preparing the Arduino
  3. Setting up Python on your PC
  4. Learning Python
  5. Arduino for Data Collection
  6. Ultrasonic Range Sensing
  7. Pendulum Experiment

 

Updated 2020-05-19  CEW

Leave a Reply