Featured Post

Hướng dẫn cài C++ với Visual Studio Code

Image
Điều kiện để cài Để cài đặt thành công, trước tiên bạn cần phải thiết lập môi trường phát triển cho C++. Cụ thể, trong hướng dẫn này yêu cầu: MinGW Visual Studio Code Visual Studio Code Extension Cài đặt MinGW Vào trang chủ của MinGW Vào trang Downloads Click vào icon windows để tải về Mở app vừa cài lên  Bấm Install, Next và đợi phần mềm tải về. Cài đặt xong, tick vào ô mingw32-gcc-g++-bin, chọn Mark for Installation Trên thanh menu bar, chọn Installation > Apply Changes    Sau khi cài đặt xong, vào đường dẫn mà bạn đã cài ứng dụng, theo mặc định là ở: C:\MinGW Vào thư mục bin trong đường dẫn trên  Copy path từ thanh address  Trên thanh search bar của Windows, gõ Variables , chọn Edit the system enviroment variables  Trong System Properties, chọn Environment Variables...  Trong Environment Variables, ở khung System Variables, chọn Path, xong bấm vào ô Edit  Ở cửa sổ Edit environment variables, bấm vào New và copy đường dẫn của MinGW mà mì...

Getting Started with Python 3 in Visual Studio Code

Prerequisquisites

To successfully complete this tutorial, you need to first setup your Python development environment. Specifically, this tutorial requires:
  • VS Code
  • VS Code Python Extension
  • Python 3

Install a Python interpreter

Which interpreter you use is dependent on your specific needs, but some guidance is provided below
In Windows, install Python from python.org. You can typically use Download Python button that appears first on the page to download the lastest version.
For additional information about using Python on Windows, see Using Python on Windows at Python.org

Verify the Python Installation

To verify that you’re installed Python successfully on your machine, run one of the following command (depending own your operating system):
In Windows, open a command prompt and run the following command:
 py -3 --version
If the installation was successful, the output windows should show the version of Python that you installed.

Install Visual Studio Code and the Python Extension

  1. If you have not already done so, install VS Code.
  2. Next, when you have finished the installation, open VS Cocde to install Python Extension.

  3. Click here go to Extension Marketplace.
    Click here go to Extension MarketplaceType go to search bar "Python" and install thatType go to search bar “Python” and install that

Start VS Code in a project (workspace) folder

Using a command prompt, create an empty folder called “hello”, navigate into it, and open VS Code (code) in that folder (.) by entering the following commands:
 mkdir hello
 cd hello
 code .
Alternately, you can run VS COde throught the operating system UI, then use File > Open Folder (In Menu Bar) to open the project folder

Select a Python interpreter

Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
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):

Select Python Environment
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don’t see the desired interpreter, she Configuring Python environments.

Create a Python Hello World source code file

From the File Explorer toolbar, select the New File button on the hello folder:

Open new file

Name the file hello.py, and it automatically opens in the editor:

Open file hello

By using the .py file extension, you tell VS Code to interoret this file as a Python program, so that is evaluates the contents with the Python extension and the selected interpreter.
Now that you have a code file in your Workspace, enter the following source code in hello.py:
 message = "Hello World"
 print(message)
When you start typing print, notice how IntelliSense presents auto-completion options.

IntelliSence

IntelliSense and auto-completions work for standard Python modules as well as other packages you’ve installed into the environment of the selected Python interpreter. It also provides completions for methods available on object types. For example, because the message variable contains a string, IntelliSense provides string methods when you type message. :

IntelliSense

Feel free to experiment with IntelliSense some more, but then revert you changes so you have only the message variable and the print call, and save the file (Ctrl+S ).
For full details on editing, formatting, and refactoring, see Editing code. The Python extension also has full support for Linting.

Run Hello World

It’s simple to run hello.py with Python. Just click the Run Python File in Terminale play button in the top-right side of the editor.

run python

The button opens a terminal panel in which your Python interpreter is automatically activated, then runs python hello.py:

run python

Install and use packages

Let’s now run an example that’s a little more interesting. In Python, package are how you obtain any number of useful code libraries, typically from PyPI. For this example, you use the flask packages to create minimal Flask application.

Return to the Explorer view (the top-most icon on the left side, which shows files), create new file called simple.py, and paste in the following source code:
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

Next, try running the file in command prompt.
python -m flask run

You should see the message, “‘flask’ is not recognized as an internal or external command, operable program or batch file.”. Such a message indicates that the required package isn’t available in your system.

To install the flask package, use the Command Palette to run Terminal: Create New Integrated Terminal ( Ctrl+Shift+` ). This command opens a command prompt for your selected interpreter.

A best practice among Python developers is to a void install packages into a global interpreter environment. You instead use a project-specific virtual environment that contains a copy of a global interpreter. Once you activate that environment, any packages you then install are isolated from other environments. Such isolation reduces many complications that can arise from conflicting package versions. To create a virtual environment and install the required packages, enter the following commands as appropriate for your operating system:

Note: For additional information about virtual environments, see Environments.
  1. Create and activate the virtual environment
    Note: When you create a new virtual environment, you should be prompted by VS Code to set it as the default for your workspace folder. If selected, the environment will automatically be activated when you open a new terminal.
    python -m venv .venv
    .venv\Script\activate
    
    setting environment
    If the activate command generates the message “Activate.ps1 is not digitally signed. You cannot run this script on the current system.”, then you need to temporarily change the PoswerShell execution policy to allow scripts to run:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
    
  2. Select your new environment by using the Python: Select Interpreter command from the Command Palette.
  3. Install the packages
    python -m pip install flask
  4. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:
    - On command Prompt:
    (.venv) C:\hello> set FLASK_APP=hello.py
    - And on PowerShell:
    (.venv) PS C:\hello> $env:FLASK_APP = "hello.py"
  5. Alternatively you can use python -m flask
     $env:FLASK_APP = "hello.py"
     python -m flask run
    
    Flask
  6. This launches a very simple builtin server, which is good enough for testing but probably not what you want to use in production. For deployment options see Deployment Options.
  7. Now head over to http://127.0.0.1:5000/, and you should see you hello world greeting
    Flask

Next step

You can configure VS Code to user any Python environment you have installed, including virtual. You can also use a separate environment for debugging. For full details, see Environments.
To learn more about the Python Language, follow any of the programming tutorials listed on python.org within the context of VS COde.
To learn to build web apps with the Flask framework, see coming soon my tutorial…

Comments

Popular posts from this blog

Hướng dẫn cài C++ với Visual Studio Code