Search

How to Install Python on Windows

Python was created by Guido van Rossum and first released in 1991. It is an open source, interpreted programming language, and is simple to use, readable and extensible. You can split your program into modules that can be reused in other Python programs. Python supports several paradigms, such as object-oriented, functional, and procedural with the support of a large comprehensible library.  Python releases for Windows  The latest version of Python is 3.9.0, released on Oct. 5, 2020. It contains many new features and optimization. This is the first version of Python to default to the 64-bit installer on Windows. The installer now also actively disallows installation on Windows 7. Python 3.9 is incompatible with this unsupported version of Windows. If you go to the page:  https://www.python.org/downloads/windows/, you will find all releases of Python.   The installation process is the same, regardless of the version. The Python installers for the Windows platform usually include the entire standard library and often also include many additional components. For Unix-like operating systems Python is normally provided as a collection of packages, so it may be necessary to use the packaging tools provided with the operating system to obtain some or all of the optional components. There are a number of different installers available for Windows, each with certain advantages and disadvantages. Web-based installers: This installer is very small in size. It could be installed in a local computer with web-access. The installer will download necessary files and install them.  Executable installer: Best option for installing Python locally. The installer contains all important components and installs them itself. This is the best option for development of any kind of Python projects.   The embeddable package: is a minimal package of Python suitable for embedding into a larger application. The embeddable package uses Python as runtime and allows to distribute applications on Windows. Installing Python 3.9.0 for Windows Step 1: Open your browser window and navigate : https://www.python.org/downloads/ Step 2: Click on button download python 3.9.0 to download Python Executable Installer for Windows. It will start downloading the installer, which is around 27 MB in size.( If you have a lower version of Windows, please click Downloads menu button and select a specific version from the downloads page.) Step 3:  Once finished with the download, double click on the downloaded file to run the installer.  Step 4: The setup windows shows following dialog window with two installation options:  A. Install Now B. Customize Installation A. If you select “Install Now”: You will not need to be an administrator. This option installs Python in your user directory shown in the dialog box under Install Now option.  The standard library, test suite, launcher and pip will be installed. If selected, the install directory will be added to your PATH. If you check the install launcher for all users, it will install python launcher for all the users of your system. Step 5: The setup process will be shown in a Python 3.9.0(64-bit) Setup pop-up window.  Step 6: After successful completion of installation the following popup window will be shown with Setup was successful message. This dialog box also gives you the option to Disable path length limit. Clicking the “Disable path length limit” option removes the limitation on the MAX_PATH variable. This change won't break anything, but will allow Python to use long path names. Removing the MAX_PATH Limitation Windows historically has limited path lengths to 260 characters. This meant that paths longer than this would not resolve, and errors would result; this is termed as MAX_PATH Limitation. In the latest versions of Windows, this limitation can be expanded to approximately 32,000 characters.  You must save the file python-3.9 exe somewhere on your computer in case you need to reinstall, modify the settings or uninstall python.  B. Customize installation: This option allows you to select the features to install, the installation location and other options or post-install actions. If you select customized installation: You may be required to provide administrative credentials or approval Python will be installed into the Program Files directory The Python Launcher for Windows will be installed into the Windows directory The standard library can be pre-compiled to bytecode If selected, the install directory will be added to the system PATH Step 5: Step 1 to 4 would be same, but as you select the customized installation, the following dialog box will appear. This window provides a list with several optional features. You may select the respective checkboxes as per the requirement. The meaning of each option is as follows: Documentation: Will download the documentation, which can help you for future references.  Pip: Pip is a tool which helps in installation of additional libraries and dependencies which are not available in the standard library. It is also called the package manager of Python. Starting from the Python 3.4 version, the pip comes bundled with the installation files, but before that we used to install this package manager separately.  IDLE: stands for Integrated Development Learning Environment. This is CLI bundled with python. Along with IDLE editor, enabling this option will also install the “tkinter” toolkit, which is a python default GUI package.  Python test suite: This feature will install all the standard libraries for python application testing.  Py launcher: enables you to launch python CLI through programs like command prompt or windows shell.  For all users: it will install python launcher for all the users of your system Step 6: After clicking on Next button one more dialog window appears on the screen which helps developers to check or uncheck additional features.  This page provides 7 advance options: Install for all users: It will install python launcher for all the users of your system Associate files with Python (requires the py launcer): this option will link all the files Python extensions like .py, .pyd, .pyc or .pyo with the python launcher or editor. Create shortcut for installed applications: It will create a shortcut of application for you Add python to environment variables: PATH is an environment variable in Windows. It basically tells the command-line argument, what folders to look in when attempting to find a file The last three options tell the python installation wizard to install all the debug symbols and binaries along with the bytecode of all the standard libraries, which we will be using with our programs.  Customize installation location: We can change the location of python installation directory. The location you will specify will be added in the environmental variable of our windows 10 operating system.  After selecting the required advance options, click on Install button. After successful completion of installation, the following popup window will be shown with Setup was successful message. Modifying an install Once Python has been installed, you can add or remove features through the Programs and Features tool that is part of Windows. Select the Python entry and choose “Uninstall/Change” to open the installer in maintenance mode. “Modify” allows you to add or remove features by modifying the checkboxes - unchanged checkboxes will not install or remove anything. Some options cannot be changed in this mode, such as the install directory; to modify these, you will need to remove and then reinstall Python completely. “Repair” will verify all the files that should be installed using the current settings and replace any that have been removed or modified. “Uninstall” will remove Python entirely, with the exception of the Python Launcher for Windows, which has its own entry in Programs and Features. Setting Path The definition of “PATH” by Wikipedia is “PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting” In Windows %PATH%  is a variable which provides a list of directory names separated by semicolon(;). When we enter a command on command shell , the system will search in current windows directory and after that it searches in path. It actually looks for a filename in the path which matches with the command you have given. Once it finds the matching executable file it loads the new process and gets executed.  Python Launcher for Windows The Python launcher for Windows is a utility which helps in locating and executing different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version. Unlike the PATH variable, the launcher will correctly select the most appropriate version of Python. It will prefer per-user installations over system-wide ones, and orders by language version rather than using the most recently installed version. The launcher was originally specified in PEP 397. A Python launcher is a single executable which uses a number of heuristics to locate a Python executable and launches it with a specified command line. The launcher allows Python scripts or the Windows command-line to specify a particular Python version and will locate and launch that version.Documentation links for python https://www.python.org/doc/:  Browse the docs online or download a copy of your own https://www.python.org/doc/versions/:  document release by version Conclusion We hope this blog “How to install Python on Windows” would help you during the Python installation process. For additional information you can visit the official site of Python: https://www.python.org 

How to Install Python on Windows

8K
How to Install Python on Windows

Python was created by Guido van Rossum and first released in 1991. 

It is an open source, interpreted programming language, and is simple to use, readable and extensible. You can split your program into modules that can be reused in other Python programs. 

Python supports several paradigms, such as object-oriented, functional, and procedural with the support of large comprehensible library.  

Python releases for Windows  

The latest version of Python is 3.9.0, released on Oct. 5, 2020. It contains many new features and optimization. This is the first version of Python to default to the 64-bit installer on Windows. The installer now also actively disallows installation on Windows 7. Python 3.9 is incompatible with this unsupported version of Windows. 

If you go to the page:  https://www.python.org/downloads/windows/, you will find all releases of Python.   The installation process is the same, regardless of the version. 

The Python installers for the Windows platform usually include the entire standard library and often also include many additional components. For Unix-like operating systems Python is normally provided as a collection of packages, so it may be necessary to use the packaging tools provided with the operating system to obtain some or all of the optional components. 

There are a number of different installers available for Windows, each with certain advantages and disadvantages. 

  • Web-based installers:This installer is very small in size. It could be installed in a local computer with web-access. The installer will download necessary files and install them.  
  • Executable installer:Best option for installing Python locally. The installer contains all important components and installs them itself. This is the best option for development of any kind of Python projects.   
  • The embeddable package: is a minimal package of Python suitable for embedding into a larger application. The embeddable package uses Python as runtime and allows to distribute applications on Windows. 

Installing Python 3.9.0 for Windows 

Step 1: Open your browser window and navigate : https://www.python.org/downloads/ 

How to Install Python on Windows

Step 2: Click on button download python 3.9.0 to download Python Executable Installer for Windows. It will start downloading the installer, which is around 27 MB in size.( If you have a lower version of Windows, please click Downloads menu button and select a specific version from the downloads page.) 

Step 3:  Once finished with the download, double click on the downloaded file to run the installer 

Step 4: The setup windows shows following dialog window with two installation options 

A. Install Now 

B. Customize Installation 

How to Install Python on Windows

A. If you select “Install Now”: 

  • You will not need to be an administrator. 
  • This option installs Python in your user directory shown in the dialog box under Install Now option.  
  • The standard library, test suite, launcher and pip will be installed. 
  • If selected, the install directory will be added to your PATH. 
  • If you check the install launcher for all users, it will install python launcher for all the users of your system. 

Step 5: The setup process will be shown in a Python 3.9.0(64-bit) Setup pop-up window.  How to Install Python on Windows

Step 6: After successful completion of installation the following popup window will be shown with Setup was successful message. 

This dialog box also gives you the option to Disable path length limit. 

Clicking the “Disable path length limit” option removes the limitation on the MAX_PATH variable. This change won't break anything, but will allow Python to use long path names. 

Removing the MAX_PATH Limitation 

Windows historically has limited path lengths to 260 characters. This meant that paths longer than this would not resolve, and errors would result; this is termed as MAX_PATH LimitationIn the latest versions of Windows, this limitation can be expanded to approximately 32,000 characters.  

How to Install Python on WindowsYou must save the file python-3.9 exe somewhere on your computer in case you need to reinstall, modify the settings or uninstall python.  

B. Customize installationThis option allows you to select the features to install, the installation location and other options or post-install actions. If you select customized installation: 

  • You may be required to provide administrative credentials or approval 
  • Python will be installed into the Program Files directory 
  • The Python Launcher for Windows will be installed into the Windows directory 
  • The standard library can be pre-compiled to bytecode 
  • If selected, the install directory will be added to the system PATH 

Step 5: Step 1 to 4 would be same, but as you select the customized installation, the following dialog box will appear. 

How to Install Python on Windows

This window provides a list with several optional featuresYou may select the respective checkboxes as per the requirement. The meaning of each option is as follows: 

  • Documentation: Will download the documentation, which can help you for future references.  
  • Pip: Pip is a tool which helps in installation of additional libraries and dependencies which are not available in the standard library. It is also called the package manager of Python. Starting from the Python 3.4 version, the pip comes bundled with the installation files, but before that we used to install this package manager separately.  
  • IDLE: stands for Integrated Development Learning Environment. This is CLI bundled with python. Along with IDLE editor, enabling this option will also install the “tkinter” toolkit, which is a python default GUI package.  
  • Python test suite: This feature will install all the standard libraries for python application testing.  
  • Py launcher: enables you to launch python CLI through programs like command prompt or windows shell.  
  • For all users: it will install python launcher for all the users of your system 

Step 6: After clicking on Next button one more dialog window appears on the screen which helps developers to check or uncheck additional features.  How to Install Python on WindowsThis page provides 7 advance options: 

  • Install for all users: It will install python launcher for all the users of your system 
  • Associate files with Python (requires the py launcer): this option will link all the files Python extensions like .py, .pyd, .pyc or .pyo with the python launcher or editor. 
  • Create shortcut for installed applications: It will create a shortcut of application for you 
  • Add python to environment variables: PATH is an environment variable in Windows. It basically tells the command-line argument, what folders to look in when attempting to find a file 
  • The last three options tell the python installation wizard to install all the debug symbols and binaries along with the bytecode of all the standard libraries, which we will be using with our programs.  
  • Customize installation location: We can change the location of python installation directory. The location you will specify will be added in the environmental variable of our windows 10 operating system.  

After selecting the required advance options, click on Install button. 

After successful completion of installation, the following popup window will be shown with Setup was successful message. 

How to Install Python on Windows

Modifying an install 

Once Python has been installed, you can add or remove features through the Programs and Features tool that is part of Windows. Select the Python entry and choose “Uninstall/Change” to open the installer in maintenance mode. 

“Modify” allows you to add or remove features by modifying the checkboxes - unchanged checkboxes will not install or remove anything. Some options cannot be changed in this mode, such as the install directory; to modify these, you will need to remove and then reinstall Python completely. 

“Repair” will verify all the files that should be installed using the current settings and replace any that have been removed or modified. 

“Uninstall” will remove Python entirely, with the exception of the Python Launcher for Windows, which has its own entry in Programs and Features. 

Setting Path 

The definition of “PATH” by Wikipedia is “PATH is an environment variable on Unix-likeoperating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting 

In Windows %PATH%  is a variable which provides a list of directory names separated by semicolon(;). When we enter a command on command shell , the system will search in current windows directory and after that it searches in path. It actually looks for a filename in the path which matches with the command you have given. Once it finds the matching executable file it loads the new process and getexecuted.  

Python Launcher for Windows 

The Python launcher for Windows is a utility which helps in locating and executing different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version. 

Unlike the PATH variable, the launcher will correctly select the most appropriate version of Python. It will prefer per-user installations over system-wide ones, and orders by language version rather than using the most recently installed version. 

The launcher was originally specified in PEP 397. 

A Python launcher is a single executable which uses a number of heuristics to locate a Python executable and launches it with a specified command line. 

The launcher allows Python scripts or the Windows command-line to specify a particular Python version and will locate and launch that version.

Documentation links for python 

https://www.python.org/doc/:  Browse the docs online or download a copy of your own 

https://www.python.org/doc/versions/:  document release by version 

Conclusion 

We hope this blog “How to install Python on Windows” would help you during the Python installation process. For additional information you can visit the official site of Python: https://www.python.org 

KnowledgeHut

KnowledgeHut

Author

KnowledgeHut is an outcome-focused global ed-tech company. We help organizations and professionals unlock excellence through skills development. We offer training solutions under the people and process, data science, full-stack development, cybersecurity, future technologies and digital transformation verticals.
Website : https://www.knowledgehut.com

Join the Discussion

Your email address will not be published. Required fields are marked *

Suggested Blogs

What is Context in React? How to use Context in React?

What the hack is Context?Have you ever wondered about passing data or using states in between different components without using Props? Or passing a state from Parent to Child component without manually passing a prop at every level?  Let’s understand with an example below:Here we have a parent component app.js where we have defined our states. We want to access the data of state in the last child which is “Child 1.2” in the below chart.app.js Parent ComponentThe ideal or older approach in React is to pass the data from the root component to the last component is via Props. We have to pass props in each intermediary level so as to send in the last level. While this approach also works, the real problems begin if data is needed on a different branch i.e Child 2.1 or Child 2.2 in above chart…In order to solve this problem, we need to pass data from the root/top level of the application through all the intermediate components to the one where we want to pass the data, even though some intermediate components don't even need it.  This mind-numbing process is known as prop drilling,  Prop Drillingwhere you’re passing the state from your root component to the bottom and you end up passing the data via props through components that do not even necessarily need themOne really good solution to solve the above problem is using Context According to the React documentation:  “Context provides a way to pass data through the component tree without having to pass props down manually at every level”Ordinarily, we’d have used any state management library like Redux or have used HOC’s to pass the data in a tedious manner. But what if we don’t want to use it all? Here comes the role of new Context API!In layman words, it gives an approach to make specific data available to all components throughout the React component tree regardless of how deeply nested those components are.Context is just like a global object to the subtree of the React component.When to use the Context APIThe Context API is convenient for sharing data that is either global, such as setting the header and footer theme of a website or logic of user authentication and many more. In cases like these, we can use the Context API without using any extra library or external modules. It can also be used in a multilingual application where we want to implement multiple languages that can be translated into the required text with the help of ContextAPI. It will save prop-drilling   In fact, in any situation where we have to pass a prop through a component so it reaches another component, inside down the tree is where we can use the Context API.Introducing The Context APIThe context API is a way to pass data from top component to bottom ones, without manually passing it to via props. Context is fundamentally utilized when some data needs to be accessible by numerous components at different nesting levels. To create a new Context, we can use the React createContext function like below: const MyContext = React.createContext(defaultValue);In React, data is often passed from a parent to its child component as a property. Here, we can also omit the default value which we have passed to the context, if needed.React data passing from parent to its child Let’s Get Started With ContextThree things are needed to tap into the power of context: 1. The context itselfTo create a context we can use React.createContext method which creates a context object. This is used to ensure that the components at different level can use the same context to fetch the data.   In React.createContext, we can pass an input parameter as an argument which could be anything or it can be null as well.import React from `react';  const ThemeContext = React.createContext('dark');  // Create our context        export default ThemeContext;In this example, a string is passed for the current Context which is “dark”. So we can say, the current theme required for a specific component is Dark.   Also, we have exported the object so that we can use it in other places. In one app, React also allows you to create multiple contexts. We should always try to separate context for different purposes, so as to maintain the code structure and better readability. We will see that later in our reading.   What next?? Now, to utilize the power of Context in our example, we want to provide this type of theme to all the components.  Context exposes a pair of elements which is a Provider Component and a Consumer Component.2. A context providerOkay, so now we have our Context object. And to make the context available to all our components we have to use a Provider.   But, what is Provider? According to the React documentation:"every context object comes with a Provider React component that allows consuming components to subscribe to context changes"In other words, Provider accepts a prop (value) and the data in this prop can be used in all the other child components. This value could be anything from the component state.// myProvider.js import React from 'react'; import Theme from './theme'; const myProvider = () => ( ...   ); export default myProvider;We can say that a provider acts just like a delivery service.prop finding context and deliverling it to consumerWhen a consumer asks for something, it finds it in the context and delivers it to where it's needed.But wait, who or what is the consumer???3.  A context consumer What is Consumer? A consumer is a place to keep the stored information. It can request for the data using the provider and can even manipulate the global store if the provider allows it. In our previous example, let’s grab the theme value and use it in our Header component. // Header.js   import React from 'react'; import Theme from './theme';   const Header = () => (                        {theme => Selected theme is {theme}}             );   export default Header;Dynamic Context:   We can also change the value of the provider by simply providing a dynamic context. One way of achieving it is by placing the Provider inside the component itself and grabbing the value from component state as below:// Footer.js   import React from 'react';   class Footer extends React.Component {    state = {        theme: 'dark'    };      render() {        return (                                                );    } }Simple, no? We can easily change the value of  the Provider to any Consumer.Consuming Context With Class-based ComponentsWe all pretty know that there are two methods to write components in React, which is Class based components and Function based components. We have already seen a demo of how we can use the power of Context in class based components.  One is to use the context from Consumer like “ThemeContext.Consumer” and the other method is by assigning context object from current Context to contextType property of our class.import React, { Component } from "react"; import MyThemeContext from "../Context/MyThemeContext"; import GlobalTheme from "../theme";   class Main extends Component {    constructor() {        super();    }    static contextType = MyThemeContext;  //assign context to component    render() {        const currentTheme = GlobalTheme[this.context];        return (            ...        );    }   }There is always a difference in how we want to use the Context. We can either provide it outside the render() method or use the Context Consumer as a component itself.  Here in the above example, we have used a static property named as contextType which is used to access the context data. It can be utilized by using this.context. This method however, limits you consuming, only one context at a time.Consuming Context With Functional ComponentsContext with Functional based components is quite easy and less tedious. In this we can access the context value through props with the help of useContext method in React. This hook (useContext) can be passed in as the argument along with our Context to consume the data in the functional component.const value = useContext(MyContext);It accepts a context object and returns the current context value. To read more about hooks, read here.  Our previous example looks like:import React, { useContext } from 'react' import MyThemeContext from './theme-context'   const User = props => {    const context = useContext(MyThemeContext)    return ...Now, instead of wrapping our content in a Consumer component we have access to the theme context state through the ‘context’ variable.But we should avoid using context for keeping the states locally. Instead of  conext, we can use local state there.Use of Multiple ContextsIt may be possible that we want to add multiple contexts in our application. Like holding a theme for the entire app, changing the language based on the location, performing some A/B testing, using global parameters for login or user Profile… For instance, let’s say there is a requirement to keep both Theme context and userInfo Context, the code will look like as:       ...   It’s quite possible in React to hold multiple Contexts, but this definitely hampers rendering, serving ‘n’ number of contexts in ‘m’ component and holding the updated value in each rendered component.To avoid this and to make re-rendering faster, it is suggested to make each context consumer in the tree as a separate node or into different contexts.                 And we can perform the nesting in context as:    {theme => (                    {colour => (                Theme: {theme} and colour: {colour}            )}            )} It’s worth noting that when a value of a context changes in the parent component, the child components or the components’ holding that value should be rerendered or changed. Hence, whenever there is a change in the value of provider, it will cause its consumers to re-render.ConclusionDon’t you think this concept is just amazing?? Writing a global context like theme or language or userProfile and using the data of them directly in the child or other components?Implementing these stateful logic by global preferences was never so easy, but Context made this transportation job a lot simple and achievable! Hope you find this article useful. Happy Coding!Having challenge learning to code? Let our experts help you with customized courses!
7993
What is Context in React? How to use Context in Re...

What the hack is Context?Have you ever wondered ab... Read More

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.
6382
How To Run Your Python Scripts

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

Scala Vs Python Vs R Vs Java - Which language is better for Spark & Why?

One of the most important decisions for the Big data learners or beginners is choosing the best programming language for big data manipulation and analysis. Just understanding business problems and choosing the right model is not enough but implementing them perfectly is equally important and choosing the right language (or languages) for solving the problem goes a long way. If you search top and highly effective programming languages for Big Data on Google, you will find the following top 4 programming languages: JavaScalaPythonRJavaJava is one of the oldest languages of all 4 programming languages listed here. Traditional Frameworks of Big data like Apache Hadoop and all the tools within its ecosystem are Java-based and hence using java opens up the possibility of utilizing large ecosystem of tools in the big data world.  ScalaA beautiful crossover between object-oriented and functional programming language is Scala. Scala is a highly Scalable Language. Scala was invented by the German Computer Scientist, Martin Odersky and the first version was launched in the year 2003.PythonPython was originally conceptualized by Guido van Rossum in the late 1980s. Initially, it was designed as a response to the ABC programming language and later gained its popularity as a functional language in a big data world. Python has been declared as one of the fastest-growing programming languages in 2018 as per the recently held Stack Overflow Developer Survey. Many data analysis, manipulation, machine learning, deep learning libraries are written in Python and hence it has gained its popularity in the big data ecosystem. It’s a very user-friendly language and it is its biggest advantage.  Fun factPython is not named after the snake. It’s named after the British TV show Monty Python.RR is the language of statistics. R is a language and environment for statistical computing and graphics. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is named partly after the first names of the first two R authors and partly as a play on the name of S*. The project was conceived in 1992, with an initial version released in 1995 and a stable beta version in 2000.*SS is a statistical programming language developed primarily by John Chambers and R is an implementation of the S programming language combined with lexical scoping semantics, inspired by Scheme.Every framework is implemented in the underlying programming language for its implementation. Ex Zend uses PHP, Panda Framework uses python similarly Hadoop framework uses Java and Spark uses Scala.However, Spark officially supports Java, Scala, Python and R, all 4 languages. If one browses through Apache Spark’s official website documentation, he/she would find many other languages utilized by the open-source community for Spark implementation.    When any developer wants to start learning Spark, the first question he stumbles upon is, out of these pools of languages, which one to use and which one to master? Solution Architects would have a tough time choosing the right language for spark framework and Organizations will always be wondering, which skill sets are relevant for my problem if one doesn’t have the right knowledge about these languages in the context of Spark.    This article will try to answer all these queries.so let’s start-JavaOldest of all and popular, widely adopted programming language of all. There is a number offeatures/advantages due to which Java is favorite for Big data developers and tool creators:Java is platform-agnostic language and hence it can run on almost any system. Java is portable due to something called Java Virtual Machine – JVM. JVM is a foundation of Hadoop ecosystem tools like Map Reduce, Storm, Spark, etc. These tools are written in Java and run on JVM.Java provides various communities support like GitHub and stack overflow etc.Java is scalable, backward compatible, stable and production-ready language. Also, supports a large variety of tried and tested libraries.It is statically typed language (We would see details of this functionality in later sections, in comparison with others)Java is mostly the choice for most of the big data projects but for the Spark framework, one has to ponder upon, whether Java would be the best fit.One major drawback of Java is its verbosity. One has to write long code (number of lines of code) to achieve simple functionality in Java.Java does not support Read-Evaluate-Print-Loop (REPL) which is a major deal-breaker when choosing a programming language for big data processing.ScalaScala is comparatively new to the programming scene but has become popular very quickly. Above are a few quotes from bigger names in the industry for Scala. From the Spark context, many experts prefer Scala over other programming languages as Spark is written in Scala. Scala is the native language of Spark. It means any new API always first be available in Scala.Scala is a hybrid functional programming language because It has both the features of object-oriented programming and functional programming. As an OO Programming Language, it considers every value as an object and all OOPS concepts apply. As a functional programming language, it defines and supports functions. All operations are done as functions. No variable stands by itself. Scala is a machine-compiled language.Scala and Java are popular programming languages that run over JVM. JVM makes these languages framework friendly. One can say, Scala is an advanced level of Java.Features/Advantages of Scala:It’s general-purpose object-oriented language with functional language properties too. It’s less verbose than Java.It can work with JVM and hence is portable.It can support Java APIs comfortably.It's fast and robust in Spark context as its Spark native.It is a statically typed language.Scala supports Read-Evaluate-Print-Loop (REPL)Drawbacks / Downsides of Scala:Scala is complex to learn due to the functional nature of language.Steep learning curve.Lack of matured machine learning languages.PythonPython is one of the de-facto languages of Data Science. It is a simple, open-source, general-purpose language and is very easy to learn. It has a rich set of libraries, utilities, ready-to-use features and support to a number of mature machine learning, big data processing, visualization libraries.Advantages of Python:It is interpreted language (i.e. support to REPL, Read, Evaluate, Print, Loop.) If you type a command into a command-line interpreter and it responds immediately. Java lacks this feature.Easy to learn, easy debugging, fewer lines of code.It is dynamically typed. i.e. can dynamically defined variable types. i.e. Python as a language is type-safe.Python is platform agnostic and scalable.Drawbacks/Disadvantages:Python is slow. Big data professionals find projects built in Java / Scala are faster and robust than the once with python.Whilst using user-defined functions or third party libraries in Python with Spark, processing would be slower as increased processing is involved as Python does not have equivalent Java/Scala native language API for these functionalities.Python does not support heavy weight processing fork() using uWSGI but it does not support true multithreading.R LanguageR is the favourite language of statisticians. R is fondly called a language of statisticians.  It’s popular for research, plotting, and data analysis. Together with RStudio, it makes a killer statistic, plotting, and data analytics application.R is majorly used for building data models to be used for data analysis.Advantages/Features of R:Strong statistical modeling and visualization capabilities.Support for ‘data science’ related work.It can be integrated with Apache Hadoop and Spark easily.Drawbacks/Disadvantages of R:R is not a general-purpose language.The code written in R cannot be directly deployed into production. It needs conversion into Java or Python.Not as fast as Java / Scala.Comparison of four languages for Apache SparkWith the introduction of these 4 languages, let’s now compare these languages for the Spark framework:These languages can be categorized into 2 buckets basis high-level spark architecture support, broadly:JVM Languages: Java and ScalaNon-JVM Languages: Python and RDue to these categorizations, performance may vary. Let’s understand architecture in little depth to understand the performance implications of using these languages. This would also help us to understand the question of when to use which language.Spark Framework High-level architecture An application written in any one of the languages is submitted on the driver node and further driver node distributes the workload by dividing the execution on multiple worker nodes.JVM compatible Application Execution Flow Consider the applications written are JVM compatible (Java/Scala). Now, Spark is also written in native JVM compatible Scala language, hence there is no explicit conversion required at any point of time to execute JVM compatible applications on Spark. Also, this makes the native language applications faster to perform on the Spark framework.There are multiple scenarios for Python/R written applications:Python/R driver talk to JVM driver by socket-based API. On the driver node, both the driver processes are invoked when the application language is non-JVM language.Scenario 1: Applications for which Equivalent Java/Scala Driver API exists - This scenario executes the same way as JVM compatible applications by invoking Java API on the driver node itself. The cost for inter-process communication through sockets is negligible and hence performance is comparable. This is with the assumption that processed data over worker nodes are not to be sent back to the Driver again.Scenario 1(b): If the assumption taken is void in scenario 1 i.e. processed data on worker nodes is to be sent back to driver then there is significant overhead and serialization required. This adds to processing time and hence performance in this scenario deteriorates.Scenario 2: Applications for which Equivalent Java/Scala Driver API do not exist – Ex. UDF (User-defined functions) / Third party python libraries. In such cases equivalent Java API doesn’t exist and hence, additional executor sessions are initiated on worker node and python API is serialized on worker node and executed. This python worker processes in addition to JVM and coordination between them is overhead. Processes also compete for resources which adds to memory contention.In addition, if the data is to send back to the driver node then processing takes a lot of time and problem scales up as volume increases and hence performance is bigger problem.As we have seen a performance, Let’s see the tabular comparison between these languages.Comparison PointsJavaScalaPythonRPerformanceFasterFaster (about 10x faster than Python)SlowerSlowerLearning CurveEasier than JavaTougher than PythonSteep learning curve than Java & PythonEasiestModerateUser GroupsWeb/Hadoop programmersBig Data ProgrammersBeginners & Data EngineersData Scientists/ StatisticiansUsageWeb development and Hadoop NativeSpark NativeData Engineering/ Machine Learning/ Data VisualizationVisualization/ Data Analysis/ Statistics use casesType of LanguageObject-Oriented, General PurposeObject-Oriented & Functional General PurposeGeneral PurposeSpecifically for Data Scientists.Needs conversion into Scala/Python before productizingConcurrencySupport ConcurrencySupport ConcurrencyDoes not Support ConcurrencyNAEase of UseVerboseLesser Verbose than ScalaLeast VerboseNAType SafetyStatically typedStatically typed (except for Spark 2.0 Data frames)Dynamically TypedDynamically TypedInterpreted Language (REPL)NoNoYesYesMaturated machine learning libraries availability/ SupportLimitedLimitedExcellentExcellentVisualization LibrariesLimitedLimitedExcellentExcellentWeb Notebooks SupportIjava Kernel in Jupyter NotebookApache Zeppelin Notebook SupportJupyter Notebook SupportR NotebookWhich language is better for Spark and Why?With the info we gathered for the languages, let's move to the main question i.e. which language to choose for Spark? My answer is not a straightforward single language for this question. I will state my point of view for choosing the proper language: If you are a beginner and want to choose a language from learning Spark perspective. If you are organization/ self employed or looking to answer a question for solutioning a project perspective. I. If you are beginner:If you are a beginner and have no prior education of programming language then Python is the language for you, as it’s easy to pick up. Simple to understand and very user-friendly. It would prove a good starting point for building Spark knowledge further. Also, If you are looking for getting into roles like ‘data engineering’, knowledge of Python along with supported libraries will go a long way. If you are a beginner but have education in programming languages, then you may find Java very familiar and easy to build upon prior knowledge. After all, it grapevine of all the languages.  If you are a hardcore bigdata programmer and love exploring complexities, Scala is the choice for you. It’s complex but experts say if once you love Scala, you will prefer it over other languages anytime.If you are a data scientist, statistician and looking to work with Spark, R is the language for you. R is more science oriented than Python. II. If you are organization/looking for choice of language for implementations:You need to answer the following important questions before choosing the language:Skills and Proficiency: Which skill-sets and proficiency over language, you already have with you/in your team?Design goals and availability of features/ Capability of language: Which libraries give you better support for the type of problem(s) you are trying to solve.Performance implications Details of these explained below: 1. Skillset: This is very straightforward. Whichever is available skill set within a team, go with that to solve your problem, after evaluating answers of other two questions. If you are self-employed, the one you have proficiency is the most likely suitable choice of language.  2. Library Support:  Following gives high-level capabilities of languages:R: Good for research, plotting, and data analysis.Python: Good for small- or medium-scale projects to build models and analyse data, especially for fast start-ups or small teams.Scala/Java: Good for robust programming with many developers and teams; it has fewer machine learning utilities than Python and R, but it makes up for it with increased code maintenance.In my opinion, Scala/Java can be used for larger robust projects to ease maintenance. Also, If one wants the app to scale quickly and needs it to be robust, Scala is the choice.Python and R: Python is more universal language than R, but R is more science oriented. Broadly, one can say Python can be implemented for Data engineering use cases and R for Data science-oriented use cases. On the other hand, if you discover these two languages have about the same library support you need, then pick the one whose syntax you prefer. You may find that you need both depending on the situation. 3. Performance: As seen earlier in the article, Scala/ Java is about 10x faster than Python/R as they are JVM supported languages. However, if you are writing Python/R applications wisely (like without using UDFs/ Not sending data back to the Driver etc) they can perform equally well.ConclusionFor learning, depending upon your prior knowledge, Python is the easiest of all to pick up. For implementations, Choice is in your hands which language to choose for implementations but let me tell you one secret or a tip, you don’t have to stick to one language until you finish your project. You can divide your problem in small buckets and utilize the best language to solve the problem. This way, you can achieve balance between optimum performance, availability, proficiency in a skill, and sub-problem at hand.  Do let us know how your experience was in learning the language comparisons and the language you think is better for Spark. Moreover, which one you think is “the one for you”, through comments below.
8310
Scala Vs Python Vs R Vs Java - Which language is b...

One of the most important decisions for the Big da... Read More

20% Discount