Visual studio code как установить библиотеку python
Перейти к содержимому

Visual studio code как установить библиотеку python

  • автор:

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.

Once you have a version of Python installed, activate 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:

The Python extension then provides shortcuts to run Python code in the currently selected interpreter (Python: Select Interpreter in the Command Palette):

  • In the text editor: right-click anywhere in the editor and select Run Python File in Terminal. If invoked on a selection, only that selection is run.
  • In Explorer: right-click a Python file and select Run Python File in Terminal.

You can also use the Terminal: Create New Terminal command to create a terminal in which VS Code automatically activates the currently selected interpreter. See Environments below. The Python: Start REPL activates a terminal with the currently selected interpreter and then runs the Python REPL.

For a more specific walkthrough on running code, see the 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, and you can trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space ) . You can also hover over identifiers for more information about them.

Tip: Check out the IntelliCode extension for VS Code (preview). IntelliCode provides a set of AI-assisted capabilities for IntelliSense in Python, such as inferring the most relevant auto-completions based on the current code context.

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.

Copilot extension in the VS Code Marketplace

GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, 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! Set breakpoints, inspect data, and use the debug console as you run your program step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.

For Python-specific details, including setting up your launch.json configuration and remote debugging, see Debugging. General VS Code debugging information is found in the debugging document. The Django and Flask tutorials also demonstrate debugging in the context of those web apps, including debugging Django page 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 left side of the VS Code Status Bar:

Selected Python interpreter in the Status Bar

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 other than debugging. It is also activated when you run Python in a terminal.

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).

Installing packages

Packages are installed using the Terminal panel and commands like pip install <package_name> (Windows) and pip3 install <package_name> (macOS/Linux). VS Code installs that package into your project along with its dependencies. Examples are given in the Python tutorial as well as the Django and Flask tutorials.

Jupyter notebooks

If you open a Jupyter notebook file ( .ipynb ) in VS Code, you can use the Jupyter Notebook Editor to directly view, modify, and run code cells.

Jupyter notebook running in VS code in the Notebook Editor

You can also convert and open the notebook as a Python code file. The notebook’s cells are delimited in the Python file with #%% comments, and the Python extension shows Run Cell or Run All Cells 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

Opening a notebook as a Python file allows you to use all of VS Code’s debugging capabilities. You can then save the notebook file and open it again as a notebook in the Notebook Editor, Jupyter, or even upload it to a service like Azure Notebooks.

Using either method, Notebook Editor or a Python file, you can also connect to a remote Jupyter server for running the code. For more information, see Jupyter support.

Testing

The Python extension supports testing with unittest and pytest.

To run tests, you enable one of the frameworks in settings. Each framework also has specific settings, such as arguments that identify paths and patterns for test discovery.

Once discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests, including the ability to run individual test files and individual 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.

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.

How to install a new python module on VSCode?

I’m trying to install new python modules on my computer and I know how to install through the terminal, but I wish to know if there is a way to install a new module directly through VSCode (like it is possible on PyCharm)?

I already installed through the terminal, it isn’t a problem, but I want to install without be obligate to open the terminal when I’m working on VSCode.

TCrepalde's user avatar

3 Answers 3

You should open the terminal inside the VSCode and install the modules you want. something like��

VSCode Snapshot

if that’s not you meant, please let me know.

Mithilesh's user avatar

  1. First of all I would advise you to select the current Python version you have. It has been explained here:
  1. Next, you should check is the pip installed in your Python main directory or not, by checking how to do on this website:

in your terminal, like

You should have the list of commands and general options which can be used. One of them is install

On the Python library platform, you always have the command to be copied in order to the installation of package you want.

enter image description here

In your terminal, the initial command should look as:

to which you should append the command prepared on the Python library platform (by copying it and pasting).

That’s it. The package should be installed in your Python folder, what you will see in the terminal.

Visual Studio Code and Python Extension: For Beginners

As of May 2019, Microsoft released the Python Extension for Visual Studio Code. In this post, we are going to show you the beginner steps to configure the interpreter, execute and debug a sample project with newly released extension.

Install Visual Studio Code

You can install the appropriate VSC for your platform from here.

Install Python Extension

Once the VSC is installed, you should install the newly released extension. To do so, open the Extension windows and simply search for Python .

Open a Project and Write the Code

  • File > Open Folder.
  • File > New File
  • Write the code and save the changes

Configure and Select the Python Interpreter

The extension automatically looks for interpreters in a predefined set of paths, including:

  • Default install paths, e.g. /usr/local/bin
  • Virtualenvs in the project directory $
  • Virtualenvs located in the directory identified by python.venvPath

If your virtualenv is located in a custom directory, you should setit in python.venvPath option:

  • File > Preferences > Settings ,
  • Search python.venvPath
  • Fill the empty box below with the path to the custom directory

After changing the settings, it is usually needed to restart the VSC window.

  • Open the command palette (Crtl + Shift + P)
  • Search: reload window and select it

Note that you can set the settings customisation across user, workspace.

Installing Python package in Visual Studio Code

Python Visual Studio Code

This short post shows you how to install the Python package in Visual Studio Code, which is widely used in the programming environment. The prerequisite for our operation is the Python extension in Visual Studio code, which should be installed at the very beginning (Pic. 1).

Python extension in Visual Studio Code

Pic. 1 Python extension in Visual Studio Code.

You can do the installation from this link and get the newest version of the Python language.
Next, you can start programming with Python in Visual Studio Code without a problem. However, it seems, like you are somewhat restricted when you discover, that there are problems with installing the external Python libraries and importing them to Visual Studio Code. I hope, that this quick solution will help you manage these issues.
The biggest problem at the initial stage is the presumed lack of the Pip installation in our Visual Studio Code software. The Pip is the most popular tool for installing Python packages and includes a modern version of Python. It provides the essential core features for finding, downloading, and installing Python packages.
In Visual Studio Code, the Pip seems to be not recognized as an internal or external command (Pic. 2). Usually, we choose the quickest way of Python module installation, where after visiting the module we want to get, we simply make the copy of the top installation command and next, paste it to our Python dedicated software, i.e. PyCharm.

Pip not recognized in Visual Studio Code

Pic. 2 The “Pip not recognized” error in Visual Studio Code.

In the Visual Studio Code, we need to change our pip command a bit. The proper command in our terminal should be:

Once we hit enter, the whole list of commands should be populated as you can see below (Pic. 3).

Python Pip commands in Visual Studio Code

Pic. 3 A part of the list of commands for Pip in Visual Studio Code.

Now we are sure, that the Pip has been recognized by our Visual Studio Code. It means, that the Python library can be installed, which is great.
In order to make the installation correctly, we can’t simply make a copy of the main command available just below the library name (Pic. 2).
If we would do so, then our command would look like this:

and we would have to remove one “ pip ” preventing at least one space between the strings as you can see here.
I think, much easier is just type the py-m in our terminal path:

and then append the library command, which we want to install (Pic. 2)…

After hitting the “Enter” button, you can see the installation progress. Next, the tool will inform you the information about the target path where the library has been installed as well as the new pip version if available (Pic. 4).

Python library installation successful in Visual Studio Code

Pic. 4 The Python library installation progress in Visual Studio Code.

In order to make sure, that everything is alright, you can import the library to the file you are working on, by simply typing:

If something would be wrong, the Python console will underline your code with a wavy red line. Since everything has been installed properly, the library is to be recognized instantly by the Visual Studio Code (Pic. 5).

Visual Studio Code library installed

Pic. 5 Python library imported successfully to Visual Studio Code.

I highly recommend using Visual Studio Code, which appears to be the best coding application in the market. The biggest advantage is a broad opportunity for coding in various programming languages. This application is great and supersedes other programming applications dedicated to a specified language. As a result, a lot of memory is saved keeping our computers faster.

Добавить комментарий

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