Как установить питон в visual studio code
Перейти к содержимому

Как установить питон в visual studio code

  • автор:

Начало работы с Python в Visual Studio Code

В этом эпизоде Роберт присоединяется к Тайреку Уайту, который показывает, как приступить к разработке Python с помощью Visual Studio Code. Он описывает, что необходимо установить и как настроить среду, а также советы по совместному использованию для отладки кода Python.

  • Начало работы: https://aka.ms/vst/pythonvscode
  • Следуйте нашему блогу: https://aka.ms/pythonblog
  • Следите за нами в Twitter: @pythonvscode
  • Демонстрационный код

В этом эпизоде Роберт присоединяется к Тайреку Уайту, который показывает, как приступить к разработке Python с помощью Visual Studio Code. Он описывает, что необходимо установить и как настроить среду, а также советы по совместному использованию для отладки кода Python.

  • Начало работы: https://aka.ms/vst/pythonvscode
  • Следуйте нашему блогу: https://aka.ms/pythonblog
  • Следите за нами в Twitter: @pythonvscode
  • Демонстрационный код

Как установить питон в visual studio code

Одной из сред разработки, которая позволяет работать с Python, является Visual Studio. Преимуществом данной IDE по сравнению, скажем, с PyCharm, следует отметить прежде всего то, что в ее бесплатной редакции Visual Studio Community бесплатно доступны ряд функций и возможностей, которые в том же PyCharm доступны только в платной версии Professional Edition. Например, это веб-разработка, в том числе с помощью различных фреймворков. В то же время средства ля разработки на Python в Visual Studo доступны пока только в версии для Windows.

Итак, загрузим установочный файл Visual Studio Community по ссылке https://visualstudio.microsoft.com/ru/vs/community/. После запуска установочного файла выберем среди устанавливаемых опций пункт Разработка на Python :

Установка Python в Visual Studio

После установки Visual Studio запустим ее и в окне программы выберем Create a new project :

Создание проекта для Python в Visual Studio

Далее в окне создания нового проекта выберем шаблон Python Application :

Первый проект Python в Visual Studio

На следующем окне укажем название и путь к проекту. Например, в моем случае проект будет называться «HelloApp»:

Первый проект Python в Visual Studio

Нажмем на кнопку Create, и Visual Studio создаст новый проект:

Первый проект на Python в Visual Studio

Справа в окне Solution Explorer (Обозреватель решений) можно увидеть структуру проекта. По умолчанию здесь мы можем увидеть следующие элементы:

  • Python Environments : здесь можно увидеть все используемые среды, в частности, здесь можно версию Python, которая используется.
  • References : в этот узел помещаются все внешние зависимости, которые используются текущим проектом
  • Search Paths : этот узел позволяет указать пути поиска для модулей Python
  • HelloApp.py : собственно файл Python с исходным кодом

По умолчанию в Visual Studio уже открыт файл HelloApp.py, но он пока пуст. Добавим в него следующую строку:

print("Hello Python from Visual Studio!")

И затем в панели инструментов нажмем на зеленую стрелочку для запуска:

Запуск скрипта Python в Visual Studio

В результате запуска отобразится консоль, которая выведет нужную строку:

Шаг 2. Написание и запуск кода

Хотя управление файлами проекта осуществляется в обозревателе решений, с содержимым файлов, например файлов исходного кода, вы обычно работаете в окне редактора. Редактор распознает тип редактируемого файла с учетом контекста. Он также определяет язык программирования (благодаря расширению файла) и обеспечивает характерные для этого языка возможности, например раскраску синтаксических конструкций и автоматическое завершение с помощью IntelliSense.

  1. При создании проекта «Python Application» в редакторе Visual Studio откроется пустой файл по умолчанию с именем PythonApplication1.py.
  2. В редакторе начните вводить print(«Hello, Visual Studio») . По мере ввода функция IntelliSense в Visual Studio покажет варианты автозавершения. Выделенный параметр в раскрывающемся списке — это вариант завершения по умолчанию, который применяется при нажатии клавиши TAB. Завершения очень удобны при использовании длинных инструкций или идентификаторов. IntelliSense auto-completion popup
  3. Функция IntelliSense отображает различные сведения на основе используемого оператора, вызываемой функции и т. д. Если в функции print ввести ( после print для указания вызова функции, будет показана полная информация об использовании этой функции. Во всплывающем окне IntelliSense также полужирным шрифтом выделяется текущий аргумент (в этом случае это value): IntelliSense auto-completion popup for a function
  4. Допишите оператор, чтобы он соответствовал следующему коду:

print("Hello, Visual Studio") 

IntelliSense syntax coloring and error highlighting

Обратите внимание на раскраску синтаксических конструкций, которая позволяет отличить оператор print от аргумента «Hello Visual Studio» . Можно ненадолго удалить последний символ » в строке. Обратите внимание, что код, содержащий синтаксические ошибки, подчеркивается красной линией в Visual Studio. Снова введите символ » , чтобы исправить ошибку.

Совет Visual Studio предоставляет полный контроль над внешним видом и поведением среды разработки, что позволяет настроить ее, как вам нравится. Выберите пункт меню Сервис>Параметры и изучите параметры на вкладках Среда и Текстовый редактор. По умолчанию вы видите ограниченный набор параметров. Чтобы просмотреть все параметры для каждого языка программирования, щелкните Показать все параметры внизу диалогового окна.

  • Чтобы выполнить весь написанный код, нажмите клавиши CTRL+F5 или выберите команду меню Отладка>Запуск без отладки. Если в коде все еще есть ошибки, в Visual Studio будут выведены предупреждения.
  • Во время выполнения программы в окне консоли будут отображаться результаты. Это напоминает запуск интерпретатора Python с файлом PythonApplication1.py из командной строки. Чтобы закрыть это окно и вернуться в редактор Visual Studio, нажмите любую клавишу. Output for the first run of the program
  • Помимо инструкций и функций, IntelliSense выводит варианты для завершения операторов Python import и from . Эти варианты позволяют легко узнать, какие модули доступны в вашей среде и какие члены доступны в определенном модуле. В редакторе удалите строку print и начните вводить import . Список модулей появляется после ввода пробела: IntellSense showing available modules for an import statement
  • Завершите строку, введя или выбрав sys .
  • В следующей строке введите from , чтобы снова вывести список модулей: IntellSense showing available modules for a from statement
  • Выберите или введите math , а затем пробел и import , чтобы отобразился список элементов модуля: IntellSense showing module members
  • Наконец импортируйте элементы cos и radians . Обратите внимание, как для каждого из них будут выводиться варианты автозавершения. По завершении код должен выглядеть следующим образом:

    import sys from math import cos, radians 

    Совет Принцип функции завершения заключается в следующем — по мере ввода появляются подстроки со словами, в которых совпадают части или буквы в начале слова, и даже отображаются пропущенные символы. Дополнительные сведения см. в руководстве по редактированию кода.

    for i in range(360): print(cos(radians(i))) 

    Python in Visual Studio Code

    Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code’s power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.

    This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.

    Install Python and the Python extension

    The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace.

    Note: To help get you started with Python development, you can use the Python profile template that includes useful extensions, settings, and Python code snippets.

    Once you have a version of Python installed, select it using the Python: Select Interpreter command. If VS Code doesn’t automatically locate the interpreter you’re looking for, refer to Environments — Manually specify an interpreter.

    You can configure the Python extension through settings. Learn more in the Python Settings reference.

    Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.

    Run Python code

    To experience Python, create a file (using the File Explorer) named hello.py and paste in the following code:

    print("Hello World") 

    The Python extension then provides shortcuts to run Python code using the currently selected interpreter (Python: Select Interpreter in the Command Palette). To run the active Python file, click the Run Python File in Terminal play button in the top-right side of the editor.

    Using the run python file in terminal button

    You can also run individual lines or a selection of code with the Python: Run Selection/Line in Python Terminal command ( Shift+Enter ). If there isn’t a selection, the line with your cursor will be run in the Python Terminal. An identical Run Selection/Line in Python Terminal command is available on the context menu for a selection in the editor. The same terminal will be used every time you run a selection or a line in the terminal/REPL, until that terminal is closed. The same terminal is also used for Run Python File in Terminal. If that terminal is still running the REPL, you should exit the REPL ( exit() ) or switch to a different terminal before running a Python file.

    The Python extension automatically removes indents based on the first non-empty line of the selection, shifting all other lines left as needed.

    The command opens the Python Terminal if necessary; you can also open the interactive REPL environment directly using the Python: Start REPL command that activates a terminal with the currently selected interpreter and then runs the Python REPL.

    For a more specific walkthrough and other ways of running code, see the run code tutorial.

    Autocomplete and IntelliSense

    The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.

    IntelliSense quickly shows methods, class members, and documentation as you type. You can also trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space ) . Hovering over identifiers will show more information about them.

    Enhance completions with AI

    GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.

    GitHub Copilot extension in the VS Code Marketplace

    GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++.

    You can learn more about how to get started with Copilot in the Copilot documentation.

    Linting

    Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.

    The Python extension can apply a number of different linters including Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. See Linting.

    Debugging

    No more print statement debugging! VS Code comes with great debugging support for Python via the Python Debugger extension, allowing you to set breakpoints, inspect variables, and use the debug console for an in-depth look at how your program is executing step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.

    For more specific information on debugging in Python, such as configuring your launch.json settings and implementing remote debugging, see Debugging. General VS Code debugging information is found in the debugging document.

    Additionally, the Django and Flask tutorials provide examples of how to implement debugging in the context of web applications, including debugging Django templates.

    Environments

    The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments.

    The current environment is shown on the right side of the VS Code Status Bar:

    Status Bar showing a selected interpreter

    The Status Bar also indicates if no interpreter is selected:

    Status bar showing no selected Python interpreter

    The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature. It is also activated when you run or debug Python in a terminal, or when you create a new terminal with the Terminal: Create New Terminal command.

    To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.

    Python: Select Interpreter command

    VS Code prompts you with a list of detected environments as well as any you’ve added manually to your user settings (see Configuring Python environments).

    Jupyter notebooks

    To enable Python support for Jupyter notebook files ( .ipynb ) in VS Code, you can install the Jupyter extension. The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.

    Jupyter notebook running in VS code in the Notebook Editor

    You can also convert and open the notebook as a Python code file through the Jupyter: Export to Python Script command. The notebook’s cells are delimited in the Python file with #%% comments, and the Jupyter extension shows Run Cell or Run Below CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:

    Jupyter notebook running in VS Code and the Python interactive window

    You can also connect to a remote Jupyter server to run your notebooks. For more information, see Jupyter support.

    Testing

    The Python extension supports testing with Python’s built-in unittest framework and pytest.

    In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.

    Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods

    Configuration

    The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Testing. The complete list is found in the Settings reference.

    Python profile template

    Profiles let you quickly switch your extensions, settings, and UI layout depending on your current project or task. To help you get started with Python development, you can use the Python profile template, which is a curated profile with useful extensions, settings, and snippets. You can use the profile template as is or use it as a starting point to customize further for you own workflows.

    You select a profile template through the Profiles > Create Profile. dropdown:

    Create Profile dropdown with profile templates

    Once you select a profile template, you can review the settings and extensions, and remove individual items if you don’t want to include them in your new Profile. After creating the new profile based on the template, changes made to settings, extensions, or UI are persisted in your profile.

    Other popular Python extensions

    The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.

    1. Open the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ).
    2. Filter the extension list by typing ‘python’.

    The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

    Next steps

    • Python Hello World tutorial — Get started with Python in VS Code.
    • Editing Python — Learn about auto-completion, formatting, and refactoring for Python.
    • Basic Editing — Learn about the powerful VS Code editor.
    • Code Navigation — Move quickly through your source code.
    • Django tutorial
    • Flask tutorial
  • Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *