Files
freeCodeCamp/curriculum/challenges/english/blocks/lecture-python-installation/69fd6b21fcaa14b089c27182.md
T

4.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69fd6b21fcaa14b089c27182 How to Run Python Scripts 19 how-to-run-python-scripts

--description--

In the previous lesson, you learned how to install Python on your local device. In this lesson, you will learn how to run Python code locally.

Before you can start building your Python scripts locally, you will need to install a code editor or IDE. IDE stands for Integrated Development Environment and it has features including a code editor, testing tools, a terminal and more.

One popular code editor that many developers use is VS Code. Visit https://code.visualstudio.com/download to download VS Code which supports Mac, Windows and Linux environments. This is the editor that will be used in this lesson.

Other common choices for Python development include the following:

  • PyCharm
  • Spyder

Once you have downloaded your editor, you will need to open it. Once it is opened, you will need to open a folder. You can click on the File menu option in the upper left hand corner and click on Open Folder. From there, you can choose or create a folder for your project. For extra practice outside of the lesson, you might want a folder named python-projects.

Once you have your folder set up, you can open the explorer which is the left sidebar in VS Code. Click on the New File icon which looks like a piece of paper with a plus icon over it. Then create a file called main.py and press enter. The .py is a file extension signalling this is a Python file.

Open up your main.py file and type the following code:

print("Hello, world!")

To run this code, you have a few options. The first option is to click on the run button which looks like a play button located in the upper right hand corner. That will open a terminal and run your Python script there. You should see the text "Hello, world!".

Another option is to open a terminal and manually run the program using:

python main.py

You must run this command from the folder where main.py is saved. For example, if main.py is inside a folder called python-projects, first use the following in the terminal:

cd python-projects

Then run:

python main.py

In some environments, especially on macOS and Linux systems where Python 2 and Python 3 are both installed, you may need to use python3 instead of python. This is common when the python command either does not exist or points to an older version of Python.

python3 main.py

As you progress throughout the course, you should try the examples from the lessons locally and see the results. You should also try to come up with your own examples to test what you have learned from the lessons.

--questions--

--text--

Which of the following is NOT a commonly used editor or IDE for Python development?

--answers--

Clang


VS Code

--feedback--

Refer back to the beginning of the lesson for the correct answer.


Spyder

--feedback--

Refer back to the beginning of the lesson for the correct answer.


PyCharm

--feedback--

Refer back to the beginning of the lesson for the correct answer.

--video-solution--

1

--text--

What does IDE stand for?

--answers--

Internal Development Environment

--feedback--

Refer back to the beginning of the lesson for the correct answer.


Integrated Development Environment


Integrated Development Eval

--feedback--

Refer back to the beginning of the lesson for the correct answer.


Integrated Deno Environment

--feedback--

Refer back to the beginning of the lesson for the correct answer.

--video-solution--

2

--text--

Which of the following is the correct command to use if you want to run a Python script in the terminal?

--answers--

pip main.py

--feedback--

Refer to the end of the lesson for the answer.


pyrun main.py

--feedback--

Refer to the end of the lesson for the answer.


python main.py

piode main.py

--feedback--

Refer to the end of the lesson for the answer.

--video-solution--

3