This half-day workshop will explore coding basics through the creation of customized Twitter bots with Python. Topics will include use of the command line, installation and basics of the Python programming language, use of the Twitter developer console, practical Github for projects and an introduction to servers.
Please read this carefully and follow the instructions before the workshop Wednesday. You’ll need to have a few things set up on your machine properly before moving forward. If you’re using a work machine, you will likely have to get IT to use their administrative access to install these items.
All Macs should come pre-installed with Python, the coding language we’ll be using for the workshop. Open Terminal and use the following command to check which version of Python you’re using (press return to execute the line of code).
python -VYou should be running a version of Python 2.7.X. On my machine, for example, I’m running 2.7.10.
You’ll need to tell your machine where to install new packages, so we’ll use a built-in text editor called nano to do that. Enter the following command:
nano ~/.bash_profileThen paste the following line into the empty text field displayed in Terminal.
export PATH=/usr/local/lib/python2.7/site-packages:$PATHSave the file by typing control+X, Y in response to “Save modified buffer?” and Return to confirm the file name. This will take you back to the command line. Update your Terminal window with the following command:
source ~/.bash_profileInstall Python version 2.7 by downloading and installing the file at the following link. This will also install pip, which is like an app store for Python code.
Run a Powershell window and type the following command (you can paste by right clicking in the Powershell window). This command makes it easier to run Python without specifying its exact location on your machine.
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")Restart your Powershell window. You should now be able to enter the following command, which starts the Python shell.
pythonExit the Python shell with:
exit()Pip is, to quote much smarter developers, like an app store for Python code. It will help you install new tools you need to code projects more efficiently and expand your own code’s capabilities.
Open Terminal and enter the following command. Press Return to run (you must have admin privileges on your machine):
sudo easy_install pipUsing virtual environments allows you to cordon off all the various packages and libraries you install and get around requirements to run the system as an administrator. To install this software, you will need admin privileges on your machine.
Open Terminal and enter the following command. Press Return to run:
pip install virtualenvNext, we’ll install an additional piece of software that will make working with virtual environments a little easier. Open Terminal and enter the following commands, one by one. Press “Return” to run each of them:
pip install virtualenvwrapperThen, edit your bash profile again using nano:
nano ~/.bash_profileThen paste the following line into the file, below the one you entered earlier:
source /usr/local/bin/virtualenvwrapper.shSave the file by typing control+X, Y in response to “Save modified buffer?” and Return to confirm the file name. This will take you back to the command line. Update your Terminal window with the following command:
source ~/.bash_profileNow, you will be able to create a new virtual environment with:
mkvirtualenv fooWork on it with:
workon fooDeactivate with:
deactivateFor more information and follow-up reading, see the virtual environments entry in The Hitchhiker’s Guide to Python.
Using virtual environments allows you to cordon off all the various packages and libraries you install and get around requirements to run the system as an administrator.
Open Powershell and enter the following command. Press Return to run:
pip install virtualenvNext, we’ll install an additional piece of software that will make working with virtual environments a little easier. Open Terminal and enter the following commands, one by one. Press “Return” to run each of them:
pip install virtualenvwrapper-winWe'll need to slightly tweak the execution policies on the system, so as an administrator, get someone in IT to run the following:
Set-ExecutionPolicy -Scope LocalMachine RemoteSignedNow, you will be able to create a new virtual environment with:
virtualenv .\fooActivate it with:
.\foo\scripts\activateDeactivate with:
deactivateGit is a version control system you can use to manage your code projects. It powers an easy-to-use online repository system called GitHub, which allows you to track your code contributions and collaborate with others.
Install Git using the installer here for Mac or here for PC and follow the prompts (you will probably need admin access to do this).
Now, sign up for GitHub.
Now, follow the instructions on generating SSH keys and adding them to your GitHub account (don't install the desktop client). This will prevent you from having to use passwords to log in to your account from the Terminal or Powershell, allowing you to quickly upload code contributions to your GitHub projects. Start with “Checking for existing SSH keys,” and make sure you select the instructions for Mac or PC (you will probably need admin access to do this).
There are lots of text editors out there, and you’re free to use whichever one you’re comfortable with. I suggest installing Sublime Text (you’ll need admin privileges).