PyCharm 2023.3 Help

Create and run your first project

Before you start

Ensure that the following prerequisites are met:

  • You are working with PyCharm Community 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.

To get started with PyCharm, let’s write a Python script.

Create a Python project

  1. If you’re on the Welcome screen, click New Project. If you’ve already got any project open, choose File | New Project from the main menu.

  2. Although you can create projects of various types in PyCharm, in this tutorial let's create a simple Pure Python project. This template will create an empty project.

    Create a new project
  3. Choose the project location. Click the Browse button in the Location field and specify the directory for your project.

  4. Python best practice is to create a dedicated environment for each project. In most cases, the default Project venv will do the job, and you won't need to configure anything.

    Still, you can switch to Custom environment to be able to use an existing environment, select other environment types, specify the environment location, and modify other options.

    Custom environment options for the new Python project

    For more information, refer to Configure a Python interpreter.

    For now, let's keep the default Project venv option.

  5. Click Create when you are ready.

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.

For more information, refer toOpen, reopen, and close projects.

Create a Python file

  1. In the Project tool window, select the project root (typically, it is the root node in the project tree), right-click it, and select File | New ....

    Create a Python file
  2. Select the option Python File from the context menu, and then type the new filename.

    Creating a new Python file

    PyCharm creates a new Python file and opens it for editing.

    New Python file

Edit Python code

Let's start editing the Python file you've just created.

  1. Start with declaring a class. Immediately as you start typing, PyCharm suggests how to complete your line:

    Create class completion

    Choose the keyword class and type the class name, Car.

  2. PyCharm informs you that there are errors in your file:

    Create class analysis

    Note that PyCharm analyzes your code on-the-fly, the results are immediately shown in the inspection indicator in the upper-right corner of the editor.

    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.

    Click it to preview the details in the Problems tool window. There's a missing colon, then an indentation is expected:

    View code problems
  3. Let's continue by creating the __init__ function for the class: when you start typing the function's name, PyCharm suggests pasting the entire code construct including the mandatory self parameter, the closing parenthesis, and the colon:

    Create class
  4. If you notice any inspection warnings as you're editing your code, click the bulb symbol to preview the list of possible fixes and recommended actions:

    Intention actions
  5. Let's copy and paste the entire code sample. Hover over the upper-right corner of the code block below, click the copy icon, and then paste the code into the PyCharm editor replacing the content of the Car.py file:

    class Car: def __init__(self, speed=0): self.speed = speed self.odometer = 0 self.time = 0 def accelerate(self): self.speed += 5 def brake(self): self.speed -= 5 def step(self): self.odometer += self.speed self.time += 1 def average_speed(self): return self.odometer / self.time if __name__ == '__main__': my_car = Car() print("I'm a car!") while True: action = input("What should I do? [A]ccelerate, [B]rake, " "show [O]dometer, or show average [S]peed?").upper() if action not in "ABOS" or len(action) != 1: print("I don't know how to do that") continue if action == 'A': my_car.accelerate() print("Accelerating...") elif action == 'B': my_car.brake() print("Braking...") elif action == 'O': print("The car has driven {} kilometers".format(my_car.odometer)) elif action == 'S': print("The car's average speed was {} kph".format(my_car.average_speed())) my_car.step()

At this point, you're ready to run your first Python application in PyCharm.

Run your application

Use either of the following ways to run your code:

  • Right-click the editor and select Run 'Car' from the context menu .

  • Press Ctrl+Shift+F10.

  • Since this Python script contains a main function, you can click in the gutter.

    You'll see the popup menu of the available commands. Choose Run 'Car':

    Run command

PyCharm executes your code in the Run tool window.

Run Tool window

Here you can enter the expected values and preview the script output.

Note that PyCharm has created a temporary run/debug configuration for the Car file.

Temporary run/debug configuration

The run/debug configuration defines the way PyCharm executes your code. You can save it to make it a permanent configuration or modify its parameters. For more information about running Python code, refer to Run/debug configurations.

Summary

Congratulations on completing your first script in PyCharm! Let's repeat what you've done with the help of PyCharm:

  • Created a project.

  • Created a file in the project.

  • Created the source code.

  • Ran this source code.

In the next step, learn how to debug your programs in PyCharm.

Last modified: 11 February 2024