Python – How do I shorten import statements when developing Python packages?

How do I shorten import statements when developing Python packages?… here is a solution to the problem.

How do I shorten import statements when developing Python packages?

I’m designing a Python package. Take a look at the project structure below

└── android_py

Here's what

from setuptools import setup, find_packages

      description='The description goes here',
      author_email='[email protected]',

Using Python you can successfully install the above packages. However, in order to use this package, I need to write a long import statement like this-

from import Android
from android_py.options import Power

my_robot = Android()

As you can see, there are two problems –

  1. The first import, import Android, is too long and user-friendly because it’s hard to remember. I think shorter things like import android are much better.
  2. The second import, from android_py.options import Power, is cumbersome. It should be imported automatically on the first import.

Could you please suggest how I can reconfigure this package to overcome the above issue? Note that I’m using Python 2.7 (if important)!


In addition to my comment, I will try to give a short example. Let’s say you have a

class Power:
   On = True 

There is a in the same package:

from . import power

class Android:
    Power = power. Power

In the android_py package

from .android import Android

Now, from outside of your, or whatever you can:

from android_py import Android
my_robot = Android()
my_robot.set_power(my_robot. Power.On)

By the way: I’m not very happy with the package name android_py. Also named Android, having a in the Android package is no problem. Or explain with a path name: there is no problem with android/android .py. Use relative imports in and in the example above . It should work.

Related Problems and Solutions