Tag Archives: PyCharm

Firsts steps programming in Python 3 with PyCharm and Git

This is a very simple guide for beginners, to benefit from using this amazing Python IDE with Git.

You may think that it’s a bit difficult and why you should use it. The first reason I would tell is because it marks the errors, not just syntax errors also if you have typos or you forgot an import or you forgot to declare a variable, has autocomplete, will mark when you don’t follow standards, etc… If you want to be a professional, this is the tool that most Python professional developers and companies use.

Go to Jetbrains page and from there select Developer Tools and PyCharm.

You can use this url to go directly:

https://www.jetbrains.com/pycharm/

PyCharm Community Edition is completely free.

If you don’t have it, create an account in Gitlab. I recommend you to use Gitlab over Github.

Before continuing you’ll have to validate your account by clicking the email that Gitlab sent you.

Create a New project, by pressing the blue button with this label.

Choose Create blank project.

If you select Public everybody will be able to see your code, I recommend you to start with a Private project.

Let checked Initialize repository with a README as this will save you plenty of time and will allow you to start working immediately.

The project will be created for you.

Hit the Clone blue button and it will provide two methods to clone your git repository. You will copy the Clone with HTTPS url.

There are different ways to clone a new project.

Assuming you already have one opened go to Main Menu > Git > Clone

In the URL field paste the URL you copied from Gitlab, in my case:

https://gitlab.com/carles.mateo/carles-mateo-demo-project.git

And press the blue botton Clone.

If you don’t have Git installed, PyCharm will tell you in red, and will allow you to install it for you. It is a very convenient way to install it, specially if you use Windows.

In Ubuntu or Debian Linux installing git is so easy as typing from the Terminal:

sudo apt install git

If it’s your first time PyCharm will ask you for your credentials in Gitlab, so your username or email, and your password.

As it is you own new project and it’s empty, you can trust it. Hit Trust Project.

A window with tips will appear, you can close it.

As you see your project is empty. Only has a the default README.md file created by Gitlab.

Creating new Python files

On the top left, over the name of your project folder press with the Right button of the mouse and select New > Python File

Enter the name of the file, I choose helloworld and press Enter.

You will be asked if you want to add automatically the file to Git. This refers to your local Git repository. Say Yes and you will see the file changing from color Red, which means it is not added to the repository to Green. Green means that is added, but has never been committed.

Just add this line to helloworld.py

print("Hello World")

Note: I made a typo and I wrote “helloworl.py” instead of “helloworld.py”, I will show how to fix this later.

Now, press over the file name over the top left, with the Right button of the mouse, and select Run helloworl (In your case will be Run helloword)

Note: To be able to run a Python program, you need to have Python installed.

Note2: Do not start the name of your file by test as PyCharm will consider that you are writing Unit Testing Code, and will not show the option to run the code, but to run with pytest (if you have pytest framework installed).

And you see on the bottom how it worked and printed “Hello World”.

To rename the file you’ll click over it with the Right button of the mouse and select Refactor > Rename

Type the correct name, in my case “helloworld.py” and for the Scope select: Current File.

PyCharm is so powerful that can search for your entire project to see if other files are using that file and will update the code to reflect the file name change. By indicating Scope: Current File we are telling that the change affects only to this file, and by not marking Search for references and not marking Search in comments and strings you are indicating to PyCharm that it should not search in code and comments to update them with the new file. This option is really useful when you get more confident with programming.

Perfect. Now you want to commit the file to the repository and Push.

The first time PyCharm will ask you how people will know you, that is, your visible name in the Git History of Commits, and your email. You can mark to use these properties locally. This is basically because Git needs to know how to identify you. Consider that Git was created to work with Teams, so everybody should know who made what. Add your friendly name, like “Carles Mateo” and your email. I recommend you to check to use this info for Git globally (all the projects in your computer).

When you commit you’re expected to provide a Commit Message. As Git is used by teams, to work on the same code, try to explain briefly what your changes consisted on.

If you press Commit, you will see how helloworld.py is no longer in color green, now is black. This means that the file is up to date respect our local Git.

Go to the Main Menu and select Git > Push.

You’ll see a message in the bottom indicating that your code has been pushed to the repository.

Now, you can refresh your browser, and you’ll see the changes in Gitlab:

PyCharm vs VSCode

I got asked what are the advantage from PyCharm respect VSCode.

I may refer to this excellent comparison:

While VSCode has some great support for Python coding with the ‘Python’ plugin by Microsoft, PyCharm is truly designed for Python development and it shows.

https://tangenttechnologies.ca/blog/pycharm-vs-vscode/

https://tangenttechnologies.ca/blog/pycharm-vs-vscode/

A trick to see what causes Python error Unindent does not match any outer indentation level with PyCharm

That’s one of the problems with Python. Blocks of code are defined by their indentation position.

That’s a pain when you copy and past and the IDE reindents the code thinking that is doing great, or generate a new inner class instead of replacing all the code.

Well, this error is very annoying cause it means that you mixed spaces and Tabs as indent separators.

But you can go crazy trying to find a tab in your code, so there is a trick that I came with:

Basically go to Menu Edit > Find and then type 4 times space. PyCharm will highlight all the places were this indentation (4 spaces) is present, so you’ll find the impostor without going blind or losing to many time.

As you can see, in front of def execute_command_without_waiting we don’t have 4 spaces. And in this case the impostor was not a camouflaged tab \t but 3 spaces instead of four.

Programs I use for Windows in my Workstations

I love Linux and Linux tools and I’m a big fan of it, using it for Servers since 1995.

However some companies use Windows for the Workstations, and that’s not necessarily bad.

So I describe here the tools I use to maximize productivity.

Antivirus

That really depends on my employers. I’ve my opinion about several of them.

Apache Directory Studio

For working with LDAP.

BalenaEtcher

To flash images to USB and external drives. https://www.balena.io/etcher/

I also use Rufus https://rufus.ie/

Chrome

Debut
This is a Commercial Software to capture video. I record bugs, tutorials, internal web training sessions…

Docker Desktop for Windows

But not in the laptops cause the hyper-v may conflict with the BitLocker drive encryption and may cause the entire drive to be lost.

However as much as possible, I will do everything in Linux Workstations and Servers.

(CRLF problems in Docker Linux are horrible)

Editplus

Very powerful for doing replacement over large CSV files.

Firefox

Filezilla

GIMP

HeidiSQL

Free Database Manager for MariaDB, MySQL, SQLite, PostgreSQL and Microsoft SQL.

Is compatible with Wine, so you can use it on Linux.

LastPass (for Teams)

With Chrome’s plugin.

LibreOffice

MobaXTerm

With zmodem, sftp, SSH, tunnel….

MyDebugger

A MySQL debugger. Specially useful for Stored Procedures.

Is compatible with Wine, so you can use it on Linux.

OBS

https://obsproject.com/ is a screen recorder Software, for Linux, Mac Os X and Windows.

One Note

OpenVPN

Opera

Specially useful the option of using a VPN.

Project (Microsoft Project)

PuTTy

PyCharm, PHPStorm, CLion from JetBrains

Python

Radmin

A powerful Server Remote Control for Windows, much more stable than VNC.

Remote Desktop Connection

Rufushttps://rufus.ie

To toast ISOs to USB.

Slack

Toad for Oracle

Videopad

Video editor

VirtualBox
Yes, I always have a Linux VM.

Visio

For the Diagrams.

VLC

Video player

VMWare Player

VNC

Normally RealVNC.

WinRAR

Zoom

News from the blog 2020-08-19

  • I assisted to the OpenZFS leadership meeting.

I tried to continue following it since I left Sanmina. ZFS is really an amazing Software and it’s lead by an amazing Community of super cool Engineers and companies. I would like to continue contributing ASAP.

I bought some new hard drives in order to work a bit on this. You don’t need to have dedicated hardware if you want to test features. You can run in a VirtualBox or VMWare Workstation.

  • I received more books about DevOps and Python

None is perfect. I see flaws in all of them and bad architecture practices*, however from all I learn interesting things.

*I guess that’s why I wrote my own book :)

You know, I study every day. At least 30 minutes, after work. As part of my healthy routines.

But I also study and learn during the work, as we have time available for this.

I’m very fortunate that Blizzard gives me time every day to study. That’s amazing. They also send us to events paying the ticket, travel, hotel, expenses… now with covid-19 we only go to virtual events, but the company still pay for this and give free days. Is a very nice company.

I use a lot Linux Academy too:

I continue having purchases of my book, and I’m very happy about that. I’m working on improving it and providing more contents and samples going from the scratch, with step by step code samples. From spaghetti code reading CSV files, to OOP with Full Coverage.

  • My application for a Higher degree Computer Science Cloud Computing (Level 8) has been accepted. The Irish government pays me 90% of the degree, and Blizzard will pay me the other 10% after I pass the first year course.

I’m really grateful to this beautiful country, Ireland.

Having an Irish degree is something that brings me an special illusion.

  • I have updated CTOP.py with some interesting features

It allows to pass a fixed width and height for the terminal render. That’s very useful when you run CTOP in a Docker non interactive session, or from a Cron, with the –iterations=1 so the output can be captured programmatically.

  • Jetbrains has provided me with a Free License of all their products, in order to support my work in Open Source projects. That’s very nice. I’m using now mainly PyCharm and PhpStorm.
  • At the beginning of the covid-19 I wrote a simulator in Python. That’s why I was able to anticipate that the number of cases and deaths would be very much higher when nobody around me knew what was going to happen. My first simulations were simple, and the algorithms were growing in complexity until I had a full rich Object Oriented modeler. Maybe I’ll write an article about this someday.
    • I based my data in https://www.worldometers.info/coronavirus/#countries
    • I studied the evolution of several countries and I was working with simulations in Spain until their government started blocking the information and stop providing transparent and accurate metrics.
  • I’m seeing how the covid is affecting and transforming several kind of business:
    • Meetup.com I see meetups with more than 1,000 users closing, as they are no meeting anymore
    • Airlines, obviously
    • Hotels, offering less services
    • Metasearchers and OTAs (Online travel agencies)
    • I can imagine the impact on airbnb
    • Discos, nightclubs are closing doors
    • Restaurants, they will lose the Christmas season (with families and companies doing lunch and dinners)
  • At the same time, other companies are hitting records in sales
  • After doing a Masterclass to some colleagues about Refactor, Code Reliability, Quality, The non-happy path and Unit Testing, I’m preparing some contents that I’ll publish to the Community soon. So far I created this repo, where I added the source code for lesson 0: starting to program in Python videos that I created few months ago to help beginners.

https://gitlab.com/carles.mateo/teach-unit-testing/-/tree/master/lesson0

I also added some contents to lesson 1, where we refactor pure spaghetti code with no error control, to something more elaborated with unit testing and full code coverage. Still procedural, but I will jump to next class in two weeks, where we will move to OOP and Dependency Injection.

  • Here my “Harley” assembled :)

Lesson 0, learning to code in Python for non programmers

Please note: Even if I tried to make it easy, probably there are too many concepts for a non-programmer. Will try to deliver more basic previous knowledge and foundations, so people with zero knowledge don’t feel overwhelmed.

Start by installing Python 3.8 or 3.9 in your computer, and the IDE PyCharm. Install also Git, and create an account in GitLab so you can share code with other people and understand how Git works.

Here you can read the basic steps for setup PyCharm and GitLab.

Ok, so you can take a look at my video, and hopefully it makes spark your motivation to learn by yourself. :)

I’ve been asked why I used print(“”) instead of print().

Is a good question. The reason is, when we programmed in Python 2.x the native way was to print without parenthesis, like:

print "Hello World!"

Python 3.x was incompatible with that and requires to use parenthesis, like:

print("Hello World!")

Fortunately Python 2.x accepts also to print using parenthesis. In order to have compatibility within Python 2.x and Python 3.x or for future compatibility we were using always print(“Whatever”) in Python2.

However, there is one difference.

If you user print() or print(“”) in Python3 that will generate an empty line.

In Python 2 print(“”) will generate too an empty line, nevertheless print() in Python2 will print two parenthesis. We don’t want that.

This is illustrated in this screenshot:

So all the people that are at home, closed down for coronavirus, you have a chance now to start learning Python and from there get a live as programmer.

You can download the code for this lesson 0, from:

https://gitlab.com/carles.mateo/teach-unit-testing/-/blob/master/lesson0/tree.py

Capturing data from keyboard

In order to be able to do more samples, and then being a bit interesting an dynamic, I will introduce here how to get data inputted by the Keyboard.

print("Please enter your name:")
s_name = input()

This will add whatever we type, without the final Enter, to the String variable s_name.

Capturing numbers from Keyboard

How we do to capture a number, like how old are you, in years?.

The same way, and then we convert this to an Integer value. An Integer is a data type which is basically a number, not decimal. Like: 1, 2, 7, 1000 o -5.

print("Please enter your name:")
s_name = input()

print("Please enter your age:")
s_age = input()
# With int() we convert a String to an Integer, as long as it is possible.
# Wit str() we convert a Integer to a String, as long as it is possible.
i_age = int(s_age)

If you enter a number incorrectly and so that cannot be converted, you will get an Exception Error. That is something that happened in a way that was not expected. These error can be trapped, and we will see this later, in the future.

You know:

  • How to capture data from the keyboard with input()
  • How to convert data entered as String to Integer with int()
  • How to sum two numbers, like 2 + 3
  • How to subtract two numbers, like 2 – 3
  • How to multiply, like 2 * 3

So know, you should be able to solve a basic arithmetic exercise in Hacker Rank:

https://www.hackerrank.com/challenges/python-arithmetic-operators/problem

More sources with explanations

I’m teaching Unit Testing, Refactors, Quality Code and moving from Procedural to OOP to some colleagues, you can find source code for our classes here (please, be aware that there are some error made on purpose to show why and why not do things and hot to apply proper unit testing)

https://gitlab.com/carles.mateo/teach-unit-testing/-/tree/master

More resources

There are many free useful resources to learn Python:

A handy trick command line to get the usages of our Python Methods in the code

We all use powerful code analysis tool, but sometimes you’re presented with a problem and you have just… the terminal.

This Bash code is handy.

grep "def " /home/carles/code/gitlab/cloud/terraform/src/scale/lib/iscsi.py | tr "()" "  " | awk '{ print $2; }' |  grep -v "__init" | sort > ./function_names_iscsi.txt

So this basically will get all the methods (“def ” whatever), strip the parenthesis with tr, and get the second column with awk, so basically the method name, sort it and write it to the file.

Then I will cd to the src directory and execute the seconds part:

cd /home/carles/code/gitlab/cloud/terraform/src/
for fname in $(cat ~/function_names_iscsi.txt); do printf "%s: %s\n" "$fname" "$(grep -r $fname *|grep -v 'def ' -c)"; done > ~/functions_being_used.txt

That will produce a nice list with the number of times of the method being called, in the form of:

method_name: occurrences

That’s the equivalent to doing Find Usages is PyCharm.

It’s easy to identify dead code then, with method_name: 0.

You can also run this to your Jenkins to warn when there is Dead Code in your repository.