Introduction to Python on Windows with Visual Studio

Well you got just your new Windows PC.
Hopefully a crispy Surface Pro 4 (Starting at $899)

And you want to learn how to program, and Python is the language you’re going with.
Or you already know how to program and this is the first windows PC you’ve used in a while and like Python.
I don’t know you…

Well that’s fine too.

All the code and resources associated with this post can be found on GitHub

If at any point you get lost or confused, feel free to raise your hand/add a comment or PM me on twitter @timmyreilly

In this course we’ll cover:
1. Installing Python
2. Using the REPL
3. Running our first program
4. Examining the Python Folders
5. Using Pip
6. Creating, installing to, and using virtualenv and virtualenvwrapper-win
7. Examining the Envs folders!
8. setprojectdir and Workflow
9. Visual Studio
10. Virtual Envs in VS
11. Exploring Visual Studio Directories
12. Using the REPL in VS
13. Shortcuts for VS
14. Continued Learning

1. Installing Python

Windows

2. Using the REPL

The REPL stands for Read Evaluate Print Loop

Makes iterating and testing easy!

Type python into your command prompt


C:\..\> python
Python 2.7...
>>>

try these commands:

>>> x = 2 + 2
>>> print x
>>> import this
>>> type(x)
>>> dir(x)

More Practice

3. Running our first program

Use your favorite text editor. I’m using Visual Studio Code

I’ve named my example: beginnings.py

 
sentence = "Four score and seven years ago"
sentence_no_vowels = ""
for letter in sentence: 
    if letter not in "AEIOUaeiou":
        sentence_no_vowels = sentence_no_vowels + letter
         
print sentence_no_vowels

4. Examining the Python Folders

So because we’re on windows let’s take a look and explore our current folder structure just to get familiar with how Python works.

PythonDirectory

We’ll also show some helpful ways of interacting with python in our terminal

Calling Python Explicitly
> C:\Python27\python.exe
Calling Pip directly
> C:\Python27\Scripts\pip.exe list
Using Easy Install
> C:\Python27\Scripts\easy_install.exe
What Else is in there?
> dir C:\Python27\

5. Using Pip

Install a package

Python Packages are installed with pip

We’ll install requests


> pip -v
> pip install requests

Uninstall a package

pip uninstall requests

6. Creating, installing to, and using virtualenv and virtualenvwrapper-win

To another Blog Post for these steps.

7. Examining the Envs folders!

Now that we’ve used our first virtual environment where is it?
Let’s go find it! The files are in the computer!


> C:\Users\tireilly\Envs\helloworld\Scripts\pip.exe list
> C:\Users\tireilly\Envs\helloworld\Scripts\easy_install.exe

8. setprojectdir and workflow

That was fun. Let’s do it a lot.

cd into root of project


> workon newProject
> cd newProjectDirectory
> setprojectdir .
> deactivate
> workon helloworld

Now that was all fun and agnostic.
Let’s dive into Visual Studio.

9. Visual Studio

Community Edition is Free and Awesome!
– And there’s support for extensions like PTVS: http://microsoft.github.io/PTVS
– It’s a beast!
– Great for debugging, large projects and working with many different technologies.
The python offering is found in the form of Python Tools for Visual Studio.

The three things I want to introduce are Virtual Envs, the REPL, and Shortcuts

10. Virtual Envs in VS

– Visual Studio will Manage Virtual Environments for you!

11. Exploring Visual Studio Directories


> C:\visualstudio\projects\OneHourPython\OneHourPython\env\Scripts\pip.exe
> C:\visualstudio\projects\OneHourPython\OneHourPython\env\Scripts\python.exe

PythonVisualStudioDirectory

Check them out in the directory too!

12. Using the REPL in VS

13. Shortcuts for VS

Make it buttery

– Ctrl+F5 = Run Without Debugging
– Ctrl+E, Ctrl+E = Selected text to interactive
– Alt+Shift+F5 = Send file to interactive

That’s it for now, but don’t stop here the funs about to start!

Continued Learning & Other Resources:
Try Azure
Take your ideas of the Ground: BizSpark

Now that we’ve taken our first couple steps with Python, where should we go next?

See all the code associated with this project and more resources on my GitHub!

Please contact me if you have any questions: @timmyreilly

This is what happens when you get coffee on your birthday...
This is what happens when you get coffee on your birthday…

Python, Pip, virtualenv installation on Windows

No more struggles Windows Python development! I’ve found this is the best way to configure your dev environment.
This has made things much easier to get started and less of a headache overall.

We use Virtual Environment so we can test python code in encapsulated environments and to also avoid filling our base Python installation with a bunch of libraries we might use for only one project.

But Virtual Environments can be tricky if you don’t establish a good workflow. I’ll show you how to setup your python environment from Scratch and then do a very simple workflow using Flask.

SETUP
4 Steps:
Install Python
Install Pip
Install VirtualEnv
Install VirtualEnvWrapper-win

Install Python:

First Go to the Python Downloads Site.

As of March 2015 the download you want for a standard windows machine is Windows x86-64 MSI installer (The other download is for servers). Its circled here:

Download

Run the installer!
You’ll come across this page in the installer:

PythonInstaller

You’ll want to scroll down and add it to the path. If you don’t that’s okay. You can add it later.
Adding Python to the PATH will allow you to call if from the command line.

After the installation is complete double check to make sure you see python in your PATH. You can find your path by opening your control panel -> System and Security -> System -> Advanced System Settings -> Environment Variables -> Selecting Path -> Edit ->

Now you’re looking at your Path. Be Careful, if you delete or add to the path accidently you may break other programs.

You need to confirm that C:\Python27; and C:\Python27\Scripts; is part of your path.

If you do not see it in your path you can simply add it at the beginning or end of the variable value box. As you can see in the image below.

AdvancedSettings

Install Pip:

As of Python Version 2.7.9 Pip is installed automatically and will be available in your Scripts folder.

If you install a later version of Python I would recommend installing it according to this helpful stackoverflow post.

Pip is a Package manager for python which we will use to load in modules/libraries into our environments.

An example of one of these libraries is VirtualEnv which will help us keep our environments clean from other Libraries. This sounds really confusing but as you start using it you’ll begin to understand how valuable this encapsulation of modules/libraries can be.

To test that Pip is installed open a command prompt (win+r->’cmd’->Enter) and try ‘pip help’

You should see a list of available commands including install, which we’ll use for the next part:

Install virtualenv:

Now that you have pip installed and a command prompt open installing virtualenv to our root Python installation is as easy as typing ‘pip install virtualenv’
Like so:

pipinstallvirtualenv

Now we have virtualenv installed which will make it possible to create individual environments to test our code in. But managing all these environments can become cumbersome. So we’ll pip install another helpful package…

Install virtualenvwrapper-win:

This is the kit and caboodle of this guide.

Just as before we’ll use pip to install virtualenvwrapper-win. ‘pip install virtualenvwrapper-win’
Like so:

virtualenvwrapper-win

Excellent! Now we have everything we need to start building software using python! Now I’ll show you how buttery smooth it is to use these awesome tools!

USAGE
7 Steps:
Make a Virtual Environment
Connect our project with our Environment
Set Project Directory
Deactivate
Workon
Pip Install
Flask!

Make a Virtual Environemt:

Lets call it HelloWold. All we do in a command prompt is enter ‘mkvirtualenv HelloWold’
This will create a folder with python.exe, pip, and setuptools all ready to go in its own little environment. It will also activate the Virtual Environment which is indicated with the (HelloWold) on the left side of the prompt.

mkvirtualenv

Anything we install now will be specific to this project. And available to the projects we connect to this environment.

Connect our project with our Environment:

Now we want our code to use this environment to install packages and run/test code.

First lets create a directory with the same name as our virtual environment in our preferred development folder. In this case mine is ‘dev’

See here:

mkdir

HelloWold will be the root folder of our first project!

Set Project Directory:

Now to bind our virtualenv with our current working directory we simply enter ‘setprojectdir .’
Like so:

setprojectdir

Now next time we activate this environment we will automatically move into this directory!
Buttery smooth.

Deactivate:

Let say you’re content with the work you’ve contributed to this project and you want to move onto something else in the command line. Simply type ‘deactivate’ to deactivate your environment.
Like so:

deactivate

Notice how the parenthesis disappear.
You don’t have to deactivate your environment. Closing your command prompt will deactivate it for you. As long as the parenthesis are not there you will not be affecting your environment. But you will be able to impact your root python installation.

Workon:

Now you’ve got some work to do. Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move into your root project folder.

Like so:

workon

Pretty sweet! Lets get working.

Pip Install:

To use flask we need to install the packages and to do that we can use pip to install it into our HelloWold virtual environment.

Make sure (HelloWold) is to the left of your prompt and enter ‘pip install flask’
Like so:

pipinstallflask

This will bring in all the tools required to write your first web server!

Flask:

Now that you have flask installed in your virtual environment you can start coding!

Open up your favorite text editor and create a new file called hello.py and save it in your HelloWold directory.

I’ve simply taken the sample code from Flask’s website to create a very basic ‘Hello World!’ server.

I’ve named the file hello.py.

Once the code is in place I can start the server using ‘python hello.py’ this will run the python instance from your virtual environment that has flask.

See here:

webserver

You can now navigate with your browser to http://127.0.0.1:5000/ and see your new site!

Sweet. You have everything you need to start working through tutorials on Flask without worrying about gunking up your Python installations.

Let me know if you have any questions! Happy Developing!

Art Deco From Afar
Art Deco From Afar