kracekumar

How to import custom module / package in Python

In Linux, Python on September 15, 2011 at 07:32

Python keeps track of all the packages/modules/libraries included by installation, pip or easy_install using sys.path.

When one types import sys, python looks up in paths contained in sys.path and resolves  it.

Lets us have look at the value of sys.path

<pre>kracekumar@python-lover:~$ ipython
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
Type "copyright", "credits" or "license" for more information.

IPython 0.10.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import sys

In [2]: sys.path
Out[2]: 
['',
 '/usr/local/bin',
 '/usr/local/lib/python2.7/dist-packages/pip-1.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/greenlet-0.3.1-py2.7-linux-i686.egg',
 '/usr/local/lib/python2.7/dist-packages/tweepy-1.7.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mysite-3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_model_utils-0.6.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/sessionprofile-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_openid-2.2.5_openhatch1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/lockfile-0.9.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/html2text-3.02-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_launchpad_bugs-0.3.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_otp-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/staticgenerator-1.4.1.2_openhatch-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_voting-0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_registration-0.8_alpha_1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/hexagonit.recipe.download-1.5.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_haystack-1.2.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/sisynala-0.9.19-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/South-0.7.3.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pysolr-2.0.14-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/keyring-0.5.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_debug_toolbar-0.8.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pygeoip-0.1.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_github2-0.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_assets-0.2.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/celery-1.0.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_authopenid-1.0.1_openhatch2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/invitation-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Traits-3.6.0-py2.7-linux-i686.egg',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.10.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-1.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_twitter-0.6_devel-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask-0.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_SQLAlchemy-0.11-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/newsmeme-0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/blinker-1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/feedparser-5.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Markdown-2.0.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Themes-0.1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Babel-0.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_OpenID-1.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Mail-0.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Principal-0.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Cache-0.3.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/speaklater-1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/lamson-1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_daemon-1.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mock-0.8.0alpha1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/pymodules/python2.7/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7/ubuntuone-client',
 '/usr/lib/pymodules/python2.7/ubuntuone-control-panel',
 '/usr/lib/pymodules/python2.7/libubuntuone',
 '/usr/lib/pymodules/python2.7/ubuntuone-storage-protocol',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.10.2-py2.7.egg/IPython/Extensions',
 u'/home/kracekumar/.ipython']</pre>

Note: Here Ipython is used .

Here  sys.path is list , in case custom path is missing,  append the list.

<pre>In [3]: sys.path.append(u'/home/kracekumar/codes/python/snippets/custom/')

In [4]: sys.path
Out[4]: 
['',
 '/usr/local/bin',
 '/usr/local/lib/python2.7/dist-packages/pip-1.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/greenlet-0.3.1-py2.7-linux-i686.egg',
 '/usr/local/lib/python2.7/dist-packages/tweepy-1.7.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mysite-3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_model_utils-0.6.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/sessionprofile-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_openid-2.2.5_openhatch1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/lockfile-0.9.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/html2text-3.02-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_launchpad_bugs-0.3.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_otp-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/staticgenerator-1.4.1.2_openhatch-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_voting-0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_registration-0.8_alpha_1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/hexagonit.recipe.download-1.5.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_haystack-1.2.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/sisynala-0.9.19-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/South-0.7.3.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pysolr-2.0.14-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/keyring-0.5.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_debug_toolbar-0.8.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pygeoip-0.1.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_github2-0.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_assets-0.2.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/celery-1.0.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_authopenid-1.0.1_openhatch2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/invitation-1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Traits-3.6.0-py2.7-linux-i686.egg',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.10.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-1.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_twitter-0.6_devel-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask-0.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_SQLAlchemy-0.11-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/newsmeme-0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/blinker-1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/feedparser-5.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Markdown-2.0.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Themes-0.1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Babel-0.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_OpenID-1.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Mail-0.6.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Principal-0.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Flask_Cache-0.3.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/speaklater-1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Babel-0.9.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/lamson-1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_daemon-1.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/mock-0.8.0alpha1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/pymodules/python2.7/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7/ubuntuone-client',
 '/usr/lib/pymodules/python2.7/ubuntuone-control-panel',
 '/usr/lib/pymodules/python2.7/libubuntuone',
 '/usr/lib/pymodules/python2.7/ubuntuone-storage-protocol',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.10.2-py2.7.egg/IPython/Extensions',
 u'/home/kracekumar/.ipython',
 u'/home/kracekumar/codes/python/snippets/custom/']</pre>

Take a look at the last element of the list, newly added path is updated.

 

<pre>kracekumar@python-lover:~/codes/python/snippets/custom$ ls
like.py  like.pyc</pre>

My custom path has one python file named like.py.

Note: This is not demo for packaging, in that case package structure is different.

The source code of like.py.

<pre>kracekumar@python-lover:~/codes/python/snippets/custom$ cat like.py
like = 'I love india'</pre>

Now try importing the module.

<pre>In [5]: import like

In [6]: like.like
Out[6]: 'I love india'</pre>

Be careful while handling sys.path.

You can be successful without FOSS, but You can’t make it big without FOSS

In Free Software, GNU/Linux, Linux, Tech News on August 21, 2011 at 15:51

GNU/Linux has revolutionized the world with new era of computing.GNU/Linux is omnipresent next to air, but most people don’t know it.Internet runs on GNU/Linux.

Read Complete article red-hat-ceo-google-facebook-owe-it-all-to-linux-open-source

What time do developers / coders usually go to sleep on a daily basis?

In Uncategorized on August 17, 2011 at 09:03

What time do developers / coders usually go to sleep on a daily basis? 9 answers on Quora

What time do developers / coders usually go to sleep on a daily basis?

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: