Creating and Running Your First Python Project
In this section:
- Before you start
- Creating a simple Python project in PyCharm
- Creating a Python file
- Editing source code
- Running application
- Run/debug configuration
Before you start
Make sure that the following prerequisites are met:
- You are working with PyCharm CE or Professional.
- You have installed Python itself. If you’re using macOS or Linux, your computer already has Python installed. You can get Python from python.org.
Creating a simple Python project in PyCharm
To get started with PyCharm, let’s write a Python script.
Do you remember the quadratic formula from math class? This formula is also known as the A, B, C formula, it’s used for solving a simple quadratic equation:
ax2 + bx + c = 0. As manually solving quadratic formulas gets boring quickly, let’s replace it with a script.
Note that PyCharm suggests several project templates for the development of the various types of applications (Django, Google AppEngine, etc.). When PyCharm creates a new project from a project template, it produces the corresponding directory structure and specific files.
Let’s start our project: if you’re on the Welcome screen, click Create New Project. If you’ve already got a project open, choose .
PyCharm suggests several project templates for the creation of the various types of applications (Django, Google AppEngine, etc.). When PyCharm creates a new project from a project template, it produces the corresponding directory structure and specific files, and any needed run configurations or settings.
Choosing which interpreter to use for a project is an important decision. Python is a script language, which means that your code is converted to machine code by a Python interpreter.
You can have multiple versions of Python installed on your computer, and you need to choose the one you’d like for this project. Refer to the section Configuring Python SDK for details. Note that you can always change your mind later and specify another interpreter for your project.
When you’re using external libraries (from PyPI or elsewhere) you need to manage the versions of these as well. The Pythonic solution for this are virtualenvs (sometimes abbreviated to venv). Virtualenvs help you keep the dependencies for your different projects separate. Although in this project we’re not using any dependencies, it’s considered best practice to create a virtualenv for every new project you make, in case you’d like to add dependencies in the future.
Python best practice is to create a virtualenv for each project. To do that, click , choose Create VirtualEnv, and then specify the virtual environment’s name and location, and the base interpreter. For more information, see Creating Virtual Environment .
If you’ve already got a project open, after clicking Create PyCharm will ask you whether to open a new project in the current window or in a new one. Choose Open in current window - this will close the current project, but you'll be able to reopen it later. Refer to the page Opening Multiple Projects for details.
Creating a Python file
Select project root in the Project tool window, and press Alt+Insert.
Editing source code
Let us first have a look at the Python file we've just generated.
Immediately as you start typing, you understand that PyCharm, like a pair-programmer, looks over your shoulder and suggests how to complete your line. For example, you want to create a Python class. As you just start typing the keyword, a suggestion list appears:
Choose the keyword
class and type the class name (
Solver). PyCharm immediately informs you about the missing colon, then expected indentation:
Note the error stripes in the right gutter. Hover your mouse pointer over an error stripe, and PyCharm shows a balloon with the detailed explanation. Since PyCharm analyses your code on-the-fly, the results are immediately shown in the inspection indicator on top of the right gutter.
This inspection indication works like a traffic light: when it is green, everything is OK, and you can go on with your code; a yellow light means some minor problems that however will not affect compilation; but when the light is red, it means that you have some serious errors.
Let's continue creating the function
demo: when you just type the opening brace, PyCharm creates the entire code construct (mandatory parameter
self, closing brace and colon), and provides proper indentation:
Note as you type, that unused symbols are greyed out:
As soon as you calculate a discriminant, they are rendered as usual. Next, pay attention to the unresolved reference
math. PyCharm underlines it with the red curvy line, and shows the red bulb.
Let's make a brief excursion into PyCharm's notion of intention actions and quick fixes. When you write your code, it is sometimes advisable to modify code constructs - in this case PyCharm shows a yellow light bulb. However, if PyCharm encounters an error, it shows the red light bulb.
In either case, to see what does PyCharm suggest you to do, press Alt+Enter - this will display the suggestion list, which in our case contains several possible solutions:
Let's choose importing the
math library. Import statement is added to the
Solver.py file. Next, calculate roots of the quadratic equation, and print them out, and finally, let's call the function
demo of the class
import math class Solver: def demo(self): a = int(input("a ")) b = int(input("b ")) c = int(input("c ")) d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) print(root1, root2) Solver().demo()
Next, right-click the editor, and on the context menu choose to run the script (Ctrl+Shift+F10). A console appears in the Run tool window.
Let’s try out our code by calculating the roots of
3x2 + 5x - 4. So enter
a = 3,
b = 5, and
c = -4, and you should see a result:
When we ran the script just now, PyCharm created a temporary run/debug configuration for us. Let’s first save this configuration: go to the run configuration dropdown on the top-right of the editor, and choose Save configuration.
Afterwards, choose Edit Configurations to have a look at what is happening here.
Note that there are different configuration types for unit testing, and the various frameworks (like Django in PyCharm Professional).
If you’d like to change how your program is executed by PyCharm, this is where you can configure various settings like: command-line parameters, work directory, and more. See Run/Debug configurations for more details.
If you’d like to start the script using this Run configuration, use the button next to the dropdown.
Congratulations on completing your first script in PyCharm! Learn how to debug your programs in PyCharm.