Python is a high-level general purpose programming language that has been getting a lot of hype for quite a while now. 1991 is the birth date of Python, and one of the greatest strength of Python is its flexibility.

Most of you must be well-known about the popularity of Python in the fields, such as data science, artificial intelligence, and machine learning. The majority of professionals in the field of data science use Python. One can get into Data Science with Python certification training to jump right into the field of data science. These certifications provide a strong foundation for those professionals who want to get into data science.

With that said, Python is not only limited to data science, AI, and machine learning. Many developers prefer using Python for many other fields, and one of them is web development. Take a look at the rise in the popularity of Python regarding job posting.

Image Source: SMARTBEAR

Some of the most popular websites in the world were built using Python. To name some, we have Google, YouTube, Quora, Reddit, Instagram, Spotify, and so on. Some might argue about the speed of Python, but the fact is that it hardly matters in most of the cases. Why? The processors today are super-fast, and it will get faster.

To create a web application, you need a framework. Django is one of the most popular Python web frameworks in the world today.

In this article, I will be dealing with the process of creating your first Django library. I will take you through the process of creating your first application from the beginning to the point of testing it without projects. Furthermore, you will also learn how to distribute it. Are you ready? Let’s get started then.

Creating your Django library

To get the best outcome from this guide, you should follow the steps correctly. After creating a simple library, you can explore on your own to come up with more complex libraries. Without further ado, let’s dive right into the guide.

  1. The first step is to develop your application

There is no need for you to create a separate project for building a third-party library. The process of creating an app is simple. Jump into start-app sub-command and create your new application. Keep in mind that your job is not only to create an app but also making it distributable.

To make your application distributable, you should have a parent directory. Here is how you do it.

mkdir django-myapp
cd django-myapp
django-admin.py startapp myapp

You do not need to do anything fancy. Follow this guide and write specific codes and you will be fine. I will show you the way to test it later in this guide.

It is now a time for creating “Readme” and “License” file to give a clear description of your project. It is not wise to go ahead without these files.

touch README.rst
touch LICENSE

It is also great to monitor packages with VCS like git:

git init

2. Time for Packaging

You should create your application like a package for distributing your app/lib. However, you need to put “setup. Py” file inside the “Django-myapp” directory (side of a myapp directory).

The content below should include some crucial pieces of information like your package, name, version, description, and so on.

    import os
    from setuptools import setup
    here = os.path.abspath(os.path.dirname(__file__))
    README = open(os.path.join(here, 'README.rst')).read()

    setup(
      name='django-myapp'
      version='0.1'
      package=['myapp'],
      description='A line of description',
      long_description=README,
      author='yourname',
      author_email='yourname@example.com',
      url='https://github.com/yourname/django-myapp/',
      license='MIT',
      install_requires=[
        'Django>=1.6,<1.7'
      ]
    )

Code Source: GitHub

Your content should have at least these pieces of information:

  • Your package name like Django-cms. You should specify the name of your package by pip. In case your package name is “Django-myapp,” others need to use “pip install Django-myapp.”
  • Include the package version. You can use semantic versioning for now.
  • Packages: You should mention the Python modules that you are about to use. For now, you can specify your Django application.
  • Include your description line. For instance, you can use PyPI on package list.
  • Your name as an author
  • Email address of an author, i.e., your email address
  • Mention the URL of your package.
  • Mention your license name. For instance, you can use “MIT,” which means the MIT license.
  • Install_requires: Write down packages that you are about to use. For Django package, you should mention at least ‘Django.’

You also need “MANIFEST. In” file inside your project directory for specifying the list of files for distribution.

include *.text *.ini *.cfg *.rst
recursive-include myapp *.ico *.png *.css *.gif *.jpg *txt *.js *.html *.xml

Code Source: GitHub

You will not be able to distribute the application without this file. Check out this official document for more detail about specifying a file.

Now, you should register a new package after creating “setup.py” with this command.

python setup.py develop

Here is a document if you want to know more about “setup.py.”

For uploading package to PyPI, here is a way:

python setup.py register
python setup.py sdist upload

Here is a guide to know more about Python packaging.

3. You can test your application without projects

In general, Django needs plans for almost all actions. However, we can run tests without projects if we use “Django-admin. Py”. To do that, you need to create environments for testing (do that on the project root).

touch test_settings.py

Your content will look like this:

INSTALLED_APPS = (

'myapp',

)

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.sqlite3',

'NAME': ':memory:',

}

}

SECRET_KEY = "secret_key_for_testing"

Code Source: GitHub

It is also possible to put additional settings to correspond your application test. Here is a guide to learn more about test optimization.

For running the test in this setting, insert this command inside the project root.

Django-admin.py test --settings=test_settings

4. Enlarge your reach with tox

You can use tox to make your package supportive for more than one version of Django and Python.

Check out this case where we are using “tox.ini” for supporting Python, Python 2.7 and 3.3.

touch tox.ini

next steps:

[tox]
envlist=py27, py33, flake8

[testenv]
commands=
  pip install -e
  django-admin.py test --settings=test_settings

Code Source: GitHub

Install tox and run your tests:

pip install tox

tox

You will see three bunches of tests running with flake 8, Python 2.7 and 3.3.

Here are the guides to know more about tox and flake 8.

Your Next Step

Now that you are aware of the steps to create your own Python library stop procrastination and get started right away. I’ve written this guide for people who want to get started with the library creation process with Python Django.

Of course, there is a lot more to explore libraries and Django. However, you can take this article as a base for getting started. If you guys have some of your Django projects that you want to talk about, I would love to hear from you. Let me know in the comment.

Moesif is the most advanced API Analytics platform, supporting REST, GraphQL, Web3 Json-RPC and more. Over two thousand companies process billions of API calls through Moesif for deep insights into their platforms.

Learn More

Leave a comment