How to run python in pycharm

Python IDE: PyCharm Tutorial for Beginners

The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. But if you are a beginner, don’t worry, it can help you too! Find out how to create Python projects using PyCharm and what basic features can help you write code more efficiently.

The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. The vast number of PyCharm features doesn’t make this IDE difficult to use–just the opposite. Many of the features help make Pycharm a great Python IDE for beginners.

If you are just starting to learn Python, you should try out different Python IDEs to see if you prefer working with PyCharm or with another editor.

In my previous article, I showed you how to install the PyCharm IDE on Windows and Ubuntu. In this article, you’ll find out how to create a program and run it using PyCharm. You’ll also get to know some of the features that help beginners write code.

Читайте также:  Css drop down menu css code

After the PyCharm IDE, we’ll move on to other popular Python IDEs and review them. Follow our blog or subscribe at the bottom to get the hang of other IDEs like Spyder, Jupyter, and more!

How to create and run Python scripts in PyCharm

To create a Python script right click on the project name node and choose from the pop-up menu New › Python File.

PyCharm

The New Python file window appears.Enter the name of the file in the Name field.

PyCharm

Click OK and you are ready to code in the PyCharm editor.

PyCharm

Copy this example code into the editor:

def hello(): print("Hello!") hello()

Save the file by clicking on the title tab,

PyCharm

then pressing Ctrl+S or by clicking File › Save All.

PyCharm

How to Run Python Code in PyCharm

We can run a project or a single file by selecting the project/file node and choosing from the menu Run › Run (the green Run button) or by using a keyboard combination Alt+Shift+F10.

PyCharm

Click the name of your script.

At the bottom of the editor the Output window with the result will be shown.

PyCharm

User Interface of the Editor

In almost all Python IDEs, the working area is divided into several parts that help make working with files and code more convenient. The screen below illustrates these different parts and what they are doing.

PyCharm

On the top is the main menu, which contains commands like open file, open project, create project or file, run, debug code, etc. Under the main menu is a toolbar with the same functionality, but it provides quick access to these functions. At the bottom is a status bar that displays messages about errors, warnings, and the status of a project. The central part of the screen consists of the editor where you read and write code. To the left, right, and bottom of the editor are tool windows. They allow for many different tasks, for example: see the node of the project and quickly open it, search in files, run options, change debugging settings, etc.

How to Customize the PyCharm Theme and Color Scheme

The PyCharm IDE allows you to customize many options. The first custom function is the theme of the IDE. You have a choice among different themes. To access the themes click on File › Settings.

PyCharm

The Settings window appears. From the left pane, expand the Editor and Color Scheme menus.

PyCharm

In the right pane select from the Scheme drop-down list one of the scheme names. For example it may be currently set to the Default scheme. Please note: the default color in PyCharm is Darcula and here, Default means your OS’s default theme.

PyCharm

Click the OK button.

PyCharm

If the Change PyCharm Theme window appears, click the Yes button. You will see a new theme like this below.

PyCharm

You can also change font colors in the scheme. You’ll find all the available options in the Settings window.

PyCharm Features with Shortcuts

Code Highlighting

The main feature that helps developers in writing code is highlighting. The editor highlights symbols and keywords in the code. It helps a developer to find these words in the script fast and it helps to notice errors. For example strings are green in color, integers are in blue, functions are in dark blue, etc. You can also change colors for groups of words in the settings of the IDE. Syntax highlighting improves the readability of the code.

PyCharm

If you click on the symbol or word, all occurences of this word or symbol will be highlighted.

PyCharm

PyCharm also helps you with errors. If you make a mistake in the code, this word will be indicated by red underlining. Hovering over this word with the mouse displays a tooltip with details.

PyCharm

Code Completion

Another very important feature, especially if you are just starting to learn Python, is code completion. While writing code you don’t always remember all the keywords in Python. If you start writing a word, the IDE gives you suggestions in a pop-up list, from which you can choose the appropriate word. It’s very convenient.

You can invoke code completion with the Ctrl+Space command.

If you write code without importing a required package, you’ll see this underlined in red. Select the word and press Alt+Enter or click the red bulb.

PyCharm

The IDE helps by suggesting that you import this package. Click on it and the line containing import will be inserted into the code. Now you can use the code with functions from the package math.

PyCharm

PyCharm also helps you remove unused imports if you remove code that depended on a package. If you see an orange bulb, expand it and choose the Optimize imports option.

PyCharm

You can do it also by pressing Ctrl+Alt+O or by choosing from the menu Code › Optimize imports.

Input Definition

The next helpful feature is the ability to see the definition of a function. If, for example, you type input, but forgot how many and which parameters this function takes, you can display the documentation about this function. Just select the word and choose from the menu View › Quick Definition.

PyCharm

You can also choose Quick Documentation.

PyCharm

Expression Type

Python doesn’t require declaration of data types for variables. However, during writing many lines of code, sometimes you need to find out what data type is assigned to a variable. In PyCharm it is easy. Select the variable and choose from the menu View › Expression type.

PyCharm

Of course you can also use the keyboard shortcut Ctrl+Alt+P. In a popup window you see the data type.

PyCharm

Code Folding

The next useful feature is hiding parts of code. If your code is long, it is very useful to hide sections of code to improve readability. You can select a block of code to hide and choose from the menu Code › Folding › Fold Selection.

PyCharm

The selected code will be folded.

PyCharm

If you click the plus symbol («+»), you expand the block of code.

PyCharm

Code Constructs with Surround Templates

PyCharm provides adding surrounding fragments of code with constructs based on the language of the source code like if. For example, you can write only the condition for an if statement and select it.

PyCharm

Then you can choose from the menu Code › Surround With or press Ctrl+Alt+T.

PyCharm

The list of statements appears.

PyCharm

If you choose if expr, your selected code will be inserted as the condition of an if expression.

PyCharm

Code Inspection

In PyCharm you can also easily navigate to the declaration of a variable, function, etc. Select the name of an object and press Ctrl+B. In the screen below we selected the a variable.

PyCharm

PyCharm allows for analysis of code by inspection. It detects compiling errors, unused code, memory leaks, and other problems. By default it analyses all open files and highlights all detected problems. Just select from the menu Code › Inspect Code.

PyCharm

The Specify Inspection Scope window appears, where you can choose to inspect all of a project or one file.

PyCharm

Click the OK button.

At the bottom of the IDE you see errors and warnings. In our script there are missing spaces around operators and whitespace before the ‘:’ character.

PyCharm

If there are not any errors or warnings, you see a message about it.

PyCharm

How to Install Packages in the PyCharm IDE

PyCharm is similar to other Python IDEs in that it allows you to install packages and modules. In PyCharm this is a very easy process. First choose from the menu File › Settings › Project: project name › Project Interpreter

You will see the Settings window:

PyCharm

Here is displayed a list of installed packages. To install a new package, click the plus icon, which is next to the names of the columns. The Available Packages window is displayed. Find the package and click the Install Package button at the bottom of this window.

PyCharm

You see the new package in the list.

PyCharm

Click the OK button.You are ready to use the package in your project! 🙂

Summary

This article is a short beginner overview of using the PyCharm IDE for growing your Python programming skills. I showed you how the PyCharm user interface works, how to run scripts, and what features can help you in writing code.

Please note, this editor contains many more features and tools, but I discussed only the most useful functions available in the Community version of the editor. If you haven’t used an IDE before, maybe PyCharm will turn out to be your favorite for writing Python scripts. Try it out and have fun!

Источник

Run Python Using a Run Configuration

In the previous step, we saw how PyCharm makes coding productive.

In this step, we are going to talk about Run Configurations and how PyCharm helps you to run your Python code.

Temporary Run Configurations

First off, let’s talk about temporary Run Configurations. If you haven’t run your pure Python script yet, PyCharm will give you a drop-down that says Add Configuration. This means that PyCharm doesn’t know how to run this script yet.

To run your script, you can right-click anywhere inside the editor and click Run script name. PyCharm will create a Run Configuration, run the script, and display the output inside the Run tool window. The Run tool window is useful to see your output, as well as more comprehensive error messages if the Python interpreter finds an error in your program.

The temporary Run Configuration created by PyCharm is now available in the top bar and can now be reused every time you want to run this script again with the Play button.

Run Configuration

Alternatively If you scroll down, you can see that we have an if __name__ == ‘__main__’ block, and it has a play icon in the gutter. This is another shortcut to create a temporary Run Configuration. When you click it, a context menu shows up, and the first option says Run .

keymap

Using the Play button will also create a temporary Run Configuration as well as displaying the output inside the Run tool window.

Configurations From Project Templates

Let’s look at explicitly creating a Run Configuration. The previous scenario applies for both PyCharm Community and PyCharm Professional, but this option is only available for PyCharm Professional.

In an empty Django project, you can see that the Run Configurations button already shows the project name and a Django logo. This is because PyCharm professional has already created a basic Run Configuration, special for Django.

Django Run Configuration

When you click the play button, PyCharm launches the Django project, including a web server, so you can click the link in the Run tool window and see your Django project.

Django Run Tool Window

PyCharm Professional also creates configurations for your Flask projects, FastAPI projects, and projects based on other frameworks.

Permanent Run Configurations

Many times in your development workflow you will need to configure different startup points for your application, including, for example, executing your script with different Python interpreters. This is when your permanent Run Configurations come in handy.

The easiest way to do it is by transforming a temporary Run Configuration into a permanent one by clicking the drop-down menu and selecting Save project name Configuration.

Save project configuration

You can also use pre-configured templates to create your Run Configurations by opening the Edit configurations dialog and clicking on the plus button. You can then choose the target you are creating Run Configurations for. You can then choose the Run Configuration targets.

Editing Run Configurations

Conclusion

We saw how to quickly run our Python scripts with temporary Run Configurations, and why sometimes it is handy to create permanent ones.

In the next step, we’ll talk about basic code refactoring in PyCharm.

Video

You can also check out the video for this step from our Getting Started series on YouTube:

Источник

Оцените статью