My scrapbook about anything which I learned or want to remember, Sometime about tech tips, thoughts and rambling. If you find anything useful don't forget to give thumbs-up :)

Breaking

Wednesday, January 20, 2016

Import your own python library


How to use your own written functions or routines when code in python??


Follow below steps for auto import - 
1. Create a folder where you can put all reusable code/function/routines files
2. Let's say it is "routines"
3. Now suppose, you have written all your functions and save it in a file myfunc.py and saved it in routines folder
4. Add routines folder path to your windows path or linux path
for linux:
Edit your .bash_profile (typically towards the end)to the following line
 
export PYTHON_PATH=$PYTHON_PATH:'/path/to/folder/'   #if you have this variable
export PATH=$PATH:'/path/to/folder/'   #else use  this line

 where you put the correct path in the appropriate location


Follow below steps for manual import -
Use below python code to manually import the module file






http://www.datagenx.net/2018/01/python-pickle-to-save-your-efforts.html


How to use your function in your code:-
1.  import that module into your python script session with a command like
import myfunc

2.  for using a function "my_sqrt" from your library myfunc
x = myfunc.my_sqrt(val)

3.  you can create alias for your library also
import myfunc as mf
x = mf.my_sqrt(val)

If want to import a particular peice from library, use as
from myfunc import my_sqrt
x = my_sqrt(val)

this is tedious if you have to import multiple so use as
from myfunc import *
x = my_sqrt(val)

But, remember if you are import multiple library and they having same function name.
While using, you need to call them as step 2





Like the below page to get update  
https://www.facebook.com/datastage4you
https://twitter.com/datagenx
https://plus.google.com/+AtulSingh0/posts
https://groups.google.com/forum/#!forum/datagenx

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.