Thursday, August 6, 2020

Python IDE, PyCharm for Beginners Vol 9 rel 13

PyCharm for Beginners

Before we delve into the intricacies of what makes Python… We should arm ourselves with an Integrated Development Environment, IDE.  An IDE for Python helps all the Python coders develop syntactically correct, and clean code.  In addition, it affords the luxury of tracking versions, and readily providing accessible categories of code for repeated use. 

This session will be mainly focused on PyCharm as it has been the best utilized IDE for the most used general-purpose programming languages of today which is Python.


What is an IDE?

IDE is the short for an integrated development environment.  Specifically, a software environment used to write other programs using tools like an editor or compiler.  An IDE can prove to be an extremely useful tool but coding using various languages for multiple reasons and requirements, it allow the coder to organize their code and provide efficiency in their process, in short, it makes your life so much easier and I'm sure you guys will agree at the end of this document.

 

Why use an IDE?

While it is not necessary to use an IDE to write a program, but the added features with the IDE provides are useful.  First, we have code insight.   Code Insight is one of the most helpful tools that an IDE can provide.  It provides the ability to interpret what is being typed out by the user, so the program can change categorize color to represent different classes, functions, and variables. Some of the more common features are:

  • An Interactive interface that helps the programmers by detecting syntactic or semantic errors while developing. Contrary to non-IDE where errors are found during the build. This eventually reduces the debugging time.
  • Reduces complexity related to the configuration of the environment.
  • Inbuilt version controlling mechanism.
  • IDEs provide the provision of Visual programming that allows programmers to create flow-charts, block diagrams etc.

Some of you may be familiar with Microsoft Visual Studio.  In MVS, they have a similar function called intellisense that predicts what you are typing out and finish the line for you.  Much like other IDE’s offer a similar tool called something proprietary to that program.  Microsoft calls it as intellisense but depending on the IDE it might be called something else.

Another feature that is crucial to the success of any programmer is the ability to debug a program.  As one of the first programming assistance tools out there, this must be one of the most important tools to deploy.

When the code cannot handle the specific error and it crashes, debugging provides you with the ability to run through the entire program stopping the code at a specific point to check values of the variables,  functions, or the code itself are running as intended.

Finally, a few more tools at the IDEs offered can be resource management and a compiler.   When writing a new program there are usually many different files that have been referenced in a specific path locations, so it makes it very important for these files are in the correct location and are accessible to all of the dependencies.  Using an IDE makes it easier to see a visual representation of the location of these items.  In addition, this organized coding, makes it more understandable for the user as well.

  Remember, the very coder that may have to review the code years later, maybe you.  Hence, the more organized and properly annotated your code is, the better you must understand it is goals, functions, and outputs.

No one will ever argue that increased efficiency produces not only faster coding, but with less effort.


Introduction to Python

Python is an open source, interpreted, general-purpose programming language. It uses the concepts of object-oriented programming that help programmers develop large applications by splitting it into smaller building blocks. It was first developed by Guido van Rossum in 1990. One of the primary reasons Python became famous is due to the availability of numerous modules (a.k.a. standard library) that are publicly available for commercial use. Also, learning python is relatively easier than other programming languages due to its lesser complexity in syntaxes and semantics. Nowadays, popular uses of python are in Data Science, Machine Learning, Web Development, etc. Guess What! The world’s first Black Hole image was also developed using Python – it is that powerful.

Python is a language that has thousands of libraries and modules to work with.  Many have found that Python is fun to work with when compared to all the other languages out there.  It is very beginner friendly which is extremely important, and the syntax is quite simple to figure out as well.  As a beginner, these features are very vital.  Not to mention, best of all no semicolons.

In short, who would not just love Python with all its libraries being open source.   


Why Should a Coder Learn Python?

Python is a general-purpose language that has proven to be the number one choice for a wide range of applications.  Everything from web development, using Django or Bottle, important mathematical

computations using NumPY SymPy, and even designing your own GUI using Pygame and PANDA3D.

 

Introduction to PyCharm

Another good thing about Python is that we can make use of any number of IDEs.  There are multiple approved IDEs with support of Python.  Everything from Eclipse, Notepad++, Bluefish, and Komodo have been celebrated as great environments as well. 

 

As a result, look at the vast selection of IDEs as a preference, not too far from a favorite flavor of ice cream.  With picking a favorite ice cream flavor, you really must try it first, and only then will you realize if this is meant for you/

Python was written knowing that to build a software application right there needs to be code that is concise, clean, and readable.  

A company called jetbrains has developed PyCharm as a cross platform IDE for Python In addition to supporting versions 2.x and 3.x of Python. PyCharm is also compatible with windows Linux and even MacOS.

At the same time features provided by Python helps programmers to write a variety of software applications with Python very quickly and efficiently less so that developers can even customize the PyCharm UI according to their specific needs and preferences.  Moreover, they can extend the IDE by choosing over 60+ plugins to meet complex project requirements.  

As far and industry traction, Python IDE is being used by all the large enterprises like Twitter, Pinterest, HP, Symantec and even Groupon.

 

Features PyCharm

Code Editor

The first important feature we have is the code editor.  The intelligent code editor provided by PyCharm enables all his programmers to write high-quality Python code by using different color schemes or insert intensive lens

Note: Lens mode is a feature in PyCharm that allows you to view issues quickly and easily in your code by merely hovering over markers on the right-side scrollbar. You can see problems, and you can click and move directly to the part of the code that is marked. Once you are there, you can use intent actions by using alt-enter which displays a list of suggestions.

to automatically pick the appropriate coding style and a very context aware code completion as well.  At the same time, the programmers can also use the editor to expand the code block to an expression or logical blocks.  It can also identify errors, misspellings, detect duplicate code, and even auto generate code if need be.  All this can be done on the fly, i.e., analyze the code overall and identify the errors while writing the code.

Code Navigation

PyCharm basically helps improve the code without putting any extra effort or time the idea by easily assisting the programmers to go to a class, or a file a symbol and go down to the declaration invoked from a particular reference.  With Code Navigation, the user can even find an item in the source code or the code snippet, UI element, or a user action almost immediately.

Code Navigation allows to locate usage of various symbols and set benchmarks in the code, and at the same time the developers can even take advantage of the code navigation feature to scrutinize the code throughout.

Refactoring

Refactoring is the process of altering an application’s source code without changing its external behavior. The purpose of code refactoring is to improve some of the nonfunctional properties of the code, such as readability, complexity, maintainability, and extensibility.

Refactoring can extend the life of source code, preventing it from becoming legacy code. The refactoring process makes future enhancements to such code a more pleasant experience.

PyCharm definitely makes it easier for the developers to implement both local and global changes really quickly and efficiently guys so that developers can even take advantage of the refactoring options provided by the IDE while writing Python code and working with Python frameworks.  Changes to local and global variables as well as other methods to introduce efficiencies that can improve the code quality are easily executed.  For example, extracting variables fields, parameters, classes, etc.  Python allows programmers to break up longer classes and methods through the extract method.

Support for Web Technologies

Makes it easier for programmers to write various web applications in Python.  Python basically supports technologies like HTML, CSS, JavaScript, typescript, and even CoffeeScript.

NOTE:  CoffeeScript is a little language that compiles into JavaScript. The golden rule of CoffeeScript is: “It’s just JavaScript.” The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable, printed, and tends to run as fast or faster than the equivalent handwritten JavaScript.

In PyCharm, web developers have a live editing preview option to view a single webpage simultaneously in the editor as well as the browser.  Hence, simultaneously the live edit feature provided by the ID enables programmers to see the changes made to the code instantaneously on a web browser.

PyCharm’s Web Assistance avails script debuggers as well as coffee script and typescript editors, it even simplifies isomorphic web application development

NOTE: In web development, an isomorphic application is one whose code (in this case, JavaScript) can run both in the server and the client.

because it supports both angular JS and node JS.

AngularJS is a JavaScript framework, whereas NodeJS is a cross-platform runtime environment. As a client-side JavaScript framework, AngularJS Angular enables developers to create dynamic web applications based on model-view-controller (MVC) architectural pattern and using HTML as a template language.

Support for Python Scientific Libraries

PyCharm further helps programmers to use Python more efficiently in big data and data science projects. It supports some of the widely used scientific libraries for Python started from NUM PY, Anaconda and matplotlib, et. al.

The developers can work efficiently with all these scientific libraries by availing the interactive graphs, deep code insight, array viewers, provided by the IDE.  PyCharm can even run the REPL Python console provided by PyCharm to avail robust features like on the fly syntax check and code inspection.  In addition, programmers can also integrate the IDE seamlessly with the iPython notebook to create innovative solutions without putting extra time and effort.

 

Tools of PyCharm

Database Tools

In addition to supporting various Python libraries and frameworks, PyCharm allows developers to work with several relational databases including Oracle, SQLServer, mySQL, an even postgresSQL.

Developers can further use the IDE to run the queries edit SQLcode, browse data, alter table data, and even analyze the schemas.

Python further supports sqlalchemy library and inject SQL code into code which is returning various programming languages as well.  The professional edition of the IDE further makes it easier for the developers to hide the large volumes of data efficiently through the data.

Visual Debugger

The Visual Debugger provided helps the programmers to debug Python, javascipt, and Django code directly.  Provided by light basically helps the programmers to debug Python JavaScript engine will go directly.

The developers can use the Enlight debugger to see live debugging data directly on the editor.  Likewise, they can build multiple Python processes simultaneously and step through the code bypassed libraries as well. PyCharm further creates reusable and customizable configuration for each test script or debugger execution.

The user can have an option to facilitate remote debugging by integrating the visual debugger with the remote interpreters directly. 

Built-in Terminal

The IDE comes with a local terminal for Windows, Linux, and Mac.  The Built-in terminal basically enables the programmers to continue the code and test without ever leaving the IDE.   With this, the programmers can now use the IDE to basically but the Python files and configure custom Python environments.

Developers can run interactive Python or Django consoles directly into the IDE and the console provides a useful feature like code completion and dynamic syntax change.

Software Testing

PyCharm also comes with features and tools to simplify Python application testing.  It allows developers to perform unit testing through popular Python testing framework like Nose2, Testify, and DocTest to name a few.   

The testers even have an option to run individual or multiple test files and test classes. They can further integrate the ID with coverage or PY to measure the code coverage while testing the applications as well and while testing multithreaded applications.  The testers can use a thread concurrency visualization option provided by the Professional version of the IDE to control the application fully and efficiently. 

NOTE: The concurrency visualization tools shows the real time states of threads inside the running process in the Threading graph tab of the Concurrent Activities Diagram tool window.

PyCharm enables users to deliver high quality software by implementing behavior driven development.

Remote Development Capabilities

PyCharm allows developers to connect with various machines and build software applications remotely. The programmers can avail the built-in SSH console provided by the IDE to basically connect to machines and perform various development tasks remotely through SSH.

They can even run, debug, and profile the Python applications in a remote environment by replacing the local interpreter with that of the remote interpreter.

PyCharm enables programmers to create reproducible development environment through a robust tool called, vagrant.  This tool basically simplifies distributed application development using docker. 

The users even have the option to integrate PyCharm seamlessly with issue tracking systems as well.


Overview of the PyCharm Interface

So the interface let me open up my Python sugar is this is probably the first screen that

Python greets you with so on the left here you'll basically have all of the codes that you've actually worked with or any other  recent projects that you've opened.  In this case, there are quite a handful of projects so this might be blank for you if you are installing PyCharm for the first time.

Installing python amazingly simple so it is free, and installation is extremely straightforward as well so all you need to do is go to the official website and select the free community version.

As it is already installed on this machine you see projects already in the right column, where yours may be blank. To create a new project in Python, all you need to do is it create new project on the right side and give a name for the project – for this example, just type it out as charmtest.

You might not have the Python interpreter by this come back to this so for now let us just hit create and let PyCharm create the environment.

PyCharm creates the project folder for us as you can check out on the left and it is client base and this part actually queries all the skeleton procedures and performs a force crawl through and checks all of the installed packages.  This step may take a while.


When complete, you can go to the UI and got to file tab in the upper left and click New in the pull down. Select New scratch file.  Next, we choose what type of file are we trying to do here see based so the file we are trying to do is Python, so select Python.  

So, as you can see, we have a scratch file which is a scratch dot PY guys so you can basically right click and rename just or whatever you want.

We click on add configuration click on the tiny plus icon Python And then we don't have any configurations right so let me name this test config and then the script path the script part basically is providing the path to the particle script so on spikes on test and then as soon as we come into the environment sure is very be actually checking out our interpreter guys so multiple versions of Python installed as you can see at 3.8. so, there is a particularly good chance that this might not show for you because you have not installed, Python And it is quite simple to install.


Installing Python within PyCharm

To install an interpreter from within PyCharm, just go to File: Settings: and in settings so click on the  project interpreter that is under your Project: ProjectName and on the right side you can see the current interpreter you are using.  In this case, we are using Python 3.8.

Now, if you have not installed it,  go to the gear icon on the right, and click add sure you can actually use the system interpreter so we might guess I've installed it using Python 3.8. 

Make sure you click this and it OK and you are done.

 

Adding Packages

So how can we Add all the packages to Python not everything comes in built right so again go to File: Settings is pretty much almost same way, so we go to the interpreter and then we have the packages back under this.

We use the gear icon to actually check out our interpreter now check out all the list we have faced so basically these are all of the packages that are installed for my particular case so these are all of the libraries that are installed there so in case if you need to add a library hit on the plus icon on the right side so this type in any of the libraries that you require right so there are multiple.  The ones in blue are already installed.  So, you just click the desired package and click the lower left button “Install Package” and that is it.


PyCharm Coding Features

Code Insight

So, let us try to type some code.  Let us try to quickly import of a package.  I just basically type out the first 3 letters, then it prompts me to pick up the the suggested command, so this is how intellisense walks.

As such, this is the intellisence of PyCharm, so back to the example, at this point of time you can actually choose to type in the entire thing like IMPORT or select the option.  So, this was just basically one line of code so imagine if you must write hundreds and thousands of lines of code then this is a lifesaver.

Another Example: When trying to import pygame, you can see everything is grayed out you're right so we  can actually change the color by making sure we actually use something right so let me quickly go ahead mygame.net for example I think you see the color change right so this is basically telling us that by game  is being used.

 

 

Refactor                                                        

Another example of programming assistance… Let us suppose you are writing a simple program that sets a parameter to the numeral “10”, i.e.., i equals to 10 and we want to print it five times.  I so I think you already guessed this is not a very complex code right so it is going to print I so as soon as I run this piece of code I'm supposed to get I printed to be like 5 times.

Let us suppose you decide to change the parameter “I” to “p”.  We can probably go ahead and changes manually right so I can go ahead and change this to P again one more again it took like almost 20 seconds to do this for five times,  so imagine if someone has to do this for thousands of lines of code so it becomes really annoying.


PyCharm helps us so let me quickly select this “P” you can see this is selected every peer in the code in the program. As such, now we right click on the selected “p” or to “refactor” and then hit rename and place the new name in. 

Once that is done it changed everything else got changed in just one click and this is also an extreme time saver at the very least.

 

 

 


Keyboard Shortcuts

 



Next up there are multiple keyboard shortcuts you can use as well.

CTRL F

We can use control F to actually find something and then use it right so this actually works even in scope as well so let's say you're in the scope of a function or a user defined function or something and then you want to find something in that particular scope.  The CTRL F keyboard shortcut search option every instance of that item within your code.  Hence, what would have done multiple changes. 



ALT SHIFT C

Let’s suppose you well into coding for an hour and you just want to check out what all the changes that you have done, so we actually press alt shift c and see to actually give us a list of changes that we have actually done.

alt shift c reveals all the changes that we have done. We can see what we deleted or updated. These items listed are in reverse order.  You can see that they are definitely useful in case you actually want to step by or check out one of the changes, or repeat on of the , e.g.,  if you just want to print the same thing over and over again you can do



CTRL D

Let’s say we want to type a print command 5 times, like print (goat), so instead of typing it out or copying and pasting into something that is actually a short bit right that would be so control D basically duplicates this entire line.

 




ALT SHIFT INS

The next feature to select the current column can be achieved by toggling alt shift ins. Select “print” hit alt shift ins and scroll down.   This allows you to group modify indents or data fields, etc. For a wealth of information on selecting ranges and the detailed benefits of each option, goto: https://www.jetbrains.com/help/rider/Multicursor.html



Conclusion

This concludes all the basics of PyCharm.  The What, Why, How of it all.  Including some useful Keyboard Shortcuts, i.e., all the tips and tricks that you can use with it.  For more information on a series of shortcuts goto: https://www.jetbrains.com/help/pycharm/working-with-source-code.html#editor_lines_code_blocks

Share and enjoy…

 _________________________________________

We would like to thank our sponsors, for without them - our fine content wouldn't be deliverable!


Source
So “Once more unto the breach, dear friends, once more;”
____________________________________________________________
About Rick Ricker
An IT professional with over 23 years' experience in Information Security, wireless broadband, network and Infrastructure design, development, and support.
Currently a Computer Science Instructor at CSULB

 

 

 


No comments:

Post a Comment

Thanks for your input, your ideas, critiques, suggestions are always welcome...

- Wasabi Roll Staff