Visual studio python как подключить библиотеку

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

Visual studio python как подключить библиотеку

Step 5: Install packages in your Python environment

The Python developer community has produced thousands of useful packages that you can incorporate into your own projects. Visual Studio provides a UI to manage packages in your Python environments.

Select the View > Other Windows > Python Environments menu command. The Python Environments window opens as a peer to Solution Explorer and shows the different environments available to you. The list shows both environments that you installed using the Visual Studio installer and environments you installed separately. That includes global, virtual, and conda environments. The environment in bold is the default environment that’s used for new projects. For more information about working with environments, see How to create and manage Python environments in Visual Studio environments.

Python Environments window-2022

. moniker range=»>=vs-2022″ . moniker-end

Python Environments window-2019

. moniker range=»<=vs-2019″ . moniker-end

[!NOTE] You can also use the Ctrl+K, Ctrl+` keyboard shortcut to open the Python Environments window from the Solution Explorer window. If the shortcut doesn’t work and you can’t find the Python Environments window in the menu, it’s possible that you haven’t installed the Python workload. See How to install Python support in Visual Studio on Windows for guidance about how to install Python.

With a Python project open, you can open the Python Environments window from Solution Explorer. Right-click Python Environments and select View All Python Environments.

Python Environments-2022

. moniker range=»vs-2022″ . moniker-end

Python Environments-2019

. moniker range=»<=vs-2019″ . moniker-end

Now, create a new project with File > New > Project, selecting the Python Application template.

In the code file that appears, paste the following code, which creates a cosine wave like the previous tutorial steps, only this time plotted graphically. You can also use the project you previously created and replace the code.

Unresolved package import-2022

In the editor window, hover over the numpy and matplotlib import statements. You’ll notice that they aren’t resolved. To resolve the import statements, install the packages to the default global environment. . moniker range=»>=vs-2022″ . moniker-end

Unresolved package import

. moniker range=»<=vs-2019″ . moniker-end

When you look at the editor window, notice that when you hover over the numpy and matplotlib import statements that they aren’t resolved. The reason is the packages haven’t been installed to the default global environment.

For example, select Open interactive window and an Interactive window for that specific environment appears in Visual Studio.

The Packages tab in the Python Environments window lists all packages that are currently installed in the environment.

Install packages using the Python Environments window

From the Python Environments window, select the default environment for new Python projects and choose the Packages tab. You’ll then see a list of packages that are currently installed in the environment.

Install matplotlib by entering its name into the search field and then selecting the Run command: pip install matplotlib option. Running the command will install matplotlib , and any packages it depends on (in this case that includes numpy ).

Choose the Packages tab.

Consent to elevation if prompted to do so.

After the package is installed, it appears in the Python Environments window. The X to the right of the package uninstalls it.

. moniker range=»vs-2022″ Installing matplotlib in the environment-2022 in Packages tab . moniker-end . moniker range=»<=vs-2019″ Installing matplotlib in the environment-2019 in Packages tab . moniker-end

Consent to elevation if prompted to do so.

After the package installs, it appears in the Python Environments window. The X to the right of the package uninstalls it.

Installing matplotlib in the environment-2022

. moniker range=»vs-2022″ . moniker-end

Installing matplotlib in the environment-2019

. moniker range=»<=vs-2019″ . moniker-end

[!NOTE] A small progress bar might appear underneath the environment to indicate that Visual Studio is building its IntelliSense database for the newly-installed package. The IntelliSense tab also shows more detailed information. Be aware that until that database is complete, IntelliSense features like auto-completion and syntax checking won’t be active in the editor for that package.

Visual Studio 2017 version 15.6 and later uses a different and faster method for working with IntelliSense, and displays a message to that effect on the IntelliSense tab.

Run the program

Now that matplotlib is installed, run the program with (F5) or without the debugger (Ctrl+F5) to see the output:

Installing a Python Library in Visual Studio Code — Windows

In this quick blogpost, I will share the steps that you can follow in order to install a Python library using pip through either the Terminal or a Jupyter Notebook in Visual Studio Code (VSCode) on a Windows computer.

Pre-requisites

In order to complete the steps of this blogpost, you need to install the following in your windows computer:

  • Visual Studio Code : you can find the steps to install it here.
  • Python Extension for Visual Studio Code : you can find the steps to install it here.
  • Python Interpreter : you can find the steps to install it here.

Installing a Python Library Using the Terminal in VSCode

1) Accessing Visual Studio Code Terminal

  • Open VSCode application

  • Go to the Terminal menu and select New Terminal .

  • A new terminal (PowerShell based) window is opened.

2) Importing a Python Library

  • Run the following command to validate that pip is installed in your computer.

  • Let us say that you want to install Pandas Python library.
  • Run the following command

  • Pandas library is now ready to be imported by any python application. You can repeat this process for any Python library.

Installing a Python Library Using a Jupyter Notebook in VSCode

1) Creating a Jupyter Notebook in VSCode

  • Create a Jupyter Notebook following the steps of My First Jupyter Notebook on Visual Studio Code (Python kernel)

2) Importing a Python Library

  • Run the following command to validate that pip is installed in your computer.

  • Let us say that you want to install Pandas Python library.
  • Run the following command.

Install Python and libraries with Visual Studio

This tutorial explains how to install Python and libraries with Visual Studio. I will also show how to install libraries in the command prompt (CMD). Python is a very powerful language for machine learning as it has many libraries that can be used to help you develop great models for prediction.

Visual Studio Installer

Use Visual Studio Installer to install Python with Visual Studio. Modify your Visual Studio Version, check Python development and click on the Modify button. Python will be installed to “C:\Program Files (x86)\Microsoft Visual Studio\Shared”.

Visual Studio Installer

Add to path environment variable

You need to add the path to the Python folder and to the Scripts folder in environment variables, if you want to be able to use Python and Pip (install libraries) from anywhere in the Command Prompt. It is also useful to add paths to directories as environment variables if other programs references these programs in code.

Search for environment variables and click on the Edit environment variables result. Add the Python directory (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64) and the Scripts directory (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Scripts) as new rows in the Path variable.

Edit environment variables

You will need to browse to the Python folder in the Command Prompt to be able to execute python scripts if you don’t have added paths to environment variables.

Install libraries

You will need some libraries to be able to work with machine learning. You can install libraries with Pip from the command prompt or in Visual Studio. Some useful libraries is joblib, numpy, pandas, matplotlib, pytesseract, scipy, statsmodels, scikit-learn, tensorflow and Keras. You need to know that names of libraries is case sensitive. To use pytesseract you will also need to install Tesseract OCR and unpack poppler, you will also need to add the path to poppler and Tesseract OCR as environment variables.

Open the Command Prompt (CMD) as an administrator to install libraries with Pip. You can install specific versions of a library and uninstall libraries with Pip.

You can also install, uninstall and upgrade Python libraries in Visual Studio. This is done inside Python Environments. Select Packages (PyPI) in the dropdown (Overview).

Python Environment in Visual Studio

Test if libraries is installed

Create a new Python project in Visual Studio and add an Empty Python File to the project. Right click the new file and set it as Startup File. Add the following contents and run the script.

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

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