Search

How To Run Your Python Scripts

If you are planning to enter the world of Python programming, the first and the most essential skill you should learn is knowing how to run Python scripts and code. Once you grab a seat in the show, it will be easier for you to understand whether the code will actually work or not.Python, being one of the leading programming languages, has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. Also, it is the language of choice for working with large datasets and data science projects. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.What is the difference between Code, Script and Modules?In computing, the code is a language that is converted from a human language into a set of ‘words’ which the computer can understand. It is also referred to as a piece of statements together which forms a program. A simple function or a statement can also be considered a code.On the other hand, a script is a file consisting of a logical sequence of instructions or a batch processing file that is interpreted by another program instead of the computer processor.In simple terms, a script is a simple program, stored in a plain file text which contains Python code. The code can be directly executed by the user. A script is also called a top-level-program-file. A module is an object in Python with random attributes that you can bind and reference.Is Python a Programming Language or a Scripting Language?Basically, all scripting languages are considered to be programming languages. The main difference between the two is that programming languages are compiled, whereas scripting languages are interpreted. Scripting languages are slower than programming languages and usually sit behind them. Since they only run on a subset of the programming language, they have less access to a computer’s local abilities. Python can be called a scripting language as well as a programming language since it works both as a compiler and an interpreter. A standard Python can compile Python code into bytecodes and then interpret it just like Java and C.However, considering the historical relationship between the general purpose programming language and the scripting language, it will be more appropriate to say that Python is a general-purpose programming language which works nicely as a scripting language too.The Python InterpreterThe Interpreter is a layer of software that works as a bridge between the program and the system hardware to keep the code running. A Python interpreter is an application which is responsible for running Python scripts.The Python Interpreter works on the Read-Eval-Print-Loop (REPL) environment.Reads the command.Evaluates the command.Prints the result.Loops back and process gets repeated.The interpreter terminates when we use the exit() or quit() command otherwise the execution keeps on going.A Python Interpreter runs code in two ways— In the form of a script or module.In the form of a piece of code written in an interactive session.Starting the Python InterpreterThe simplest way to start the interpreter is to open the terminal and then use the interpreter from the command-line.To open the command-line interpreter:On Windows, the command-line is called the command prompt or MS-DOS console. A quicker way to access it is to go to Start menu → Run and type cmd.On GNU/Linux, the command-line can be accessed by several applications like xterm, Gnome Terminal or Konsole.On MAC OS X, the system terminal is accessed through Applications → Utilities → Terminal. Running Python Code InteractivelyRunning Python code through an interactive session is an extensively used way. An interactive session is an excellent development tool to venture with the language and allows you to test every piece of Python code on the go.To initiate a Python interactive session, type python in the command-line or terminal and hit the ENTER key from the keyboard.An example of how to do this on Windows:C:\users>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>>The >>> on the terminal represents the standard prompt for the interactive mode. If you do not see these characters, you need to re-install Python on your system.The statements you write when working with an interactive session are evaluated and executed immediately:print('HELLO WORLD!') HELLO WORLD! 2 + 3 5 print('Welcome to the world of PYTHON') Welcome to the world of PYTHON The only disadvantage is when you close the interactive session, the code no longer exists.Running Python Scripts by the InterpreterThe term Python Execution Model is given to the entire multi-step process to run Python scripts.At first, the statements or expressions of your script are processed in a sequential manner by the interpreter. Then the code is compiled into a form of instruction set called the bytecode.Basically, the code is converted into a low-level language known as the bytecode. It is an intermediate, machine-independent code which optimizes the process of code execution. So, the interpreter ignores the compilation step when executing the code for the next time.Finally, the interpreter transfers the code for execution.The Python Virtual Machine (PVM) is the ultimate step of the Python interpreter process. It is a part of the Python environment installed in your system. The PVM loads the bytecode in the Python runtime and reads each operation and executes them as indicated. It is the component which actually runs your scripts.Running Python Scripts using Command-LineThe most sought after way of writing a Python program is by using a plain text editor. The code written in the Python interactive session is lost once the session is closed, though it allows the user to write a lot of lines of code. On Windows, the files use the .py extension.  If you are at the beginning of working with Python, you can use editors like Sublime or Notepad++ which are easy-to-use or any other text editors.Now you need to create a test script. In order to do that, open your most suited text editor and write the following code:print('Hello World!')Then save the file in your desktop with the name first_script.py or anything you like. Remember you need to give the .py extension only.Using python commandThe most basic and the easy way to run Python scripts is by using the python command. You need to open a command-line and type the word python followed by the path to your script file, like this:python first_script.py Hello World!Then you hit the ENTER button from the keyboard and that's it. You can see the phrase Hello World! on the screen. Congrats! You just ran your first Python script. However, if you do not get the output, you might want to check your system PATH and the place where you saved your file. If it still doesn’t work, re-install Python in your system and try again.Redirecting outputWindows and Unix-like systems have a process called stream redirection. You can redirect the output of your stream to some other file format instead of the standard system output. It is useful to save the output in a different file for later analysis.An example of how you can do this:python first_script.py > output.txtWhat happens is your Python script is redirected to the output.txt file. If the file doesn’t exist, it is systematically created. However, if it already exists, the contents are replaced.Running modules with the -m optionA module is a file which contains the Python code. It allows you to arrange your Python code in a logical manner. It defines functions, classes, and variables and can also include runnable code.If you want to run a Python module, there are a lot of command-line options which Python offers according to the needs of the user. One of which is the command  python -m <module-name>. It searches the module name in the sys.path and runs the content as __main__:python -m first_script Hello World!Note that the module-name is a module object and not any string.Using Script FilenameWindows makes use of the system registers and file association to run Python scripts. It determines the program needed to run that particular file. You need to simply enter the file-name containing the code.An example on how to do this using command prompt:C:\Users\Local\Python\Python37> first_script.py Hello World!On GNU/Linux systems, you need to add a line before the text— #!/usr/bin/env python. Python considers this line nothing but the operating system considers it everything. It helps the system to decide what program should it use to run the file.The character combination #! known as hashbang or shebang is what the line starts with, which is then followed by the interpreter path.Finally, to run scripts, assign execution permissions and configure the hashbang line and then simply type the filename in the command line:#Assign the execution permissions chmod +x first_script.py #Run script using its filename ./first_script.py Hello World!However, if it doesn’t work, you might want to check if the script is located in your currentworking directory or not. Otherwise, you can use the path of the file for this method. Running Python Scripts InteractivelyAs we have discussed earlier, running Python scripts in an interactive session is the most common way of writing scripts and also offers a wide range of possibilities.Using importImporting a module means loading its contents so that it can be later accessed and used. It is the most usual way of invoking the import machinery. It is analogous to #include in C or C++. Using import, the Python code in one module gets access to the code in another module. An implementation of the import:import first_script Hello World!You can see its execution only when the module contains calls to functions, methods or other statements which generate visible output.One important thing to note is that the import option works only once per session. This is because these operations are expensive.For this method to work efficiently, you should keep the file containing the Python code in your current working directory and also the file should be in the Python Module Search Path (PMSP). The PMSP is the place where the modules and packages are imported.You can run the code below to know what’s in your current PSMP:import sys for path in sys.path: print(path)\Users\Local\Python37\Lib\idlelib \Users\Local\Python37\python37.zip \Users\Local\Python37\DLLs \Users\Local\Python37\lib \Users\Local\Python37 \Users\Local\Python37\lib\site-packagesYou’ll get the list of directories and .zip files where your modules and packages are imported.Using importlibimportlib is a module which is an implementation of the import statement in the Python code. It contains the import_module whose work is to execute any module or script by imitating the import operation.An example to perform this:import importlib importlib.import_module('first_script') Hello World! <module 'first_script' from '\Users\Local\Python37\first_script.py'>importlib.reload() is used to re-import the module since you cannot use import to run it for the second time. Even if you use import after the first time, it will do nothing. importlib.reload() is useful when you want to modify and test your changes without exiting the current session.The following code shows that:import first_script #First import Hello World! import first_script import importlib #Second import does nothing importlib.reload(first_script) Hello World! <module 'first_script' from '\Users\Local\Python37\\first_script.py'>However, you can only use a module object and not any string as the argument of reload(). If you use a string as an argument, it will show a TypeError as follows:importlib.reload(first_script)Traceback (most recent call last): ... ...   raise TypeError("reload() argument must be a module") TypeError: reload() argument must be a moduleUsing runpy.run_module() and runpy.run_path()The Python Standard Library has a module named runpy. run_module() is a function in runpy whose work is to execute modules without importing them in the first place. The module is located using import and then executed. The first argument of the run_module() must contain a string:import runpy runpy.run_module(mod_name='first_script') Hello World! {'__name__': 'first_script',     ... '_': None}}Similarly, runpy contains another function run_path() which allows you to run a module by providing a location.An example of such is as follows:import runpy runpy.run_path(file_path='first_script.py') Hello World! {'__name__': '<run_path>',     ... '_': None}}Both the functions return the globals dictionary of the executed module.Using exec()Other than the most commonly used ways to run Python scripts, there are other alternative ways. One such way is by using the built-in function exec(). It is used for the dynamic execution of Python code, be it a string or an object code.An example of exec() is:exec(open('first_script.py').read()) Hello World!Using py_compilepy_compile is a module which behaves like the import statement. It generates two functions— one to generate the bytecode from the source file and another when the source file is invoked as a script.You can compile your Python script using this module:import py_compile py_compile.compile('first_script.py'  '__pycache__\\first_script.cpython-37.pyc' The py_compile generates a new subdirectory named "__pycache__" if it doesn’t already exist. Inside the subdirectory, a Compiled Python File (.pyc) version of the script file is created. When you open the .pyc file, you can see the output of your Python script.Running Python Scripts using an IDE or a Text EditorAn Integrated Development Environment (IDE) is an application that allows a developer to build software within an integrated environment in addition to the required tools.You can use the Python IDLE, a default IDE of the standard Python Distribution to write, debug, modify, and run your modules and scripts. You can use other IDEs like Spyder, PyCharm, Eclipse, and Jupyter Notebook which also allow you to run your scripts inside its environment.You can also use popular text editors like Sublime and Atom to run Python scripts.If you want to run a Python script from your IDE or text editor, you need to create a project first. Once it is created, add your .py file to it or you can just simply create one using the IDE. Finally, run it and you can see the output in your screen.Running Python Scripts from a File ManagerIf you want to run your Python script in a file manager, all you need to do is just double-click on the file icon. This option is mainly used in the production stage after you have released the source code.However, to achieve this, some conditions must be met:On Windows, to run your script by double-clicking on them, you need to save your script file with extension .py for python.exe and .pyw for pythonw.exe.If you are using the command-line for running your script, you might likely come  through a situation where you’ll see a flash of a black window on the screen. To avert this, include a statement at the tail of the script — input(‘Enter’). This will exit the program only when you hit the ENTER key. Note that the input() function will work only if your code is free of errors.On GNU/Linux and other Unix-like systems, your Python script must contain the hashbang line and execution permissions. Otherwise, the double-click trick won’t work in a file manager.Though it is easy to execute a script by just double-clicking on the file, it isn’t considered a feasible option because of the limitations and dependency factors it comes with, like the operating system, the file manager, execution permissions, and also the file associations.So it is suggested to use this option only after the code is debugged and ready to be in the production market.ConclusionWorking with scripts has its own advantages like they are easy to learn and use, faster edit and run, interactivity, functionality and so on. They are also used to automate complex tasks in a simplified manner.In this article, you have learned to run your Python scripts using:The terminal or the command-line of the operating system.The Python Interactive session.Your favorite IDE or text editor.The system file manager.Here, you have gathered the knowledge and skills of how to run your scripts using various techniques.You will feel more comfortable working with larger and more complex Python environments which in turn will enhance the development process and increase efficiency. You can learn more about such techniques as KnowledgeHut offers Python Certification Course.
How To Run Your Python Scripts
Priyankur
Rated 4.5/5 based on 19 customer reviews

How To Run Your Python Scripts

If you are planning to enter the world of Python programming, the first and the most essential skill you should learn is knowing how to run Python scripts and code. Once you grab a seat in the show, it will be easier for you to understand whether the code will actually work or not.Python, being one of the leading programming languages, has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. Also, it is the language of choice for working with large datasets and data science projects. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.What is the difference between Code, Script and Modules?In computing, the code is a language that is converted from a human language into a set of ‘words’ which the computer can understand. It is also referred to as a piece of statements together which forms a program. A simple function or a statement can also be considered a code.On the other hand, a script is a file consisting of a logical sequence of instructions or a batch processing file that is interpreted by another program instead of the computer processor.In simple terms, a script is a simple program, stored in a plain file text which contains Python code. The code can be directly executed by the user. A script is also called a top-level-program-file. A module is an object in Python with random attributes that you can bind and reference.Is Python a Programming Language or a Scripting Language?Basically, all scripting languages are considered to be programming languages. The main difference between the two is that programming languages are compiled, whereas scripting languages are interpreted. Scripting languages are slower than programming languages and usually sit behind them. Since they only run on a subset of the programming language, they have less access to a computer’s local abilities. Python can be called a scripting language as well as a programming language since it works both as a compiler and an interpreter. A standard Python can compile Python code into bytecodes and then interpret it just like Java and C.However, considering the historical relationship between the general purpose programming language and the scripting language, it will be more appropriate to say that Python is a general-purpose programming language which works nicely as a scripting language too.The Python InterpreterThe Interpreter is a layer of software that works as a bridge between the program and the system hardware to keep the code running. A Python interpreter is an application which is responsible for running Python scripts.The Python Interpreter works on the Read-Eval-Print-Loop (REPL) environment.Reads the command.Evaluates the command.Prints the result.Loops back and process gets repeated.The interpreter terminates when we use the exit() or quit() command otherwise the execution keeps on going.A Python Interpreter runs code in two ways— In the form of a script or module.In the form of a piece of code written in an interactive session.Starting the Python InterpreterThe simplest way to start the interpreter is to open the terminal and then use the interpreter from the command-line.To open the command-line interpreter:On Windows, the command-line is called the command prompt or MS-DOS console. A quicker way to access it is to go to Start menu → Run and type cmd.On GNU/Linux, the command-line can be accessed by several applications like xterm, Gnome Terminal or Konsole.On MAC OS X, the system terminal is accessed through Applications → Utilities → Terminal. Running Python Code InteractivelyRunning Python code through an interactive session is an extensively used way. An interactive session is an excellent development tool to venture with the language and allows you to test every piece of Python code on the go.To initiate a Python interactive session, type python in the command-line or terminal and hit the ENTER key from the keyboard.An example of how to do this on Windows:C:\users>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>>The >>> on the terminal represents the standard prompt for the interactive mode. If you do not see these characters, you need to re-install Python on your system.The statements you write when working with an interactive session are evaluated and executed immediately:print('HELLO WORLD!') HELLO WORLD! 2 + 3 5 print('Welcome to the world of PYTHON') Welcome to the world of PYTHON The only disadvantage is when you close the interactive session, the code no longer exists.Running Python Scripts by the InterpreterThe term Python Execution Model is given to the entire multi-step process to run Python scripts.At first, the statements or expressions of your script are processed in a sequential manner by the interpreter. Then the code is compiled into a form of instruction set called the bytecode.Basically, the code is converted into a low-level language known as the bytecode. It is an intermediate, machine-independent code which optimizes the process of code execution. So, the interpreter ignores the compilation step when executing the code for the next time.Finally, the interpreter transfers the code for execution.The Python Virtual Machine (PVM) is the ultimate step of the Python interpreter process. It is a part of the Python environment installed in your system. The PVM loads the bytecode in the Python runtime and reads each operation and executes them as indicated. It is the component which actually runs your scripts.Running Python Scripts using Command-LineThe most sought after way of writing a Python program is by using a plain text editor. The code written in the Python interactive session is lost once the session is closed, though it allows the user to write a lot of lines of code. On Windows, the files use the .py extension.  If you are at the beginning of working with Python, you can use editors like Sublime or Notepad++ which are easy-to-use or any other text editors.Now you need to create a test script. In order to do that, open your most suited text editor and write the following code:print('Hello World!')Then save the file in your desktop with the name first_script.py or anything you like. Remember you need to give the .py extension only.Using python commandThe most basic and the easy way to run Python scripts is by using the python command. You need to open a command-line and type the word python followed by the path to your script file, like this:python first_script.py Hello World!Then you hit the ENTER button from the keyboard and that's it. You can see the phrase Hello World! on the screen. Congrats! You just ran your first Python script. However, if you do not get the output, you might want to check your system PATH and the place where you saved your file. If it still doesn’t work, re-install Python in your system and try again.Redirecting outputWindows and Unix-like systems have a process called stream redirection. You can redirect the output of your stream to some other file format instead of the standard system output. It is useful to save the output in a different file for later analysis.An example of how you can do this:python first_script.py > output.txtWhat happens is your Python script is redirected to the output.txt file. If the file doesn’t exist, it is systematically created. However, if it already exists, the contents are replaced.Running modules with the -m optionA module is a file which contains the Python code. It allows you to arrange your Python code in a logical manner. It defines functions, classes, and variables and can also include runnable code.If you want to run a Python module, there are a lot of command-line options which Python offers according to the needs of the user. One of which is the command  python -m . It searches the module name in the sys.path and runs the content as __main__:python -m first_script Hello World!Note that the module-name is a module object and not any string.Using Script FilenameWindows makes use of the system registers and file association to run Python scripts. It determines the program needed to run that particular file. You need to simply enter the file-name containing the code.An example on how to do this using command prompt:C:\Users\Local\Python\Python37> first_script.py Hello World!On GNU/Linux systems, you need to add a line before the text— #!/usr/bin/env python. Python considers this line nothing but the operating system considers it everything. It helps the system to decide what program should it use to run the file.The character combination #! known as hashbang or shebang is what the line starts with, which is then followed by the interpreter path.Finally, to run scripts, assign execution permissions and configure the hashbang line and then simply type the filename in the command line:#Assign the execution permissions chmod +x first_script.py #Run script using its filename ./first_script.py Hello World!However, if it doesn’t work, you might want to check if the script is located in your currentworking directory or not. Otherwise, you can use the path of the file for this method. Running Python Scripts InteractivelyAs we have discussed earlier, running Python scripts in an interactive session is the most common way of writing scripts and also offers a wide range of possibilities.Using importImporting a module means loading its contents so that it can be later accessed and used. It is the most usual way of invoking the import machinery. It is analogous to #include in C or C++. Using import, the Python code in one module gets access to the code in another module. An implementation of the import:import first_script Hello World!You can see its execution only when the module contains calls to functions, methods or other statements which generate visible output.One important thing to note is that the import option works only once per session. This is because these operations are expensive.For this method to work efficiently, you should keep the file containing the Python code in your current working directory and also the file should be in the Python Module Search Path (PMSP). The PMSP is the place where the modules and packages are imported.You can run the code below to know what’s in your current PSMP:import sys for path in sys.path: print(path)\Users\Local\Python37\Lib\idlelib \Users\Local\Python37\python37.zip \Users\Local\Python37\DLLs \Users\Local\Python37\lib \Users\Local\Python37 \Users\Local\Python37\lib\site-packagesYou’ll get the list of directories and .zip files where your modules and packages are imported.Using importlibimportlib is a module which is an implementation of the import statement in the Python code. It contains the import_module whose work is to execute any module or script by imitating the import operation.An example to perform this:import importlib importlib.import_module('first_script') Hello World! importlib.reload() is used to re-import the module since you cannot use import to run it for the second time. Even if you use import after the first time, it will do nothing. importlib.reload() is useful when you want to modify and test your changes without exiting the current session.The following code shows that:import first_script #First import Hello World! import first_script import importlib #Second import does nothing importlib.reload(first_script) Hello World! However, you can only use a module object and not any string as the argument of reload(). If you use a string as an argument, it will show a TypeError as follows:importlib.reload(first_script)Traceback (most recent call last): ... ...   raise TypeError("reload() argument must be a module") TypeError: reload() argument must be a moduleUsing runpy.run_module() and runpy.run_path()The Python Standard Library has a module named runpy. run_module() is a function in runpy whose work is to execute modules without importing them in the first place. The module is located using import and then executed. The first argument of the run_module() must contain a string:import runpy runpy.run_module(mod_name='first_script') Hello World! {'__name__': 'first_script',     ... '_': None}}Similarly, runpy contains another function run_path() which allows you to run a module by providing a location.An example of such is as follows:import runpy runpy.run_path(file_path='first_script.py') Hello World! {'__name__': '',     ... '_': None}}Both the functions return the globals dictionary of the executed module.Using exec()Other than the most commonly used ways to run Python scripts, there are other alternative ways. One such way is by using the built-in function exec(). It is used for the dynamic execution of Python code, be it a string or an object code.An example of exec() is:exec(open('first_script.py').read()) Hello World!Using py_compilepy_compile is a module which behaves like the import statement. It generates two functions— one to generate the bytecode from the source file and another when the source file is invoked as a script.You can compile your Python script using this module:import py_compile py_compile.compile('first_script.py'  '__pycache__\\first_script.cpython-37.pyc' The py_compile generates a new subdirectory named "__pycache__" if it doesn’t already exist. Inside the subdirectory, a Compiled Python File (.pyc) version of the script file is created. When you open the .pyc file, you can see the output of your Python script.Running Python Scripts using an IDE or a Text EditorAn Integrated Development Environment (IDE) is an application that allows a developer to build software within an integrated environment in addition to the required tools.You can use the Python IDLE, a default IDE of the standard Python Distribution to write, debug, modify, and run your modules and scripts. You can use other IDEs like Spyder, PyCharm, Eclipse, and Jupyter Notebook which also allow you to run your scripts inside its environment.You can also use popular text editors like Sublime and Atom to run Python scripts.If you want to run a Python script from your IDE or text editor, you need to create a project first. Once it is created, add your .py file to it or you can just simply create one using the IDE. Finally, run it and you can see the output in your screen.Running Python Scripts from a File ManagerIf you want to run your Python script in a file manager, all you need to do is just double-click on the file icon. This option is mainly used in the production stage after you have released the source code.However, to achieve this, some conditions must be met:On Windows, to run your script by double-clicking on them, you need to save your script file with extension .py for python.exe and .pyw for pythonw.exe.If you are using the command-line for running your script, you might likely come  through a situation where you’ll see a flash of a black window on the screen. To avert this, include a statement at the tail of the script — input(‘Enter’). This will exit the program only when you hit the ENTER key. Note that the input() function will work only if your code is free of errors.On GNU/Linux and other Unix-like systems, your Python script must contain the hashbang line and execution permissions. Otherwise, the double-click trick won’t work in a file manager.Though it is easy to execute a script by just double-clicking on the file, it isn’t considered a feasible option because of the limitations and dependency factors it comes with, like the operating system, the file manager, execution permissions, and also the file associations.So it is suggested to use this option only after the code is debugged and ready to be in the production market.ConclusionWorking with scripts has its own advantages like they are easy to learn and use, faster edit and run, interactivity, functionality and so on. They are also used to automate complex tasks in a simplified manner.In this article, you have learned to run your Python scripts using:The terminal or the command-line of the operating system.The Python Interactive session.Your favorite IDE or text editor.The system file manager.Here, you have gathered the knowledge and skills of how to run your scripts using various techniques.You will feel more comfortable working with larger and more complex Python environments which in turn will enhance the development process and increase efficiency. You can learn more about such techniques as KnowledgeHut offers Python Certification Course.
Rated 4.5/5 based on 19 customer reviews
5881
How To Run Your Python Scripts

If you are planning to enter the world of Python p... Read More

How to Work With a PDF in Python

Whether it is an ebook, digitally signed agreements, password protected documents, or scanned documents such as passports, the most preferred file format is PDF or Portable Document Format. It was originally developed by Adobe and is a file format used to present and transfer documents easily and reliably. It uses the file extension .pdf. In fact, PDF being the most widely used digital media, is now considered as an open standard which is maintained by the International Standards Organization (ISO). Python has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. The popular Python libraries are well suited and integrated which allows to easily extract documents from a PDF, rotate pages if required, split pdf to make separate documents, or add watermarks in them. Now an important question rises, why do we need Python to process PDFs? Well, processing a PDF falls under the category of text analytics. There are several libraries and frameworks available which are designed in Python exclusively for text analytics. This makes it easier to play with a PDF in Python. You can also extract information from PDF and use into Natural Language Processing or any other Machine Learning models. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.History of  pyPDF, PyPDF2, pyPDF4The first PyPDF package was released in 2005 and the last official release in 2010. After a year or so, a  company named Phasit sponsored a branch of the PyPDF called PyPDF2 which was consistent with the original package and worked pretty well for several years.A series of packages were released later on with the name of PyPDF3 and later renamed as PyPDF4. The biggest difference between PyPDF and the other versions was that the later versions supported Python3. PyPDF2 has been discarded recently. But since PyPDF4 is not fully backward compatible with the PyPDf2, it is suggested to use PyPDF2. You can also use a substitute package - pdfrw. Pdfrw was created by Patrick Maupin and allows you to perform all functions which PyPDF2 is capable of except a few such as encryption, decryption, and types of decompression.Some common libraries in PythonLet us look into some of the libraries Python offers to handle PDFs:PdfMiner It is a tool used to extract information from PDF documents. PDFMiner allows the user to analyze text data and obtain the definite location of a text. It provides information such as fonts and lines. We can also use it as a PDF transformer and a PDF parser.PyPDF2PyPDF2 is purely a Python library which allows users to split, merge, crop, encrypt, and transform PDFs. You can also add customized data, view options, and passwords to the documents. Tabula-pyIt is a Python wrapper of tabula-java which can read tables from PDF files and convert into Pandas Dataframe or into CSV/TSV/JSON file formats.SlateIt is a Python package which facilitates the extraction of information and is dependent on the PdfMiner package.PDFQueryA light Python wrapper which uses minimum code to extract data from PDFs.xPDFIt is an open source viewer of PDF which also includes an extractor, converter and other utilities. Out of all the libraries mentioned above, PyPDF2 is the most used to perform operations like extraction, merging, splitting and so on.Installing PyPDF2If you're using Anaconda, you can install PyPDF2 using pip or conda. To install PyPDF2 using pip, run the following command in the command line:pip install PyPDF2The module is case-sensitive. So you need to make sure that proper syntax is followed. The installation is really quick since PyPDF2 is free of dependencies.Extracting Document Information from a PDF in PythonPyPDF2 can be used to extract metadata and all sorts of texts from PDF when you are performing operations on preexisting PDF files. The types of data you can extract are:AuthorCreatorProducerSubjectTitleNumber of PagesTo understand it better, let us use an existing PDF in your system or you can go to Leanpub and download a book sample.The code for extracting the document information from the PDF—# get_doc_info.py from PyPDF2 import PdfFileReader def getinfo(path):     with open(path, 'rb') as f:         PDF = PdfFileReader(f)         information = PDF.getDocumentInfo()         numberofpages = PDF.getNumPages()     print(information)     author = information.author     creator = information.creator     producer =information .producer     subject = information.subject     title = information.title if __name__ == '__main__':     path = 'reportlab-sample.pdf'     getinfo(path)The output of the program above will look like—Here, we have firstly imported PdfFileReader from the PyPDF2 package. The class PdfFileReader is used to interact with PDF files like reading and extracting information using accessor methods. Then, we have created our own function getinfo with a PDF file as an argument and then called the getdocumentinfo(). This returned an instance of DocumentInformation. And finally we got extract information like the author, creator, subject or title, etc.getNumPages() is used to count the number of pages in the document. PdfMiner can be used when you want to extract text from a PDF file. It is potent and particularly designed for extracting text from PDF.We have learned to extract information from PDF. Now let’s learn how to rotate a PDF. Rotating pages in PDFA lot of times we receive PDFs which contain pages in landscape orientation instead of portrait. You may also find certain documents to be upside down, which happens while scanning a document or mailing. However, we can rotate the pages clockwise or counterclockwise according to our choice using Python with PyPDF2.The code for rotating the article is as follows—# rotate_pages.py from PyPDF2 import PdfFileReader, PdfFileWriter def rotate(pdf_path):     pdf_write = PdfFileWriter()     pdf_read = PdfFileReader(path)     # Rotate page 90 degrees to the right     page1 = pdf_read.getPage(0).rotateClockwise(90)     pdf_write.addPage(page1)     # Rotate page 90 degrees to the left     page2 = pdf_read.getPage(1).rotateCounterClockwise(90)     pdf_write.addPage(page2)     # Add a page in normal orientation     pdf_write.addPage(pdf_read.getPage(2))     with open('rotate_pages.pdf', 'wb') as fh:         pdf_write.write(fh) if __name__ == '__main__':     path = 'mldocument.pdf'     rotate(path)The output of the code will be as follows—Here firstly we imported the PdfFileReader and the PdfFileWriter so that we can write out a new PDF file. Then we declared a function rotate with a path to the PDF that is to be modified. Within the function, we created a read object pdf_read and write object pdf_write.Then, we used the getPage() to grab the pages. Two pages page1 and page2 are taken and rotated to 90 degrees clockwise and 90 degrees counterclockwise respectively using rotateClockwise() and rotateCounterClockwise().We used addPage() function after each rotation method calls. This adds the rotated page to the write object. The last page we add is page3 without any rotation.Lastly, we have used write() with a file-like parameter to write out the new PDF. The final PDF contains three pages, the first two will be in the landscape mode and rotated in reversed direction and the third page will be in normal orientation.Now we will learn to merge different PDFs into one.Merging PDFsIn many cases, we need to merge two PDFs into a single one. For example, suppose you are working on a project report and you need to print it and bind it into a book. It contains a cover page followed by the project report. So you have two different PDFs and you want to merge them into one PDF. You can simply use Python to do so. Let us see how can we merge PDFs into one.The code for merging two PDF documents using PyPDF in mentioned below:# pdf_merging.py from PyPDF2 import PdfFileReader, PdfFileWriter def pdfmerger(paths, output):     pdfwrite = PdfFileWriter()     for path in paths:         pdfread = PdfFileReader(path)         for page in range(pdfread.getNumPages()):             # Add each page to the writer object             pdfwrite.addPage(pdfread.getPage(page))     # Write out the merged PDF     with open(output, 'wb') as out:         pdfwrite.write(out) if __name__ == '__main__':     paths = ['document-1.pdf', 'document-2.pdf']     pdfmerger(paths, output='merged.pdf')Here we have created a function pdfmerger() which takes a number of inputs and a single output. Then we created a PdfFileReader() object for each PDF path and looped over the pages, added each page to the write object. Finally, using the write() function the object’s contents are written to the disk.PyPDF2 makes the process of merging simpler by creating the PdfFileMerger class.Code for merging two documents using PyPDF2—# pdf_merger2.py import glob from PyPDF2 import PdfFileMerger def merger(output_path, input_paths):     pdfmerge = PdfFileMerger()     file_handles = []     for path in input_paths:         pdfmerge.append(path)     with open(output_path, 'wb') as fileobj:         pdfmerge.write(fileobj) if __name__ == '__main__':     paths = glob.glob('d-1.pdf')     paths.sort()     merger('d-2.pdf', paths)The PyPDF2 makes it simpler in the way that we don’t need to loop the pages of each document ourselves.  Here, we created the object pdfmerge and looped through the PDF paths. The PyPDF2 automatically appends the whole document. Finally, we write it out.Let’s perform the opposite of merging now!Splitting PDFsThe PyPDF2 package has the ability to split up a single PDF into multiple PDFs. It allows us to split pages into different PDFs. Suppose we have a set of scanned documents in a single PDF and we need to separate the pages into different PDFs as per requirement, we can simply use Python to select pages we want to split and get the work done.Code for splitting a single PDF into multiple PDFs—# pdf_splitter.py import os from PyPDF2 import PdfFileReader, PdfFileWriter def splitpdf(path):     fname = os.path.splitext(os.path.basename(path))[0]     pdf = PdfFileReader(path)     for page in range(pdf.getNumPages()):         pdfwrite = PdfFileWriter()         pdfwrite.addPage(pdf.getPage(page))         outputfilename = '{}_page_{}.pdf'.format(             fname, page+1)         with open(outputfilename, 'wb') as out:             pdfwrite.write(out)         print('Created: {}'.format(outputfilename)) if __name__ == '__main__':     path = 'document-1.pdf'     splitpdf(path)Here we have imported the PdfFileReader and PdfFileWriter from PyPDF2. Then we created a function called splitpdf() which accepts the path of PDF we want to split. The first line of the function takes the name of the input file. Then we open the PDF and create a read object. Using the read object’s getNumPages(), we loop over all the pages.In the next step, we created an instance of PdfFileWriter inside the for loop. Then, we created a PDF write instance and added each page to it for each of the pages in the PDF input. We also created a unique filename using the original filename + the word ‘page’ + the page number + 1.Once we are done with running the script, we will have each of the pages of the input PDF split into multiple PDFs. Now let us learn how to add a watermark to a PDF and keep it secured.Adding Overlays/WatermarksAn image or superimposed text on selected pages in a PDF document is referred to as a Watermark. The Watermark adds security features and protects our rational property like images and PDFs. Watermarks are also called overlays.The PyPDF2 allows us to watermark documents. We just need to have a PDF which will consist of our watermark text, image or signature.Code for adding a watermark in a PDF—# watermarker.py from PyPDF2 import PdfFileWriter, PdfFileReader def watermark(inputpdf, outputpdf, watermarkpdf):     watermark = PdfFileReader(watermarkpdf)     watermarkpage = watermark.getPage(0)     pdf = PdfFileReader(inputpdf)     pdfwrite = PdfFileWriter()     for page in range(pdf.getNumPages()):         pdfpage = pdf.getPage(page)         pdfpage.mergePage(watermarkpage)         pdfwrite.addPage(pdfpage)     with open(outputpdf, 'wb') as fh:         pdfwrite.write(fh) if __name__ == '__main__':     watermark(inputpdf='document-1.pdf',               outputpdf='watermarked_w9.pdf',               watermarkpdf='watermark.pdf')The output of the code will look like— There are three arguments of the function watermark(): inputpdf: The path of the PDF that is to be watermarked. outputpdf: The path where the watermarked PDF will be saved. watermarkpdf: The PDF which contains the watermark.Firstly, we extract the PDF page which contains the watermark image or text and then open that PDF page where we want to give the desired watermark.Using the inputpdf, we create a read object and using the pdfwrite, we create a write object to write out the watermarked PDF and then iterate over the pages.Next, we call the page object’s mergePage and apply the watermark and add that to the write object pdfwrite.When the loop terminates, the watermarked PDF is written out to the disk and it’s done!Encrypting a PDFIn the PDF world, the PyPDF2 package allows an owner password which gives the user the advantage to work as an administrator. The package also provides the user password which allows us to open the document upon entering the password.The PyPDF2 basically doesn’t permit any allowances on any PDF file yet it allows the user to set the owner password and user password.Code to add a password and add encryption to a PDF—# pdf_encrypt.py from PyPDF2 import PdfFileWriter, PdfFileReader def encryption(inputpdf, outputpdf, password):     pdfwrite = PdfFileWriter()     pdfread = PdfFileReader(inputpdf)     for page in range(pdfread.getNumPages()):         pdfwrite.addPage(pdfread.getPage(page))     pdfwrite.encrypt(user_pwd=password, owner_pwd=None,                       use_128bit=True)     with open(outputpdf, 'wb') as fh:         pdfwrite.write(fh) if __name__ == '__main__':     encryption(inputpdf='document-1.pdf',                   outputpdf='document-1-encrypted.pdf',                   password='twofish')We declare a  function named encryption() with three arguments—the input PDF path, the output PDF path and the password that we want to keep. Then we create one read object pdfread and one write object pdfwrite. Now we loop over all the pages and add them to the write object since we need to encrypt the entire document.Finally, we call the encrypt() function which accepts three parameters—the user password, the owner password and the whether or not to use 128-bit encryption. The PDF  will be encrypted to 40-bit encryption if the argument use128bit is set to false. Also if the owner password is set to none, then it will be set to user password automatically.Reading the Table data from PDFSuppose you want to work with the Table data in Pdf, you can use tabula-py to read tables in a PDF. To install tabula-py, run:pip install tabula-pyCode to extract simple Text from pdf using PyPDF2:import tabula # readinf the PDF file that contain Table Data # you can find the pdf file with complete code in below # read_pdf will save the pdf table into Pandas Dataframe df = tabula.read_pdf("document.pdf") # in order to print first 5 lines of Table df.head()If you PDF file contains Multiple Tabledf = tabula.read_pdf("document.pdf",multiple_tables=True)If you want to extract Information from the specific part of any specific page of PDFtabula.read_pdf("document.pdf", area=(126,149,212,462), pages=1)If you want the output into JSON Formattabula.read_pdf("offense.pdf", output_format="json")Exporting PDF into ExcelSuppose you want to export a PDF into Excel, you can do so by writing the following code and convert the PDF Data into Excel or CSV.tabula.convert_into("document.pdf", "document_testing.xlsx", output_format="xlsx")Let us sum up what we have learned in the article:Extraction of data from a PDFRotate pages in a PDFMerge PDFs into one PDFSplit a PDF into many PDFsAdd watermarks or overlays in a PDFAdd password or encryption to a PDFReading table from PDFExporting PDF into Excel or CSVAs you have seen, PyPDF2 is one of the most useful tools available in Python. The features of PyPDF2 makes life easier whether you are working on a large project or even when you quickly want to make some changes to your PDF documents. Learn more about such libraries and frameworks as KnowledgeHut offers Python Certification Course for Programmers, Developers, Jr./Sr Software Engineers/Developers and anybody who wants to learn Python.
Rated 4.5/5 based on 1 customer reviews
7578
How to Work With a PDF in Python

Whether it is an ebook, digitally signed agreement... Read More

ASP.NET VS PHP

ASP.NET and PHP are pretty popular languages in the programming world used by a huge number of developers and this makes it difficult for the new developers to choose either one of them. The comparison between these two has been in debate in recent times. Both of these languages are used in large web-based applications. Some successful companies like Google, Facebook, and Twitter, etc, also use these languages. In this article, we will understand the differences between PHP and ASP.Net also, will discuss which is better ASP.NET or PHP.Before we learn more about the differences between the two languages, we must first understand some basics of the two technologies:PHPPHP stands for Hypertext Preprocessor. It is an open-source programming language that is used for web development and can be embedded into HTML. The best part of PMP is that it’s free and possesses a  ton of frameworks which simplifies web development and also great for beginners since it allows simple and easy coding techniques. PHP is great for professionals as well because of its advanced features.Why use a PHP framework?A PHP framework provides a basic structure for streamlining the development of web apps. The applications and websites built using PHP frameworks will help the businesses to improve their performance needs.The best PHP frameworks available:LaravelCodeIgniterSymfonyZendPhalconCakePHPYiiFuelPHPPros and Cons of PHP frameworkPros:Rapid Development                                              Centralized DatabaseStronger TeamworkMakes your application more secure               Cons:Slower ExecutionPHP is unsecuredPoor error handling methodLimited Visibility and ControlDemand for PHP Developer:In today’s web development market, most of the websites are developed using PHP development tools which indicates a huge demand for PHP developers. If you are looking to make an entry to the IT world as a developer, then PHP programming will be an easy entry point.Taking up a PHP training from an authentic and reliable training provider will be a great platform to hone your skills.ASP.NETASP.NET is an open-source server-side web development tool developed by Microsoft for easy building of web applications and web pages. It can be written using any .Net supported language which makes it more popular among .NET developers. High speed and low cost are the main reasons to use it. Websites built ASP.NET is faster and more efficient than a website built with PHP.Pros and Cons of ASP.NET frameworkPros:Less coding timeWorld class toolboxConsistencyCustomizability and ExtensibilityCons:Limited Object-Relational (OR) supportBit expensiveSlower than Native CodeDemand for ASP.NET Developer:If you are a .NET developer, you will find yourself demanded by several asp.net development companies as your programming skills are extremely valuable in today’s market. There are many companies hunting for developers who can do programming with .NET. Therefore, it is advisable that you brush up your skills with ASP.NET Certification Training which will increase your value many times and have an edge over others. The ASP.NET Certification Training program will definitely make your future bright and offer you heaps of career opportunities. Whether you are a fresher or a working professional, you can take up the certification course.Comparison Between ASP.NET and PHPBoth ASP.NET and PHP frameworks are effective frameworks to work with, however, one may have few advantages over the other. Let’s dive deeper and compare these frameworks to understand which one is better than the other.1. Market Share:According to the report, BuiltWith data source PHP is the most used programming language which has 73% of market share, ASP.NET has 23% of market share. PHP also has a market share of 58% in top 100K websites and market share of PHP in 10K websites is 52%.Statistics for websites using Programming Language technologies:2. WebsitesHere are two lists to compare ASP.NET vs PHP websites:Websites built using PHPWebsites built using ASP.NETWikipediaFacebookYahooWordPress.comiStockPhotoMicrosoftDellGoDaddy3. Inbuilt featuresPHP has many unique in-built features that can help web developers. On the other hand, ASP.NET doesn’t have any such features.4. Speed and PerformanceWhen you compare PHP vs. ASP.NET for speed, PHP will be the winner. ASP.NET is a bit slow compared to PHP as it is built on the COM-based system whereas, PHP program runs on its own memory space.5. Community SupportCompared to ASP.NET, learning support is great in the PHP framework and has a large support community. It will be difficult for you to get hold of #C language of ASP.NET as it is difficult to understand.Key differences between ASP.NET vs PHPPHPASP.NETPHP was launched by Rasmus Lerdorf in the year 1995.ASP.NET was launched by Microsoft in the year 2002.PHP is a scripting languageASP.NET is a paid Microsoft provided web application framework.PHP suits for small sized organizationsASP.NET suits for a large and medium-sized organization.PHP has a decent market share in the  marketASP.NET has a higher market sharePHP works slow for desktop applicationsASP.NET is well equipped to assist and create desktop applications.PHP suits best for applications that contain a prime focus on UIASP.NET suits better for applications where the key concern is security.Easy to learnQuite challenging to learn.Coding using PHP is easy when compared to all other languagesCoding with ASP.NET is complicatedPHP execution is faster since it uses in-built memory spaceCoding with ASP.NET is complicatedPHP can run in Linux Operating System which is available for freeASP.NET requires a Windows platform which is not freeConclusionBoth PHP and ASP.NET come with their pros and cons. PHP is secure, fast, reliable, and inexpensive and ASP.NET is easier to use and maintain because of its class library system. Since both programming languages are similar and accomplish the same results so the company can make a choice based on the needs and requirements of the app they are about to develop.
Rated 4.5/5 based on 1 customer reviews
7681
ASP.NET VS PHP

ASP.NET and PHP are pretty popular languages in th... Read More

Android Development Principles | Guidelines

Getting started with Android development seems to be a big challenge. You’ve to have a deep understanding of android development principles and basic guidelines. Unfortunately, most of the developers don’t get the chance to learn the basics until it’s too late. If you’re just starting out, there are certain principles and guidelines that you’ve to stick to. What are these? Let’s dive in. Android development principles can be best explained by the SOLID acronym, as devised by Robert Martin. It refers to: Single Responsibility Open-Closed Principle Liskov Substitution Interface Segregation Dependency Inversion Robert Martin developed these five object-oriented design principles for the sole purpose of helping developers deliver maintainable and extensible systems. These principles not only help in writing a well-structured code but they also make sure that the code can easily be extended later. Take, for example, .NET. In the beginning, .NET was a mess and it were these principles that helped developers make a well formatted code for .NET applications and websites. So, let’s have a look at these principles individually. Single Responsibility Single Responsibility, as its name suggests, means that your classes should have only one responsibility. For example, if you have to perform calculations, mapping, and formatting, make 3 separate classes to handle each responsibly rather than making one class to handle all 3. But, what exactly do we mean by responsibility? A responsibility can be thought of as a “reason to change”. If you can think of more than one reason to change a class then you are probably breaking the Single Responsibility rule. The reason behind Single Responsibility is that it makes your class more complex and lead to code duplication issues which almost always ends up with non-updated codes. Open-Closed Principle           This means that your software entities like classes, functions, modules etc. should be open to extension but not modification. This is a very basic principle that should be followed no matter what platform you are coding for. This principle requires you to write a class or function or module in a way that it does need to be changed whenever your requirements change. For example, if you are making a class to calculate the area of a shape like a circle then you shouldn’t change the class every time a new shape comes as an input. Designing a base class and then extending it with the help of inheritance, or any other technique depending on the language you are using, is the way to go. This way, you won’t be making a change in the class for every new shape. Liskov Substitution Principle This is the type of a principle that is very tricky to explain even though almost all of us follow this principle unintentionally. Liskov Substitution Principle says that: “Objects should be replaceable with their subtype instances without having the need to alter the program.” This might be confusing but let me explain it with the help of an example. Let’s say you made a class that requires a List Object to perform a certain task. Now, what if we send an ArrayList Object to this class instead of a List Object? The class shouldn’t work right? No, the program will work just fine. The reason is the Liskov Substitution Principle. The ArrayList Object is a subtype of List class and, since, List class is an abstraction, the ArrayList Object will work without any problem. In short, you can replace the objects of List with any of its subtype without causing the program to break. So, if you require T and S in a subtype of T, then objects of T should be replaced by the objects of S without changing the properties of the program, especially the correctness property. Interface Segregation Principle This is a pretty straight forward principle. The Interface Segregation Principle states that there should be a lot of Client-specific User Interfaces rather than a general purpose User Interface. Let’s say a user has to enter his email address, fill a survey, and enter his favorite food. In this case, it would be better to make 3 separate User Interface pages instead of a single general purpose page that asks all of these questions. A general purpose User Interface will make your code complex because of the multiple callbacks as well as make the user confused. Remember, always give users small achievable goals rather than a big time-consuming task. Dependency Inversion Principle The Dependency Inversion Principle covers two things: High-level modules should be independent of the low-level modules. Both should depend on abstractions. Details should depend on Abstractions instead of Abstractions depending on the Details. You will be increasing the de-coupling by making your High-level modules independent of the low-level modules. Being a developer, you should know that requirements for an application changes a lot. And, changes are risky. So, making your modules independent will make them reusable as well as significantly reduce the risks involved in changing the implementations. In short, you will reduce the risk of changes by depending on the abstractions, for instance, interface rather than implementations. A few great tips for android developer that helps who wants to become one, you will undoubtedly have noticed that there are literally hundreds of thousands of apps on the Google Play store.
Rated 4.0/5 based on 20 customer reviews
Android Development Principles | Guidelines

Getting started with Android development seems to ... Read More

About SELENIUM WEBDRIVER-3

Why WebDriver-3? In present market we have a tool called IDE. It is a tool which works only on Mozilla firefox. It has many drawbacks and doesn’t generate proper reports. IDE is not a preferred tool because sometimes when the scripts run, it finds the locator and sometimes it doesn’t find it and so the script fails. So, to overcome all these challenges we have a tool called WebDriver. WebDriver works on Multiple browsers like Firefox, IE, Chrome, Safari, Opera etc. It supports multiple languages like Java, C#, Ruby, Python and JavaScript. With WebDriver, we can make dynamic scripts like scripts on Webpages which changes everyday and we can make our scripts run on multiple browsers. Downloading WebDriver-3 Jars and configuring in eclipse To start with WebDriver, the first thing we need to do is to download WebDriver Jars and configure it in Eclipse IDE. We need to download Jar files of WebDriver from the official website of Selenium, which is Selenuimhq.org where we need to go to download section and under the download section, we will see the latest version which is 3.4.0. Click on the link and file will be downloaded in the downloads folder. Also, click on Change log at Selenium website to describe what has changed in the new version. The last 2.x version was 2.53 which is a stable build, meaning it has no major issues. After 2.53, the next build which came up was 3.XX. The changes made in 3.0 is that, minimum java version requires 1.8, where we need to make sure that jdk8 is installed. Installing Java To install java, Go to Google and type download Jdk. Go to first link as shown below Depending upon the system, we need to install either Windows x32 or Windows x64 bit and accept the License Agreement. For downloading Java, we need to click on Download link, where Zip file will be downloaded on our PC. Go to downloaded folder and extract it and double click on exe. Java will be installed in the system and now we need to configure the class path. If we are using v2.53.0 version, then we will be needing Firefox 40 or below. In case, we are using 3.0 then we need to go for latest version of Firefox. Configuring Jar Files in Eclipse Step1: Open Eclipse and make a New Project (ex:Sample) Step2: Right click on the project and select the option Properties as shown below Step3: Go to Java Build Path > Libraries Tab and click on Add External Jars as shown below: Now, go to the location where we saved our Jar Files and add all the jar files. It will appear like below image after adding all Jar files: Once we are done with configuring Jar files in eclipse, we can see a folder called Referenced Libraries under Project folder where we have all our Jar Files. Architecture of selenium webdriver 3 First we need to know a few concepts if we want to understand the working of Selenium WebDriver Architecture. Language Binding or Selenium Client Library Selenium API Remote WebDriver Json Wired Protocol WebDrivers Language Binding or Selenium Client Library : It is nothing but a collection of Jar files, where the language in which we write our selenium framework is present. It means that the language which we used to write the script may be Java, C#, Ruby, Python, perl. Selenium API : API Stands for Application Programming Interface. API is a particular set of rules and specifications that software programs can follow to communicate with each other. API serves as an interface between software program and facilitate their interaction. API is a software to software interaction which means API works between software to software. With the help of APIs, applications talk to each other without any user knowledge. Remote WebDriver : It is an implementation class of the webdriver interface that a test script developer can use to execute their test script through webdriver server on remote machine. JSON:  It stands for JavaScript Object Notation. It is a lightweight data interchange format which helps us to interchange the data. JSON is used to transfer data between client and server on the web. A simple JSON file has .json extension. JSON wired protocol send data in JSON format. Thereafter, the server will firstly parse the data and execute it and after that, response given by the server will be sent back to the client in JSON format. Webdriver: Webdriver is a tool for automating web applications and verifying that     they work as expected.  Architecture: Tester writes script in any language like java, C# etc and selenium client library will communicate with selenium API. Selenium API will send the command taken from language level binding to browser driver with the help of JSON wired protocol and sends the request to Browser Driver, it may be Firefox driver, IE driver, Chrome driver. When we run all the commands we write in the script, it is converted with JSON wired protocol over HTTP and it will convert into URL and the request made by the HTTP and send to the browser. These browser drivers will use the HTTP server for getting HTTP request and these HTTP Server filter out all commands which we need to execute. Drivers for Firefox, IE, Chrome FirefoxDriver: It is an inbuilt class in Selenium. Lets create a New Class in Eclipse Class Name: Drivers_Selenium Inside main function, create an object of FirefoxDriver class like shown below: FirefoxDriver is an inbuilt class inside Referenced Libraries jars, where we can move mouse over the error and import FirefoxDriver class. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.gecko.driver system property” like shown in below image. import org.openqa.selenium.firefox.FirefoxDriver; public class Drivers_Selenium { public static void main(String[] args) { // TODO Auto-generated method stub new FirefoxDriver(); } } Steps to Download Geckodriver exe file: Step1: Go to seleniumhq.org Step2: Click on Download Tab Step3: Click on Mozilla Geckodriver 0.16.1 version in browsers session Step4: Click on geckodriver-v0.17.0-win64.zip or geckodriver-v0.17.0-win32.zip based upon our operating system. Open the downloaded folder and extract it, after extracting we will get geckodriver.exe file like shown below image. Now there is no need to double click on the geckodriver file, all we need is to set the System property known as “webdriver.gecko.driver” and we have to give the path of  geckodriver exe file. As java is a case sensitive language make sure that “webdriver.gecko.driver” is in lower case. Run the program where we can see a blank firefox getting opened. Now we need to give object reference as “FirefoxDriver fd” so that we can use the features of FirefoxDriver object. Inside the documentation of selenium, we find WebDriver interface. Inside this interface, we see a function get( ) which loads the new webpage in the current browser window. It open up a new URL.We can use the function “fd.get ” and get the URL. Run the above program where we can see the browser launching and it opens yahoo.com ChromeDriver : As the name suggests, it is for chrome. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.chrome.driver system property”.Now we also need to download an exe file.Go to download session in seleniumhq.org and scroll down where we find Google Chrome Driver link and click on it and it will redirect to the next page. From here we can download based upon our operating system as shown in below image.Whatever be the operating system, whether it is 32xbit or 64xbit, we can download the same file. Open the downloaded folder and extract it, after extracting we will get “chromedriver” file like shown in the below image. Now, there is no need to double click on chromedriver file, all we need to do is set the System property known as “webdriver.chrome.driver” and we have to give the path of chromedriver.exe file. As java is a case sensitive language, make sure that “webdriver.chrome.driver” is in lower case. Run the above program where we can see a blank chrome browser will launch. Now we need to give object reference as “ChromeDriver ” so that we can use the features of ChromeDriver object. Inside the documentation of selenium, we find WebDriver interface.Inside this interface we see a function get( ) which loads the new webpage in the current browser window. It opens up a new URL.We can use the function “cd.get ” and get the URL. Run the above program where we can see the Chrome browser launching and open it on yahoo.com IEDriver: As the name suggests it is for InternetExplorer. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.ie.driver system property”.Now we also need to download an exe file.Go to download session in seleniumhq.org and scroll down where we find IE Driver link and click on it and it will reside to next page from here we can download based upon our operating system, as shown in the below image.What ever be the operating system, whether it is 32xbit or 64xbit, we can download the same file. Open the downloaded folder and extract it, after extracting we will get “ieDriver” file. Now no need of double click on iedriver file, all we need is to set the System property known as “webdriver.ie.driver” and we have to give the path of  ie exe file. As java is a case sensitive language, make sure that “webdriver.ie.driver” is in lowercase. Now we need to give object reference as “IEDriver ” so that we can use the features of IEDriver object.Inside the documentation of selenium, we find WebDriver interface. Inside this interface, we see a function get( ) which loads the new webpage in the current browser window. It opens up a new URL.We can use the function “ie.get ” and get the URL. Run the above program where we can see the IE browser launching and open it in yahoo.com Launching Browsers Dynamically Let us Create New Class “Launch Browsers” Let us build a Testcase. Now our requirement is that we have a parameter called browser. If we write parameter as “Mozilla”, then the script we write in testcase should run in Mozilla browser. If we write a parameter as “Chrome”, it should run on chrome. Later on, we read this parameter from an excel file (xls) or any other file like property file, xml file. If our browser is Mozilla then below script should execute on Mozilla. If the browser is Mozilla then we want to launch the FirefoxDriver and take this FirefoxDriver to launch our desired website. In the above example, we keep on repeating the line to launch any website like fd.get(“http://gmail.com”). Later on if we want to click on a link in firefox then we need to write the code for each browsers separately. To overcome this, we use a concept called Interface reference points towards the class implementing the interface. WebDriver   d = new FirefoxDriver( ); WebDriver   d1= new ChromeDriver( ); Webdriver is an Interface and FirefoxDriver, ChromeDriver, IEDriver are implementing that Interface. Here d1 is  pointing towards chrome, so we can make d1 point towards FirefoxDriver as well , because the reference of the webdriver interface can point towards any implementing class or we can make d1 point towards InternetExplorerDriver as well, like shown below. d1 = new FirefoxDriver( ); d1 = new InternetExplorerDriver( ); So in the beginning of the program we can write WebDriver driver  =  null; Here Null means that driver is not pointing anywhere. So based on our choice, we can make driver point towards FirefoxDriver (or) ChromeDriver (or) InternetExplorerDriver as shown in the below program. Initially driver is “null”. Based on the options, driver will be initialized and we can give the command driver.get(“http://gmail.com”); All the driver classes will have same set of functions coming from WebDriver Interface, so “get” function will be there in all the driver classes. From the above program, we can launch the browser dynamically and we have to write a single script after launching the browser to interact with the browser. We do not need to write browser specific script. The reasons are : firstly all the drivers have same set of functions and secondly, the driver can point towards any of the driver classes like ChromeDriver, IEDriver. Selenium RC and WebDriver What is Firefox profile Let’s take an example where we have a PC at our place, being shared by two people. If the first person opens the PC and goes to Firefox, then firefox should get opened and it should have 1st person set of settings, his own set of bookmarks, his own set of plugins installed inside it. If a 2nd person opens PC and goes to Firefox, he should have his own set of settings, his own set of bookmarks, his own set of plugins. This concept is know as Firefox Profile. Importance of Firefox profile First where selenium launches Firefox, we write- driver  = new FirefoxDriver( ); Selenium will create its own profile and launch the browser. That profile is created programmatically by selenium and it gets destroyed when the script stops. Creating Profile manually on our PC Go to Firefox browser -> Go to File option and select Exit to close the firefox from the Exit menu, we should not close the firefox from the (x) option on the top as shown in below image. Go to windows session and type “firefox.exe -p profilemanager”. Copy the command and it will open the window as shown below. Click on Create Profile and give name as Selenium_Mod11. Click on the Finish button and click Start Firefox button where a new firefox browser will open up and this firefox browser will be like freshly installed firefox on our machine. It will have no bookmarks, no plugins, no history etc. Go to Firefox browser and bookmark the page as shown below Create a New Class -> Selenium_Profiles Run the program where it will open Firefox browser with Google Bookmark as shown in below image Close and Quit Difference driver.quit( ): It will end selenium session and close all browsers in the current session. Suppose we go to a website and click on a link, that link will open in new pop-up and if we fire driver.quit( ), then selenium will close all the browsers and end the selenuim session. driver.close( ): It won’t end selenuim session, but it closes the browser on which current focus is there. Importing Webdriver documentation in eclipse. Go to seleniumhq.org –> Go to download session and click Javadoc link and copy the URL. “http://seleniumhq.github.io/selenium/docs/api/java/index.html” and go to the project -> Right click and go to Properties->Go to Java Build Path and expand the link and click on the Javadoc location: (None). Click on Edit button and paste the URL and remove index.html and click on validate button and click OK button. Webdriver Desired Capabilities Class It helps us understand Selenium, and under which circumstances the script should run. Below example shows how we should run script on Mozilla with Javascript enabled on a windows platform.  
Rated 4.0/5 based on 20 customer reviews
About SELENIUM WEBDRIVER-3

Why WebDriver-3? In present market we have a to... Read More

Selenium And Its Salient Features

Selenium is an open source web app automation tool by ThoughtWorks (IT outsourcing company). It has 4 components. Out of which one component i.e. Selenium Remote Control was developed by a Jason Huggins team in 2004. It was primarily developed in DHTML/Javascript when they were working on time and expense application written in Python programming language. Later, Simon Stewart has developed a new component that is called as Webdriver in 2006 to overcome the disadvantages faced in Selenium Remote Control. And post that Selenium Remote Control and Webdriver were merged together and developed Selenium Webdriver. They have named this tool as Selenium based on a chemical element in Chemistry which is used to destroy Mercury chemical element.  Mercury tool which is now called as UFT was the most popular automation tool. Selenium has 4 different components that is: Selenium IDE (Integrated Development Environment) Selenium 1.0 (Also called as Remote Control) Selenium 2.0/3.0 (Also called as Webdriver) Selenium GRID Latest version of selenium is 3.4.0 which can be downloaded from http://www.seleniumhq.org/download/ Selenium supports multiple operating systems, multiple browsers and multiple languages. It gives you flexibility to choose the language in which you have expertise. Following is the list: Multiple Programming Languages : Java, Python, PHP, Ruby, Perl, JavaScript Multiple Operating Systems : Android, iOS, Windows, Linux, Mac, Solaris Multiple Browsers : Chrome, Internet Explorer, Edge, Opera, Safari etc Selenium Tool is known for its performance and execution speed. Let’s discuss about the different components of Selenium: 1)Selenium IDE : It is a record and play tool and a plugin of firefox. This plugin is used to create prototypes of tests. Following are the features of this tool: Easy to install Test Scripts are created by just click on record button Can record, edit and debug scripts Simplest way to learn Selenium syntax. Test Scripts can be imported in multiple languages like Python with Remote Control, Java with Webdriver, Java with Remote Control, Ruby with Remote Control etc This plugin can be downloaded from following URL: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/ 2)Selenium 1.0 (Remote Control) : This was the first component developed in Selenium Suite. Selenium became famous because of this component. It works in following manner: 1) Using this component, we write test scripts which interacts with Selenium Remote Control Server. 2) Server interprets the code and converts it into javascript and further injected into the browsers. 3) Javascript gets executed at the browser and response is sent back to the server which forwards it to the user. There were many drawbacks in this tool that is: Confusing commands. Remote Control Server acts as a mediator which makes its execution slower. Use of Javascript Selenium Webdriver (2.0/3.0) : After Selenium RC, Webdriver has come which make its architecture more simpler. Now, there is no server. Test Scripts interact directly with the browser. The execution is much faster compared to Selenium Remote Control. Selenium Commands were segregated in different classes which become easy for end user to remember and to implement. Last year, Selenium has launched a new version that is 3.0 which is much lighter than Selenium 2.0. There are not much change done which impact the end user but multiple were done at the backend. Syntax for loading Firefox has been changed to following: System.setProperty(“webdriver.gecko.driver”,”path to gecko driver”); Webdriver driver = new FirefoxDriver; Selenium Gecko Driver can be downloaded from following URL, the latest version for Gecko Driver is 0.16.1: https://github.com/mozilla/geckodriver/releases Selenium GRID : It is used for parallel testing. This component enable us to execute automation framework on different machines placed at different location. However, all the machines should be connected on a Local Area Network. It can be used with Selenium Remote Control as well as with Selenium Webdriver. Please note that Selenium Remote Control classes have been deprecated now. So, we couldn’t use this component with Selenium Remote Control.
Rated 4.0/5 based on 1 customer reviews
Selenium And Its Salient Features

Selenium is an open source web app automation tool... Read More