Tuesday 7 April 2015

Writing Your First Python Library

 

 #Can I do it?

YES you can, anybody can as i matter of fact I did, all you need is to be determined and must have zeal to do it. In beginning you might get this idea that these libraries are written by top-notch-geeky-nerdy developers, but you are wrong, most of the libraries are written and maintained by general enthusiasts like us. 


#How to do it?

1. Go to your project folder, create a folder namely <project_name>.

2. In the <project_name> folder, create another folder namely <Package_name>
    In <Package_name>, code your program <program.py>

3. Create a __init__.py file. Have a look. Save your program as __init__.py

             from <program.py> import <function_names>
             __all__ = [<function_name>, ]

4. Your FileSystem should look like:
                 
                 <Project_Name>
                         |
                  <Package_Name>
                         |
                         |________________________
                         |                                      |
              <program.py>                     __init__.py

5. In <Package_Name> you only needs 2 files <program.py> and __init__.py

6. Now, you need to write setup.py, README and MANIFEST.INI . Starting   from setup.py. setup.py=> A py file that contains the metadeta of your package.
Example : 

       #!/usr/bin/python2.7

       """GitGui Project"""

      __version__ =  '1.1.1'

     from setuptools import find_packages, setup

    setup(name = 'GitGui',
    package = ['GitGui'],
    version = '0.8',
    descripiton = 'A test module:GitGui',
    Summary = 'A test module : GitGui',
    long_description = 'A module for pushing a file to your github repo',
    platforms = ["Linux"],
    author = "Rahul Mishra",
    author_email = "priyrahulmishra@gmail.com",
    url= "https://github.com/Rahul91/GitGui/",
    download_url = "https://github.com/Rahul91/GitGui/tree/master/tarball/",
    license = "MIT",
    keywords = ['Git', 'Github', 'Python', 'GUI'],
    packages = find_packages()
    )


MANIFEST.INI => A file to instruct all files you need in your tarball (compressed file) zipped format.
     example: 
             include *.py
             include README

NOTE: You can also exclude any file explicitly using exclude <filename>.


README=> A file for introduction, like a manual for your package, highlighting its installation, usage and features.


6. Your tree of files should like like this.

                  <Project_Name>
                         |____________________________________________________________
                         |                               |                             |                                 |
                 <Package_Name>    README          MANIFEST.INI            setup.py   
                         |
                         |
                         |________________________
                         |                                      |
              <program.py>                     __init__.py


7. For creating a tarball, execute the command
            python setup.py sdist

8. Now we are ready to go live. 
   i. Create your account here: https://testpypi.python.org/pypi?%3Aaction=register_form

   ii. Now create a .pypirc.py file in your home folder, like given below.
                     [distutils]
                     index-servers =
                      pypi

                      [pypi]
                      repository: https://testpypi.python.org/pypi
                      username: <username>
                      password: <password>

Replace <usrname> and <password> with what you have created your account.

    iii. Register your project on PYPI.
         $ python setup.py register -r https://testpypi.python.org/pypi

     iv. Upload and you are done.
         $ python setup.py sdist upload -r https://testpypi.python.org/pypi


Now that you have uploaded it to server, it is ready for everyone, they can download, install and play around the code.

Hope this blog helps anyone who needs.

Cheers.
Happy Coding.