Category: Windows

Vagrant on Windows

There are many different ways to install python and work with python on Windows. You can install Canopy or Anaconda to have an entire python ecosystem self-contained or you can install python directly onto your machine and configure all the bits and bytes yourself. My current recommendation is to use Vagrant on Windows combined with Virtualbox to virtualize your development environment.

While I use a mac or the majority of my development, I do find myself using Windows 10 more and more, and may be moving to a Windows machine in the future for my day-to-day laptop.  I have and do use Canopy and/or Anaconda but I’ve recently moved the majority of my python development on Windows into Linux (Ubuntu) virtual machines using Vagrant and Virtualbox. You can use other products like VMWare’s virtual machine platform, but Virtualbox is free and does a good enough job for day-to-day development.1

One Caveat: if you’re doing processor / memory intensive development with python, this may not be the best option for you. That said, it can work for those types of development efforts if you configure your virtual machine with enough RAM and processors.

To get started, you’ll need to download and install Vagrant and Virtualbox for your machine.   I am using Vagrant 1.90 and Virtualbox 5.1.10 at the time of this post.

Feel free to ‘run’ either of the programs, but there’s no need to enter either program just yet.    To really use the Vagrant and the linux virtual machine, you’ll need to download a *nix emulator to allow you to do the things you need to with vagrant.

I use Git’s “bash” to interface with my virtual machines and Vagrant.  You could use putty or CygWin or any other emulator, but I’ve found Git’s bash to be the easiest and simplest to install and use.  Jump over and download git for your machine and install it. At the time of writing, I’m using Git 2.11.0.

While installing Git, I recommend leaving everything checked on the ‘select’ components window if you don’t currently have any git applications installed. If you want to use other git applications, you can uncheck the “associate .git* configuration files…” option.  There is one ‘gotcha’ when installing git that you should be aware of.

On the “adjusting your path” section (see figure 1), you’ll need to think about how you want to use git on the command line.

git command line path
Figure 1: Adjusting your path

I selected the third option when installing git. I do not use git from the windows command line though…I use a git GUI along with git from the command line within my virtual environment.

Another screen to consider is the “Configuring the terminal emulator…” screen (figure 2).  I selected and use the MinTTY option because it gives me a much more *nix feel. This is personal preference. If you are going to be doing a lot of interactive python work in the console, you might want to select the 2nd option to use the windows default console window.

Configuring your terminal emulator
Figure 2: Configuring your terminal emulator

During the remainder of the installation, I left the rest of the options at the defaults.

Now that git (and bash) is installed, you can launch Git Bash to start working with Vagrant. You should see a window similar to Figure 3.

Git Bash
Figure 3: Git Bash

From this point, you can do your ‘vagrant init’, ‘vagrant up’ and ‘vagrant ssh’ to initialize, create and ssh into your vagrant machine.

Setting up Vagrant on Windows

For those of you that haven’t used Vagrant in the past, here’s how I set it up and use it. I generally use vagrant in this way to run jupyter, so I’ll walk you through setting things up for jupyter, pandas, etc.

First, set up a directory for your project. At the Bash command line, change into the directory you want to work from and type “mkdir vagrant_project” (or whatever name you want to use). Now, initialize your vagrant project by typing:

vagrant init

This will create a Vagrantfile in the directory you’re in. This will allow you to set the configuration of your virtual machine and Vagrant. The Vagrantfile should look something like this:

VagrantFile Example
Figure 4: VagrantFile Example

Before we go any further, open up your Vagrantfile and change the following line:

config.vm.box = "base"

change “base” to “ubuntu/xenial64” to run Ubuntu 16.04. The line should now read:

config.vm.box = "ubuntu/xenial64"

If you want to run other flavors of linux or other OS’s, you can find others at https://atlas.hashicorp.com/search.

Since I’m setting this VM up to work with jupyter, I also want to configure port forwarding in the Vagrantfile. Look for the line that reads:

# config.vm.network "forwarded_port", guest: 80, host: 8080

and add a line directly below that line to read:

config.vm.network "forwarded_port", guest: 8888, host: 8888

This addition creates a forwarded port on your system from port 8888 on your host (your windows machine) to port 8888 on your guest (the virtual machine). This will allow you to access your jupyter notebooks from your Windows browser.

At this point, you could also configure lots of other vagrant options, but these are the bare minimums that you need to get started.

At the Bash command line, you can now type “vagrant up” to build your virtual machine. Since this is the first time you’ve run the command on this directory, it will go out and download the ubuntu/xenial64 ‘box’ and then build the virtual machine with the defaults.  You might see a Windows alert asking to ‘approve’ vagrant to make some changes…go ahead and allow that.

Once the ‘vagrant up’ command is complete, you should see something similar to Figure 5 below.

Output of Vagrant Up
Figure 5: Output of ‘vagrant up’

Now, you can ‘vagrant ssh’ to get into the virtual machine.  You should then see something similar to Figure 6. Now your running vagrant on windows!

Vagrant SSH - Vagrant on Windows
Figure 6: Output of ‘vagrant ssh’

One of the really cool things that vagrant does by default is set up shared folders. This allows you to do your development work in your favorite IDE or editor and have the changes show up automatically in your vagrant virtual machine.

At the Bash command line, type:

cd /vagrant

You should see a directory listing that has your Vagrantfile and a log file. If you visit your project directory using windows explorer, you should see the same two files. Shared folders for the win! I know its just a small thing, but it makes things easier for initial setup.

You now have vagrant on windows!

Configure the Python Environment

Time to set up your python environment.

First, install pip.

sudo apt install python-pip

Even though you’ve set up a virtual machine for development, it is still a good idea to use virtualenv to separate multiple projects requirements.  Install install virtualenv  with the following command:

sudo apt install virtualenv

In your project directory, set up your virtual environment by typing:

virtualenv env

Note: You may run unto an error while running this command. It will be something like like the message below:

OSError: [Errno 71] Protocol error

If this happens, delete the ‘env’ folder and then add ‘–always-copy’ to the command and re-run it. See here for more details.

#### run this only if you had an error in the step above
virtualenv env --always-copy

Activate your virtualenv by typing:

source env/bin/activate

We’re ready to install pandas and jupyter using the command below. This will install both modules as well as their dependencies.

pip install pandas jupyter

Now you’re ready to run jupyter.

jupyter notebook --ip=0.0.0.0

In the above command, we start jupyter notebook with an extra config line of ‘–ip=0.0.0.0’. This tells jupyter to listen on any IP address. It may not always be necessary, but I find it cuts out a lot of issues when I’m running it in vagrant like this.

In your windows browser, visit ‘http://localhost:8888/tree’ and  – assuming everything went the way it should – you should see your jupyter notebook tree.

Jupyter via Vagrant VM
Figure 7: Jupyter via Vagrant VM

From here, you can create your notebooks and run them just like you would with any other platform.

 

Installing python on Windows

Note: Enthought Canopy is End-of-Life.  Rather than re-write this piece, I’ll just point readers to the Enthought End-of-Life note for more information on how to move to the new support version(s). When time permits, i’ll write up another post describing the installation process.

If you’ve done any work with python on Windows, you may be cringing right now at the thought of trying to do any type of python development work on the platform.  Have no fear though…there is hope for python developers on Windows, especially if you are only going to be using python for data analysis, machine learning, etc and not doing any major web development work (with flask, django, etc). In this post, I describe the steps necessary for installing python on Windows.

There’s really only one method for using / installing python on windows that is convenient and works for 99.9% of the people on Windows who are focused on scientific computing — downloading Enthought Canopy orAnaconda and installing it. For those of you getting started with data analytics, Canopy gets you started faster and makes it very easy to get modules like panda, numpy, scipy, etc installed and configured (in most cases, these are already installed when Canopy is installed).

For those of you running on Mac or Linux, you can also install Canopy for your platforms. I personally don’t use Canopy on the Mac or Linux platform, but only because I prefer to manage things a bit differently on those platforms. There’s nothing wrong with using Canopy on Mac or Linux, I just prefer not to.

Installing Python on Windows using Canopy

For the purposes of this post, we are going to install Canopy(accurate as of November 2016).

  • Step 1 – visit the Enthought Canopy website and click the “Get Canopy” button.
  • Step 2 – select the “download” option for Canopy Express – FREE. This lets you get the platform without paying any additional money. If you are going to be using Canopy for heavy duty scientific work, I’d recommend buying one of their subscriptions since you get more modules, etc to work with. If you are a student or work in academia, you can ask for an academic license for free.  Note – Direct link for downloading Canopy Express.
  • Click the “Download Canopy” button. A web form will pop up asking for information…you can ignore that. Your download has started. Note: Canopy is available in 64-bit and 32-bit versions. I recommend the 64-bit if you are on a modern computer / operating system.

 

Installing python on Windows - Canopy Download

  • Once your download completes, run the executable to begin the installation process. A wizard will be displayed…hit “next” through the wizard and install the software. Once installation is complete, the final screen (see below) will have a ‘finish’ button and a ‘Launch Canopy when setup exits” checkbox. Leave the checkbox selected and click “finish” to complete the installation and launch Canopy.

Installing python on Windows - Canopy Final Installation Screen

  • The first  time you run Canopy, you will be presented with an ‘environment’ window (see below).  You can leave this at the default or select another location to store your environment information. I suggest leaving it at default to begin with. Click “Continue” to begin using Canopy.

Installing python on Windows - Canopy Environment Window

  • The first time you load Canopy, it will take some time to load the various modules into memory and setting up your Canopy environment. Each time after this first start, the platform should load up fairly quickly.
  • Once Canopy completes loading your environment for the first time, you’ll be asked if you want to make Canopy your default Python environment. Select “Yes” and click “Start using Canopy”.  If you select “no”, you will have to do a some manual configuration to begin using Canopy.

Installing python on Windows - Canopy Default Python

  • When Canopy starts, you’ll see the window below.

Installing python on Windows - Canopy Start Page

  • You now have Canopy installed and ready to go.  To start programming, click the ‘Editor’ button and Canopy will load up an editor to you can begin work. Below is a screenshot of the editor window.

Installing python on Windows - Editor Window

Check out the other posts on this website for more information on how to get started actually DOING something with python.