Pages

Thursday, December 8, 2016

Python for Salesforce Administrators - Installing Python 3 on Windows

As I mentioned here, an "IDE" is, basically, a text-editor with a "play" button to execute whatever code you've typed into the text editor.

My favorite suite of software for running "Python 3" on Windows is called WinPython.
(Read up here about Python 2 & Python 3.)

You can install and run WinPython without administrator rights.

The latest versions are always listed at the software's Github page, but Windows 7 & Windows 8 users whose systems don't have the "Microsoft Visual C++ Redistributable for Visual Studio 2015" update installed into Windows might not be able to actually run the very latest versions of WinPython after installing them. (And, unfortunately, you can't install this update to Windows without administrator rights.)

The most recent "64-bit" version of WinPython for "Python 3" that I've found that works without that update is WinPython 3.4.3.5, which you can download here.

WinPython doesn't really install anything on your computer. It just puts a bunch of files into whatever folder you tell it to put those files into. When you want to "uninstall" it, you simply delete the folder. (Similarly, you have to create your own Desktop and Start-Menu shortcuts by hand.) If you "install" a certain version of WinPython and find that you can't run certain programs due to an error saying that "api-ms-win-crt-runtime-l1-1-0.dll" is missing, or if those program never show any sign of running at all, delete your "installation" and start over with a different version of WinPython.


Here are screenshots of the steps I took as I installed WinPython 3.4.3.5, 64-bit, on a thumb drive.

Step 1: download the installer file to my hard drive

Step 2: run the installer file by double-clicking on it

Step 3: wait a few seconds for the installer to come up

Step 4: accept the licensing agreement

Step 5: specify where on your computer (or on a thumb drive) you want the folder containing "WinPython" files to live & click "Install"

Step 6: wait for the installer to put all of the files on your hard drive, then click "Next," then click "Finish"


Let's see what we've done so far.

Step 7: navigate to the folder where you installed WinPython and observe the two files "Spyder.exe" and "WinPython Command Prompt.exe"

  • "Spyder" is your IDE (the text-editor with a play button)
  • The "WinPython Command Prompt" is what you run if you need to type commands that begin with "pip" (which is command-line-interface software that can download and install Python "modules" you don't already have based on the module name - if you got a "full" version of WinPython, you probably won't need to use this)

You might want to manually create shortcuts to these two files - or at least to Spyder - in your Start Menu and/or on your Desktop.


All right ... let's run some code!

Step 8: run "Spyder.exe" by double-clicking on it (it takes a minute to load -- something went wrong if you don't see a greenish "splash screen" with a spiderweb on it within 20 seconds, though)

Step 9: type the following text into the text-editor in Python and click the "play" button (or hit "F5"), which means "run":

print('hello there')

Note: If you get a pop-up dialog with all sorts of options about executing code consoles or interpreters and such, click "cancel" on the pop-up dialog box and then click the play button again - for some reason. The pop-up box was giving you a chance to highly customize where you want to see any output from running your code. Clicking "cancel" and trying again seems to tell Spyder, "you figure out where to display the output of this code, don't bother me with all these options." (Unfortunately, I didn't catch this dialog box with a screenshot.)

Step 10: observe that the "console" in another corner of your screen" said "hello there" in it!

Note: Typically, when you are programming, the first thing you do with a new tool is try to get it to display some simple text on your screen. That's what we did in Steps 9 & 10. But it's important to remember that not every command in a programming language displays text on the screen when it runs. For example, in some of the Python examples on this blog, certain commands create CSV files - they don't display any text on the screen at all when they're done creating the CSV file. To know when such code is done, you could add a line to your script afterwards along the lines of "print('all done')". You may also pick up on subtle changes in the "console" in Spyder as code starts & stops running (for example, you get a new "[]" prompt - which you can ignore - when code has finished running).

Nevertheless, no matter what your code does, the area of Spyder where you typed in Step 9 is the area where you will want to copy/paste all of the example code from this blog before tweaking it to match your own needs and clicking "Run" (the play-button / F5).

Now that you're up and running, time to learn how to use Python!


Web-Browser-Based Exercises

DO NOT DO NOT DO NOT use your own company's data with this trick.

You have to promise me. Pinky-swear.

You MUST use a properly-installed copy of Python as described above for data you actually want to work with, no matter how inconsequential you think it is.

BUT ... if you'd like to just play along with the "sample1.csv"-type data in this blog, I've found a way you can do so right now, using nothing but your web browser.

I painstakingly made all of that data up, word by word. That's why it's okay to just throw into some random service hosted online.

Real CSV files from your company are NOT okay to throw into some random service hosted online. Even if it doesn't seem like important data. Just do it on your local computer and be safe, not sorry.

Anyway ... for imitating exercises in this blog, you can go to https://repl.it/languages/python3

(Update, 2018: repl.it sometimes seems to run slowly, lately, when using the "Pandas" functionality of Python. At the moment, https://codebunk.com/b/ still seems to work, but that link is just meant for trying their paid product, so please don't go too crazy.)

If an exercise in this blog claims it works with a file "C:\tempexamples\sample000.csv" whose contents look like this:

"Id","First","Last","Email","Company"
"5829","Jimmy","Buffet","jb@example.com","RCA"
"2894","Shirley","Chisholm","sc@example.com","United States Congress"

And then if the Python code looks like this:

import pandas
df = pandas.read_csv('C:\\tempexamples\\sample000.csv')
print('---Here is a sampling of the first lines---')
print(df.head())

Then your code at Repl.it will be:

import sys
if sys.version_info[0] < 3: from StringIO import StringIO
else: from io import StringIO

sample = StringIO("""
"Id","First","Last","Email","Company"
"5829","Jimmy","Buffet","jb@example.com","RCA"
"2894","Shirley","Chisholm","sc@example.com","United States Congress"
""")

import pandas
df = pandas.read_csv(sample)
print('---Here is a sampling of the first lines---')
print(df.head())

Note that you have to put in 3 lines of importing extra "modules," then you have to do some weird, very precise code surrounding a copy/paste from the contents of the CSV file, and finally, in the "df = pandas.read_csv()" line, instead of saying "'C:\\tempexamples\\sample000.csv'", you refer to the nickname you gave your fake CSV file (in this case, "sample") - without any single-quotes around it.

Drive carefully. Remember your pinky-swear.


Table of Contents

No comments:

Post a Comment