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
-
If you have not already done so, install VS Code.
-
Next, when you have finished the installation, open VS Cocde to install Python Extension.
![Click here go to Extension Marketplace.](https://i.ibb.co/1nmT0Nx/1.png)
Click here go to Extension Marketplace
Type 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](https://i.ibb.co/RHLvD3R/3.png)
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:
Name the file
hello.py, and it automatically opens in the editor:
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.
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.
:
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.
The button opens a terminal panel in which your Python interpreter is automatically activated, then runs
python hello.py
:
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.
-
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](https://i.ibb.co/Zhw0rvT/11.png)
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
-
Select your new environment by using the Python: Select Interpreter command from the Command Palette.
-
Install the packages
python -m pip install flask
-
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"
-
Alternatively you can use python -m flask
$env:FLASK_APP = "hello.py"
python -m flask run
![Flask](https://i.ibb.co/tcmZxWT/13.png)
-
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.
-
Now head over to http://127.0.0.1:5000/, and you should see you hello world greeting
![Flask](https://i.ibb.co/ncJyHhP/12.png)
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
Post a Comment