|
| 1 | +# Programming Environment |
| 2 | + |
| 3 | +You can write your Python programs in a Notepad or any text file editor. However, I strongly discourage to do so. It is much better, to use some programming environment. There is plenty of such environments that you can use with Python, e.g. Spyder, PyCharm, Visual Studio, Visual Studio Code... |
| 4 | + |
| 5 | +## VS Code |
| 6 | + |
| 7 | +You you do not have any familiar editor, I recommend [Visual Studio Code](https://code.visualstudio.com/) (VS Code), that is free and works in any operating system (Windows, Linux, Mac). You can download it from <https://code.visualstudio.com/Download>. |
| 8 | + |
| 9 | +Once you have installed VS Code, you need to install the [Python extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python) from the Visual Studio Marketplace. For additional details on installing extensions, see [Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-marketplace). The Python extension is named Python and it's published by Microsoft. |
| 10 | + |
| 11 | +### Start VS Code in a project (workspace) folder |
| 12 | + |
| 13 | +Using a command prompt or terminal, create an empty folder called ‘`hello`’, navigate into it, and open VS Code (`code`) in that folder (.) by entering the following commands: |
| 14 | + |
| 15 | +``` |
| 16 | +mkdir hello |
| 17 | +cd hello |
| 18 | +code . |
| 19 | +``` |
| 20 | + |
| 21 | +By starting VS Code in a folder, that folder becomes your “workspace”. VS Code stores settings that are specific to that workspace in `.vscode/settings.json`, which are separate from user settings that are stored globally. |
| 22 | + |
| 23 | +Alternately, you can run VS Code through the operating system UI, then use **`File` > `Open Folder`** to open the project folder. |
| 24 | + |
| 25 | +### Select a Python interpreter |
| 26 | + |
| 27 | +Python is an interpreted language, and in order to run Python code and get Python help (called IntelliSense in the VS Code), you must tell VS Code which interpreter to use. |
| 28 | + |
| 29 | +From within VS Code, select a Python 3 interpreter by opening the **Command Palette** (`Ctrl+Shift+P`), start typing the **`Python: Select Interpreter`** command to search, then select the command. You can also use the **`Select Python Environment`** option on the Status Bar if available (it may already show a selected interpreter, too): |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +The command presents a list of available interpreters that VS Code can find automatically. If you don't see the desired interpreter, see VS Code manual on [Configuring Python environments](https://code.visualstudio.com/docs/python/environments). |
| 34 | + |
| 35 | +Selecting an interpreter sets which interpreter will be used by the Python extension for that workspace. |
| 36 | + |
| 37 | +### Create a Python Hello World source code file |
| 38 | + |
| 39 | +From the File Explorer toolbar, select the **New File** button on the `hello` folder: |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +Name the file `hello.py`, and it automatically opens in the editor: |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +By using the `.py` file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter. |
| 48 | + |
| 49 | +> **Note:** The File Explorer toolbar also allows you to create folders within your workspace to better organize your code. |
| 50 | +> You can use the **New folder** button to quickly create a folder. |
| 51 | +
|
| 52 | +Now that you have a code file in your Workspace, enter the following source code in `hello.py`: |
| 53 | + |
| 54 | +```python |
| 55 | +msg = "Hello World" |
| 56 | +print(msg) |
| 57 | +``` |
| 58 | + |
| 59 | +When you start typing print, notice how IntelliSense presents auto-completion options. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +### Run Hello World |
| 64 | + |
| 65 | +It's simple to run `hello.py` with Python. Just click the **Run Python File in Terminal** play button in the top-right side of the editor. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +The button opens a terminal panel in which your Python interpreter is automatically activated, then runs `python3 hello.py` (macOS/Linux) or `python hello.py` (Windows): |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +There are three other ways you can run Python code within VS Code: |
| 74 | + |
| 75 | +* Right-click anywhere in the editor window and select **`Run Python File in Terminal`** (which saves the file automatically): |
| 76 | + |
| 77 | +  |
| 78 | + |
| 79 | +* Select one or more lines, then press `Shift+Enter` or right-click and select **`Run Selection/Line in Python Terminal`**. This command is convenient for testing just a part of a file. |
| 80 | + |
| 81 | +* From the Command Palette (`Ctrl+Shift+P`), select the Python: **`Start REPL`** command to open a REPL terminal for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time. |
| 82 | + |
| 83 | + |
| 84 | +<hr/> |
| 85 | + |
| 86 | +Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license. |
| 87 | +Source: <https://code.visualstudio.com/docs/python/python-tutorial> |
0 commit comments