<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python入門手冊 [toc] 開始創建一個最簡單的Python應用程序"Hello world"。 > **Note**: 該手冊應用Python 3; 如果用的是Python 2, 需要改寫相應的代碼; ## 準備工作 要成功完成這個手冊的內容,須做如下準備: 1. 下載安裝VS CODE,在linux終端輸入code啟動,code .在當前目錄下啟動VS Code 1. 安裝[Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python). 1. 安裝Python3 (該手冊例程基于Python3). Options include: - linux已內置Python,可在終端運行Python或python3查看版本 - An installation through [Homebrew](https://brew.sh/) on macOS using `brew install python3` (the system install of Python on macOS is not supported). - python下載1 [python.org](https://www.python.org/downloads/). - python下載2 [Anaconda](https://www.anaconda.com/download/) (用于科學數據). 1. (Windows) 確認環境變量PATH包含了Python解釋器路徑,可以在命令行終端運行path查看,如果沒有包含,需修改環境變量PATH,添加Python解釋器的路徑; ## 創建文件夾和源文件 新建文件夾"hello", 切換至該路徑,打開VS Code (`code`) in that folder (`.`): ``` mkdir hello cd hello code . ``` 菜單,文件 -> 新建文件hello.py,用`.py` 作為文件擴展名, VS Code將自動將其識別為Python文件。下一步在該文件輸入如下內容: ```python msg = "Hello World" print(msg) ``` ## 選擇一個Python解釋器 Python is an interpreted language, and in order to run Python code you must tell VS Code which interpreter to use. From within VS Code, select a Python 3 interpreter using the **Python: Select Interpreter** command on the **Command Palette** (`kb(workbench.action.showCommands)`), or by using the **Select Python Environment** option on the Status Bar if available (it may already show a selected interpreter, too): ![No interpreter selected](images/environments/no-interpreter-selected-statusbar.png) The command presents a list of available interpreters that VS Code can find automatically. If you don't see the desired interpreter, see [Configuring Python environments](/docs/python/environments.md). Selecting an interpreter sets the `python.pythonPath` value in your workspace settings to the path of the interpreter. To see the setting, select **File** > **Preferences** > **Settings**, then select the **Workspace Settings** tab. > **Note**: If you select an interpreter without a workspace folder open, VS Code sets `python.pythonPath` in your user settings instead, which sets the default interpreter for VS Code in general. ## Run Hello World It's simple to run `hello.py` with Python. Right-click in the editor and select **Run Python File in Terminal** (which saves the file automatically): ![Run Python File in Terminal command in the Python editor](images/tutorial/run-python-file-in-terminal.png) The command opens a terminal panel in which your Python interpreter is automatically activated, then runs `python3 hello.py` (Mac/Linux) or `python hello.py`: ![Program output in a Python terminal](images/tutorial/output-in-terminal.png) There are two other ways you can run Python within VS Code: - Select one or more lines, then press `kbstyle(Ctrl+Enter)` or right-click and select **Run Selection/Line in Python Terminal**. This command is very convenient for testing just a part of a file. - Use the **Python: Start REPL** command to opens 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. ## 配置和運行debugger 開始調試Hello World程序: 1. 設置在文件`hello.py`之 `print`設置斷點,在相應行左側單擊出現紅點設置斷點; 1. 點擊[調試圖標]()打開調試窗口; 1. 在調試工具欄選擇設置圖標(或在菜單欄,Debug -> Open configurations) 1. 打開有效調試器的菜單,顯示Python和Python Experimental,選擇Python,**Python extersion** 將新建一個文件×launch.json*,該文件包含一系列配置項(在配置下拉菜單中顯示)。這些配置項在*Debugging*有詳細的解釋,這里選擇"Python:Current File",該配置項用于選擇了Python解釋器的當前編輯文件。 **注意:** VS Code用JSON文件保存各種配置信息,*launch.json*是一個標準文件名用于保存調試配置項。 1. 為了在程序調試時程序自動停止于第一行,在*launch.json* -> 配置項*Python:Current File*,添加鍵值"stopOnEntry": true,如下 ```json { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "stopOnEntry": true }, ``` 保存 `launch.json`, 切換至編輯器文件`hello.py` ,點擊Debug工具欄的綠色箭頭或按F5鍵運行 調試器。因為`stopOnEntry`設置為true, 調試器停止于`hello.py` 的第一行,該行高亮黃色,行左側顯示黃色箭頭,如果你在此查看**Local** 變量, you see that only automatic dunder variables are defined: 在窗口頂部會出現一個調試工具欄,用于運行、單步、重啟和停止程序,狀態條改變顏色指示調試模式, **Python Debug Console**會出現在右下的窗口,顯示執行的命令和輸出。 選擇調試工具欄的綠色按鈕繼續運行程序,調試器停止于斷點處,斷點處定義的變量 `msg` 將會出現在 **Local**窗口, 可以在右下側窗口 **Debug Console**操作該變量 (在VS Code右下側選擇 "Debug Console" 或select it from the **...** menu if you don't see it): 選擇綠色箭頭繼續運行,在**Python Debug Console**顯示"Hello World" 。至此程序運行結束,自動退出調試模式。 調試時記住之前的調試項設置`stopOnEntry` ,根據需要設置。 ### Troubleshooting If for some reason VS Code doesn't generate `launch.json` for you, create a file by that name within the folder named `.vscode` folder (creating it if you need to), then paste the following contents into `launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}" } ] } ``` If you see the error below, it means that you attempted to start to debugger when `launch.json` is selected in the editor rather than `hello.py`: ```bash // Use IntelliSense to learn about possible attributes. ^ SyntaxError: invalid syntax ``` Select `hello.py` and try again. Alternately, create a debug configuration specifically for the `hello.py` file by adding the following lines in `launch.json` within the `configuration` array. Then select this configuration in the debugger drop-down and start the debugger again. ```json { "name": "Python: hello.py", "type": "python", "request": "launch", "program": "hello.py" }, ``` ## Install and use packages Let's now run an example that's a little more interesting. In Python, packages are how you obtain any number of useful code libraries, typically from [PyPi](https://pypi.org/). For this example you use the matplotlib and numpy packages to create a graphical plot as commonly done with data science. Return to **Explorer**, create a new file called `standardplot.py`, and paste in the following source code: ```python import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range plt.plot(x, np.sin(x)) # Plot the sine of each x point plt.show() # Display the plot ``` Try running the file in the debugger as described in the last section, without any breakpoints. The message "ModuleNotFoundError: No module named 'matplotlib'" indicates that matplotlib and numpy are not installed in the current interpreter's environment, which is expected (unless you're using an Anaconda installation). To install those packages, switch to the **Python Debug Console** that is already be open. (The Python Debug Console already has the selected interpreter's environment activated. You can also use **Python: Create Terminal** command from the Command Palette.) Enter `pip3 install matplotlib` (macOS/Linux) or `pip install matplotlib` (Windows) to install the matplotlib package (and its dependencies, such as numpy) into that environment. > **Note**: On Linux, you may need to install **tkinter** by running `sudo apt-get install python3-tk`. Also note that if you don't want to install matplotlib and its dependencies globally then use a [virtual environment](https://docs.python.org/3/tutorial/venv.html). Rerun the program now and after a few moments a plot window appears with the output: ![matplotlib output](images/tutorial/plot-output.png) ## Next steps You can configure VS Code to use any Python environment you have installed, including virtual and conda environments. You can also use a separate environment for debugging. For full details, see [Environments](/docs/python/environments.md). There is then much more to explore with Python in Visual Studio Code: - [Python environments](/docs/python/environments.md) - Control which Python interpreter is used for editing and debugging. - [Editing code](/docs/python/editing.md) - Learn about autocomplete, IntelliSense, formatting, and refactoring for Python. - [Linting](/docs/python/linting.md) - Enable, configure, and apply a variety of Python linters. - [Debugging](/docs/python/debugging.md) - Learn to debug Python both locally and remotely. - [Unit testing](/docs/python/unit-testing.md) - Configure unit test environments and discover, run, and debug tests. - [Settings reference](/docs/python/settings-reference.md) - Explore the full range of Python-related settings in VS Code.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看