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
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.
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\
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!
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.
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:
Run the installer!
You’ll come across this page in the installer:
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.
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:
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:
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.
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’
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:
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:
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:
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:
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.
I was struggling with pulling the data from a Database using flask and serving it to the client for an Ajax request.
The end goal is a heat map of sorts with paths being mapped and colors being changed for certain conditions.
I’m still working out the best practices for this, but its working! So take it with a grain of salt.
After a couple days of tinkering, the main pieces of code that lit this up are as follows.
Get the data from the Azure Table Storage and convert it to a list
# First connect to Azure Table Service
def connect_to_service():
table_service = TableService(account_name=STORAGE_ACCOUNT_NAME, account_key=TABLE_STORAGE_KEY)
print TableService
return table_service
# Grab the data from our table
def get_table_list(table_service=connect_to_service(), max=10, table_name='test', partitionKey='default'):
x = table_service.query_entities(table_name)
#print(x)
return x
# return it as a list * see below for bonus on selecting color codes
def get_json_list(entity_list=get_table_list()):
'''
Takes azure table list and returns json list
'''
i = 0
response = []
for r in entity_list:
c = convert_color_key_to_rgb(int(entity_list[i].colorKey))
t = (entity_list[i].latA, entity_list[i].longA, entity_list[i].latB, entity_list[i].longB, entity_list[i].colorKey, c[0], c[1], c[2])
response.append(t)
i += 1
# print response
return response
* BONUS to manage color codes nicely I found this neat way of doing switch statements using dictionaries.
Take that list and pass it as JSON to a template.
Still not sure if this is best practice, as on the client/javascript side it picks it up as an array object.
But this is the route we use in flask to send our data.
On the template side iterate through the given array object and build and push polylines.
This was the toughest part for me to
var map = null;
function GetMap()
{
var y = JSON.parse('{{ data|safe }}');
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),{
credentials:"{{ key }}",
center: new Microsoft.Maps.Location(y[0][0],y[0][1]),
zoom: 10
});
console.log(y);
for( index = 0; index < y.length; index++ ){
lOne = new Microsoft.Maps.Location(y[index][0], y[index][1]);
lTwo = new Microsoft.Maps.Location(y[index][2], y[index][3]);
//console.log(lOne);
var lineV = new Array(lOne, lTwo);
//console.log(y[index][4])
var pLine = new Microsoft.Maps.Polyline(lineV, {
strokeColor: new Microsoft.Maps.Color(y[index][5], y[index][6], y[index][7], 100),
strokeThickness: 3
});
map.entities.push(pLine)
}
}
Now when I run this project locally I get something that looks like this:
Soon they won’t be random.
If you want to give it a shot, check out the instructions/source code on github
Feel free to reach out if you have any questions or would like to work together on a project like this.
This is a follow up to a recent post I made about using Project Oxford with Python. If you haven’t used Project Oxford before visit that post and complete the first three steps to acquire a token.
The previous example used Python 3 and urllib which is cool. But I’m more familiar with the requests library and python 2 so I spent some time re-writing the app with requests, and in the process learned what’s actually going on.
The meat of the program is tts.py which provides the methods we use in the app to provide an access token and use it to hit the API with our words to digitize.
Then we take that token and send a POST request with our access Token and specific headers “TTS_HEADERS” and “body” which includes the text to be translated.
After we POST we receive a JSON object that we encode into base 64 and inject into our HTML Template (index.html) using views.py.
To run this project:
clone the project
create a virtual env
pip install requirements.txt
python runserver.py
visit http://localhost:5555/
This project is ready to be deployed to Azure as a Web App! If you don’t know/care about that you can simply delete all the other files besides runserver.py and the contents of FlaskWebProject
Let me know if you have any questions or would like to contribute to the next iteration!
I’ve been working a lot with Visual Studio Code the last couple weeks, but recently made the switch back to Visual Studio Community Edition with Python tools for Visual Studio because of three handy shortcuts and one awesome virtual environment manager.
Those three shortcuts are…
Ctrl+F5
Run without debugging.
This sends your python file to the virtual env you have activated to a terminal right next to your project.
Which on its own is cool and handy, but when you right select your solution then select properties:
You can set it to run whatever file you’re currently editing, which makes hoping around different files as your learning so nice.
The next shortcut is…
Alt+Shift+F5
Send file to interactive
This is great for when you’re just learning about a new library and you keep piecing together what you understand into the file.
Just keep hitting Alt+Shift+F5 send it to the interactive mess around in the terminal with the new objects you’ve created then determine the best logic and send it back into your file.
You break something?
Just send it back in… perhaps using our final shortcut:
Ctrl+E, Ctrl+E
Send selected text to interactive
Don’t let the comma throw you off, just selected some text and hit ctrl+e twice to run that code in the REPL. This in conjunction with Alt+Shift+F5 make it so pleasant to work in Visual Studio with Python.
Visual Studio is great and all, but if I need to learn some new library or work through a tutorial the robustness and Experience of Visual Studio is tough to beat.
Oh yeah, not related to shortcuts but still awesome is Virtual Environment Support:
Those env’s I created by right clicking on Environments and selecting new virtual environment.
There is support for both versions of Python and to install just use pip by right clicking on the env and install from pip!
I’ll let this nice video they’ve created break it down:
Skip to 9 minutes in to see the Virtual Env Breakdown
The last two days I’ve been learning about Visual Studio Code Extensions!
This for all the Node/JavaScript Developers out there.
In the end, writing an extension turned out to be a joy. You basically get to create a little program that changes a tool you use everyday and tweak it to exactly what you want then easily publish this to the world.
If you install this plugin, you’ll get typewriter sounds as you begin to edit your markdown file.
All the code is available on GitHub, and the instructions for creating your own extension are available on the well documented Visual Studio Code Extensions website!
I thought I’d share some links and resources to help you get started.
You can also look at the live demos/documentation to get started and test the capabilities.
3. Now that we’re signed up and have access to our keys go ahead and keep that tab open because you’ll need to use you’re own key to get this sample code working.
4. In the meantime there is some important documentation to read over to help familiarize yourself with what’s going on:
(Reminder – this API is still in Beta so expect changes along the way)
We’re requesting a specific file type in return with this header and you can set it to a number of different formats.
You’ll see at this documentation all the different available formats: https://www.projectoxford.ai/doc/speech/REST/Output
we changed our format from the mono-pcm to mulaw -> which has to do with audio compression.
Basically we get back a string that we can encode into base 64 trim the edges and send raw to the browser which will be available to play with the audio tag.
You’ll see this at line 46 of helper.py with our get_raw_wav function.
Note:
The body of our request can be edited to get different voices (male/female) different languages, and even different cadences.