Tuesday, November 12, 2013

How to import functions in another file to your current file in python

If you are working on a code (sample.py) that uses functions of another file (simpleops.py) in the same directory, you can simply call "import simpleops" and then use the functions in that file as "simpleops.()" as shown in the below example.

Example: 

Your current program (sample.py) :

#!/usr/bin/env python
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



If the another file is in different directory compared to your current file, then you should first import its directory path and then import the file like: "sys.path.insert(0, FILEPATH) import simpleops" as shown in the below example. Note that the value '0' in sys.path.insert means it first searches the modules in the current directory first from the program has been called. 

Example: 

Your current program (sample.py):

#!/usr/bin/env python
import sys
sys.path.insert(0, FILEPATH)
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



For example lets say, your sample code (simpleops.py) looks like this:

#!/usr/bin/env python

def mult(a,b):
        return(a*b)

def sum(a,b):
        return(a+b)


Monday, November 11, 2013

How to install maven on linux?

Step-1: Download the maven binary package from http://maven.apache.org/download.cgi

Step-2: Extract the tar file: tar -xvzf apache-maven-3.1.1-bin.tar.gz

Step-3: Create a folder in /usr/local with the name apache-maven and extract the above tar file in this directory which gives the file structure as: /usr/local/apache-maven/apache-maven-3.1.1

Step-4: Set your path variables as below:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Step-5: To check if maven is successfully installed: mvn --version