GitHub Copilot: Fly With Python at the Speed of Thought

GitHub Copilot: Fly With Python at the Speed of Thought

by Bartosz Zaczyński Updated Reading time estimate 52m intermediate ai editors tools

GitHub Copilot is a thrilling new technology that promises to deliver to your code editor a virtual assistant powered by artificial intelligence, and it stirred up considerable controversy when it was released to the general public. Python is among the languages that are particularly well-supported by this tool. After reading this tutorial, you’ll know whether GitHub Copilot is a risk, a gimmick, or a true game changer in software engineering.

In this tutorial, you’ll learn how to:

  • Install the GitHub Copilot extension in your code editor
  • Transform your natural language description of a task into working code
  • Choose between multiple alternative intelligent code completion suggestions
  • Explore unfamiliar frameworks and programming languages
  • Teach GitHub Copilot how to use your custom API
  • Exercise test-driven development with a virtual pair programmer in real time

To continue with this tutorial, you need to have a personal GitHub account and a code editor such as Visual Studio Code or an integrated development environment like PyCharm.

Get Started With GitHub Copilot in Python

GitHub Copilot is the first commercial product based on the OpenAI Codex system, which can translate natural language to code in over a dozen programming languages in real time. OpenAI Codex itself is a descendant of the GPT-3 deep learning language model. The neural network in Codex was trained on both text and hundreds of millions of public code repositories hosted on GitHub.

GitHub Copilot understands a few programming languages and many human languages, which means that you’re not confined to English only. For example, if you’re a native Spanish speaker, then you can talk to GitHub Copilot in your mother tongue.

Initially, the product was only available as a technical preview to a select group of people. This has changed recently, and today, anyone can experience the incredible power of artificial intelligence in their code editors. If you’d like to take it for a test drive, then you’ll need a subscription for GitHub Copilot.

Subscribe to GitHub Copilot

To enable GitHub Copilot, go to the billing settings in your GitHub profile and scroll down until you see the relevant section. Unfortunately, the service doesn’t come free of charge for most people out there. At the time of writing, the service costs ten dollars per month or a hundred dollars per year when paid upfront. You can enjoy a sixty-day trial period without paying anything, but only after providing your billing information.

Students and open-source maintainers may get a free GitHub Copilot subscription. If you’re a lucky one, then you’ll see the following information after enabling the service:

GitHub Copilot Billing Status
GitHub Copilot Billing Status

GitHub will verify your status once a year based on proof of academic enrollment, such as a picture of your school ID or an email address in the .edu domain, or your activity in one of the popular open-source repositories.

For detailed instructions on setting up and managing your GitHub subscription, follow the steps in the official documentation. Next up, you’ll learn how to install the GitHub Copilot extension for Visual Studio Code. If you’d prefer to use GitHub Copilot with PyCharm instead, then skip ahead to learn how.

Install a Visual Studio Code Extension

Because Microsoft owns GitHub, it’s no surprise that their Visual Studio Code editor was the first tool to receive GitHub Copilot support. There are a few ways to install extensions in Visual Studio Code, but the quickest one is probably by bringing up the Quick Open panel using Ctrl+P or Cmd+P and then typing the following command:

Language: Text
ext install GitHub.copilot

When you confirm it by pressing Enter, it’ll install the extension and prompt you to reload the editor afterward.

Alternatively, you can find the Extensions icon in the Activity Bar located on the left-hand side of the window and try searching for the GitHub Copilot extension on the Visual Studio Marketplace:

GitHub Copilot Extension for Visual Studio Code
GitHub Copilot Extension for Visual Studio Code

You might also show the Extensions view in Visual Studio Code directly by using a corresponding keyboard shortcut.

After the installation is complete, Visual Studio Code will ask you to sign in to GitHub to give it access to your GitHub profile, which your new extension requires:

Authorize GitHub for VS Code
Authorize GitHub for VS Code

Visual Studio Code needs to know who you are to verify your GitHub Copilot subscription status. However, granting access to your GitHub profile will also allow the editor to read your private repositories. If you change your mind, then you can revoke this authorization at any time by going to your GitHub profile settings and finding GitHub for VS Code in the Authorized OAuth Apps.

To make working with GitHub Copilot in Visual Studio Code even more productive, here are the most common keyboard shortcuts worth remembering:

Action Windows / Linux macOS
Trigger inline suggestions Alt+\ Option+\
See the next suggestion Alt+] Option+]
See the previous suggestion Alt+[ Option+[
Accept a suggestion Tab Tab
Dismiss an inline suggestion Esc Esc
Show all suggestions in a new tab Ctrl+Enter Ctrl+Enter

If you run into issues getting the default shortcuts to work, then try defining your own key bindings in Visual Studio Code. This might be especially helpful if you’re working with a non-US keyboard layout.

Sometimes GitHub Copilot suggestions may get in your way. If that’s the case, then you can disable them globally or for a particular programming language by clicking on the extension’s icon in the bottom right corner of the editor’s window:

GitHub Copilot Icon in Visual Studio Code
GitHub Copilot Icon in Visual Studio Code

That’s it! You’re all set to start using the GitHub Copilot extension in Visual Studio Code. But if you’d prefer to use GitHub Copilot with PyCharm, read on to find out how.

Install a PyCharm Plugin

PyCharm is one of many popular integrated development environments provided by JetBrains and sharing a common GitHub Copilot plugin. You can install that plugin by opening the Settings in your IDE, and choosing Plugins from the list of options. Then, on the Marketplace tab, search for the GitHub Copilot plugin and click the Install button next to it:

GitHub Copilot Plugin for PyCharm
GitHub Copilot Plugin for PyCharm

After installing the plugin, you’ll be prompted to restart your IDE. When you do, you’ll have to log in to GitHub by choosing Tools from the PyCharm menu, then GitHub Copilot, and Login to GitHub:

GitHub Login Menu in PyCharm
GitHub Login Menu in PyCharm

That’ll generate a pseudo-random device code that you must copy and paste into the Device Activation page in your web browser, which you’ll be taken to after logging in to GitHub:

Device Activation on GitHub
Device Activation on GitHub

As with Visual Studio Code, you’ll need to authorize PyCharm’s plugin to verify your identity and the corresponding GitHub Copilot subscription status. However, PyCharm uses the GitHub API instead of an OAuth token, so the authorization process looks a little different:

Authorize GitHub for PyCharm
Authorize GitHub for PyCharm

Note that granting access to your GitHub profile will allow the plugin to retrieve your profile information, such as your email address, and to read your private repositories. If you change your mind, then you can revoke this authorization at any time by going to your GitHub profile settings and finding GitHub Copilot Plugin in the Authorized GitHub Apps.

To make working with GitHub Copilot in PyCharm even more productive, here are the most common keyboard shortcuts worth remembering:

Action Windows / Linux macOS
Trigger inline suggestions Alt+\ Option+\
See the next suggestion Alt+] Option+]
See the previous suggestion Alt+[ Option+[
Accept a suggestion Tab Tab
Dismiss an inline suggestion Esc Esc
Show all suggestions in a new tab Alt+Enter Alt+Enter

Sometimes GitHub Copilot automatic completions may get in your way. If that’s the case, then you can disable them globally or for a particular programming language by clicking on the plugin’s icon in the bottom right corner of the editor’s window:

GitHub Copilot Icon in PyCharm
GitHub Copilot Icon in PyCharm

That’s it! You’re all set to start using the GitHub Copilot plugin in PyCharm.

Hand Over the Reins to GitHub Copilot

Now it’s time to make sure GitHub Copilot is working as expected in either Visual Studio Code or PyCharm. To check if GitHub Copilot is working as expected in your Visual Studio Code editor, create a new text file, select Python as the underlying programming language, and start writing a sample function signature, such as hello():

GitHub Copilot in Visual Studio Code

As soon as you type the colon (:) at the end of the first line to introduce a new code block, GitHub Copilot fills in the suggested function body for you. Until you either accept it by hitting Tab or reject it with Esc, it’ll show up in gray font. The suggested code calls the print() function to display the Hello World text on the screen in this case. While that wasn’t spectacular, it confirms that GitHub Copilot is indeed working correctly.

Working with GitHub Copilot in PyCharm is virtually the same as in other code editors. To verify if the plugin installation succeeded, try a different example. Start writing a function signature whose name may indicate that you wish to add two numbers together, such as add(a, b):

GitHub Copilot in PyCharm

Sure enough, GitHub Copilot gives a very sensible suggestion, which returns the sum of a and b. Notice the difference between returning a value from the function and printing the result on the screen. Your clever virtual assistant can infer the intent from the function’s name and arguments.

In the rest of this tutorial, you’ll explore several hands-on use cases for GitHub Copilot in daily software engineering tasks. You’ll learn how to take your productivity to a whole new level by getting instant code suggestions tailored to your programming needs.

Synthesize Python Code From Natural Language

Because GitHub Copilot was trained on natural language as well as curated samples of different programming languages, it appears to understand both domains. Therefore, it’s perfectly possible to explain an abstract problem to GitHub Copilot using plain English or another natural language and expect it to generate the corresponding code in the desired programming language.

The underlying machine learning model is also capable of doing the opposite—that is, explaining a piece of code in a natural language or even translating one programming language into another. Imagine how helpful this could be for beginners and creative souls who have a vision of what they want to accomplish but haven’t yet mastered computer programming.

You’re about to see how this translation between human and computer languages looks in practice.

Use a Python Comment to Describe the Problem

Although influential figures in the programming world like Robert C. Martin consider code comments to be an anti-pattern, comments can sometimes be helpful in explaining why a certain piece of code looks the way it does. You typically write comments for your future self or your teammates working on the same codebase.

When you add GitHub Copilot into the mix, then it becomes yet another target audience who can read your code comments. Consider the following single-line comment in Python, which describes the classic Hello, World! program:

Language: Python
# Print "Hello, World!"

After typing that comment into your code editor, you’ll notice that GitHub Copilot doesn’t automatically pick it up. When you choose to communicate with it through comments, you must open the GitHub Copilot side panel or tab to see the suggestions. Alternatively, you may start typing a bit of code to have it auto-completed. Either way, writing the comment from above should present you with the following Python code:

Language: Python
print("Hello, World!")

That’s almost the same suggestion that you got when you verified the Visual Studio Code extension by writing the hello() function stub. However, this time, you get a slightly different output. GitHub Copilot understands that you wish to treat the quoted fragment of your comment as literal text rather than an instruction.

Apparently, that was too easy for GitHub Copilot. How about raising the bar by requesting a more specific output? For example, you may want to print Hello, World! backward in Spanish:

Language: Python
# Print "Hello, World!" backward in Spanish.

You’ll see new suggestions after refreshing the GitHub Copilot panel in your editor. Their number and quality may vary each time you trigger GitHub Copilot. The best one that you’ll ever get for this particular comment is this:

Language: Python
print("¡Hola, mundo!"[::-1])

Now, that’s impressive! GitHub Copilot nails it by generating not only correct but also Pythonic code that experienced Pythonistas would write themselves. The suggestions will get even more interesting when you include more comments.

Add More Comments to Increase the Problem Complexity

Using a single-line comment to describe a problem is fine, but you can only pack so much content in it. Fortunately, it’s possible to combine multiple consecutive comments into a logical and cohesive story that GitHub Copilot will treat as a whole. It’s best to put a complete sentence on each line without breaking it, but you can optionally include an explicit backslash (\) in the middle of a sentence to mark a line break:

Language: Python