Installing Python modules

The cool thing about Python is the open source and the community. Thanks to the community there are modules for complicated task already developed for us to use. Simply putted, a Python module is usually some code someone made to share that we can use. They did the hard work, all we need to do is use it. But to use it we need to install it, and this is a quick guide on how to install someone else module.

Downloading Modules

Github has plenty of open source modules for us to use for our projects. Let use one as an example for this guide. There is two methods of downloading a module from there.

I will use pytumblr as an example to explain how to install a module.

Downloading a module

  1. Download the .zip (simplest) and extract it somewhere.
    Download pytumblr .zip from github

    or

  2. Using Git clone. If you have git installed ( highly recommended tool for saving and distributing your code), you can simply run this command to clone a copy of it to your computer.
		git clone https://github.com/tumblr/pytumblr.git
	

Installing the module

Installing a Python module is easy. Just go to the folder you downloaded and look for setup.py. Call python setup.py install in the command prompt (cmd). There are a couple ways you can do this. Here is my ways of doing it:

  1. Go to the folder where setup.py for the module is located.
  2. Hold down Shift and Right Click anywhere with empty space within that folder.
  3. Click Open command window here.
  4. Type python setup.py install into the window that pop up. You will see a bunch of text scroll. You should see some similar text.

** or **

  1. Click on the windows start menu at the bottom left hand corner.
  2. Search for cmd
  3. Type python "location of setup.py" install, replace “location of setup.py” with your location. You can even drag setup.py from the file explort into the cmd and it will insert the location for you.

    Congrats you installed your first Python module. Easy huh.

Using the module

It is important to read the documents/information provided with the module. Usually it is as simple as importing it and just calling it from in your code. For example, this is all I need to do to use pytumblr in my code.

		import pytumblr

		client = pytumblr.TumblrRestClient(
			'<consumer_key>',
			'<consumer_secret>',
			'<oauth_token>',
			'<oauth_secret>',
		)

		client.info() # Grabs the current user information
	

To break it down:

Line 1 `import pytumblr` tells Python that you want to the pytumblr module.

Line 2 just create a new object name `client` and fill it with your account information.

Line 3 `client.info()` is calling a function `info()` which will return information about that user.

If you don’t understand anything about the code right now don’t worry. This guide was simple to explain how to install a module.

comments powered by Disqus