Definition:
If you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.[R]
What is Module:
Modules are basically the py.file which contains all your code constructs in the form of variables, functions, classes, or executable statements.
How to create Modules:
If you save your file as .py extension that has your code constructs in the form of variables, functions, classes that becomes your modules.
Example:
As shown in the example, the file MathsModule.py file is located at location.E:\Software\Google Drive\PRWATECH\BATCH-A\MathsModule.py.
How to call Module:
Once the module is created, all you have to go to client script .py file and use the below code to import the module.
import MathsModule
As shown in the image if you are able to import the MathsModule in PythonShell, this means that module is imported successfully.
How to call the functions of Module:
You can call functions of module by is module name.
In the similar way if there are any variables define in your Module we can call the variables by its name.
What can be the name of Module:
Literally anything with extention of .py.
Whats an Alias:
In our example, the name of module is MathsModule. This seems to be very long, and when ever you want to call functions form module the long name is bit tiresome. So rather than calling the module name, instead call the functions with its alias name.
here m is alias.
Now we understood, How to create modules and using it, let see , how to call some built in modules within python.
import threading #This is module
import time #This is module
x=0
def M1():
global x
lock.acquire()
for i in range(1,10):
time.sleep(0.1)
x=x+10
print("M1 Executed in
thread id::",threading.current_thread().ident,"With value of
x::",x)
lock.release()
##def M2():
## for i in range(1,5):
## time.sleep(1)
## print("M2 Executed in
thread id::",threading.current_thread().ident)
lock=threading.Lock()
t1=threading.Thread(target=M1)
t2=threading.Thread(target=M1)
print("Main Thread Starts in thread
id::",threading.current_thread().ident)
t1.start()
t2.start()
t1.join()
t2.join()
print("Main Thread ends in thread
id::",threading.current_thread().ident)
How to import part of functionality from the module:
Lets say there are two functions in the MathsModule Add and Sub, and we want only add function to be called in client code.
ClientScript.py.
In the above code we can call only Add Method.
We cannot call Sub method, variable a, and b.If we try to call any other part of the module we will the errors.
When we run the above code, to access Sub Method directly or via Module name, we get the errors.
Calling Two functions from the module:
Lets say your module has three functions, and we want to call only two functions. In this case we have to import the function by comma separated.
You wont be able to call Div Function as it is not imported.
1) A module is loaded only once, regardless of the number of times it is imported. This
prevents the module execution from happening repeatedly, if multiple imports occur.
2) if we want to explicitly load the modules then we have to run the the below code.
We can see clearly 10,20 is printed again in the last line when module is reloaded.
3) Modules Can be used as Scripts:
Apart from having functions with in the modules, it can also have executable code.
Example in below MathsModule.py, you can see variables x,y is added.
Now if we simply import the module we see execution of x+y and result of x+y 1887, which is not required at all.
What we want is the execution of the code should not be happening at import of module.
But at the same time we also want the execution of code in module.
These two constraint can be achieved by simply putting the executable code in main method.
We can see the code is executed and result is printed as 1887.
The Module Search Path:
When a module named MathsModule is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named
How this works:
1) When the MathsModule.py and clientscript.py file is located in the same location, then clientscript.py is successfully able to import MathsModule and the code is executed successfully.
2) When the MathsModule.py is not available at location where clientscript.py file is present, the it gives the error.
3) When MathsModule.py is not available at location where clientscript.py file and is present at the installation-dependent default, here in my case it is C:\Users\user\AppData\Local\Programs\Python\Python38-32. Ideally this path contains python.exe.
Apart from having functions with in the modules, it can also have executable code.
Example in below MathsModule.py, you can see variables x,y is added.
Now if we simply import the module we see execution of x+y and result of x+y 1887, which is not required at all.
What we want is the execution of the code should not be happening at import of module.
But at the same time we also want the execution of code in module.
These two constraint can be achieved by simply putting the executable code in main method.
Run the python.exe "E:\Software\Google Drive\PRWATECH\BATCH-A\MathsModule.py"We can see after putting executable in main method, if we import the Module, x+y is not executed.However we still wants to execute x+y within the module.For this we have to execute the module from the command prompt.Open the command prompt and navigate to the path where python is installed.
We can see the code is executed and result is printed as 1887.
The Module Search Path:
When a module named MathsModule is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named
MathsModule.py
in a list of directories given by the Variable sys.path
. sys.path
is initialized from these locations:- The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH
(a list of directory names, with the same syntax as the shell variablePATH
).- The installation-dependent default.
PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.
The only reason to set PYTHONPATH is to maintain directories of custom Python libraries that you do not want to install in the global default location (i.e., the site-packages directory).[R]
How this works:
1) When the MathsModule.py and clientscript.py file is located in the same location, then clientscript.py is successfully able to import MathsModule and the code is executed successfully.
2) When the MathsModule.py is not available at location where clientscript.py file is present, the it gives the error.
3) When MathsModule.py is not available at location where clientscript.py file and is present at the installation-dependent default, here in my case it is C:\Users\user\AppData\Local\Programs\Python\Python38-32. Ideally this path contains python.exe.
4) When MathsModule.py is not available at location where clientscript.py file and is present at the installation-dependent default, here in my case it is C:\Users\user\AppData\Local\Programs\Python\Python38-32\Lib. Ideally this path contains python provided standard libraries.
No comments:
Post a Comment