Visual Studio Python Pip

Posted on  by 



The Coding Pack for Python helps you quickly set up a Python coding environment with Visual Studio Code. The standalone installer helps you install a Python interpreter, Visual Studio Code, extensions that provide support for Python in Visual Studio Code, and a number of common and useful Python packages.

Pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed.

  1. Build Python wheels with pip. The Build Python wheels with pip task uses the pip wheel command to compile wheels. This may be passed either the source directory to build, or the names and versions of packages that have already been published on the Python Package Index. It will ensure that pip, wheel, setuptools and cython are installed before.
  2. Coding Pack for Python. The Coding Pack for Python helps you quickly set up a Python coding environment with Visual Studio Code. The standalone installer helps you install a Python interpreter, Visual Studio Code, extensions that provide support for Python in Visual Studio Code, and a number of common and useful Python packages.

Getting started

With the Coding Pack for Python, it's easy to get started developing with Python and VS Code.

  1. Download and run the Coding Pack for Python installer.

    Note: The installer only supports Windows 10 64-bit. This download is 200MB, and up to 100MB will be downloaded while you are installing.

  2. Once the installer launches, review and accept the License Agreement. Then select Install.

  3. After installation completes, select Next.

    Note: If you select Cancel before installation completes, you will need to manually remove and uninstall any components that have already been installed.

  4. Launch Visual Studio Code and start coding!

Note: If there are any issues installing components, you can use the steps discussed in Manual installation

What's installed by the Coding Pack for Python

The Coding Pack for Python installs the key components you need to use Visual Studio Code for Python development. Specifically, it installs:

  • Visual Studio Code
  • Visual Studio Code extensions:
    • Python
    • Pylance
    • Live Share
    • Gather
  • The Python runtime (CPython 3.8.5)
  • Useful Python packages
    • jupyter
    • numpy
    • sklearn
    • pandas
    • Matplotlib

Along with the tools and packages necessary for Python development, the Coding Pack also configures common user settings and PowerShell. This includes Python extension settings, such as the default interpreter and language server, as well as execution policies to allow for virtual environment activation in the terminal.

Note: If there was an existing version of Visual Studio Code installed on your machine, your settings.json will not be overwritten and you'll need to configure Python settings yourself.

Manual installation

If you have any problems during installation, the following manual steps can be used to complete your installation.

Visual Studio Code and the Python extension

  1. If there was an issue installing VS Code, you can install it from here.

  2. Once VS Code is installed, you can install the Python extension for VS Code from the Visual Studio Marketplace. For additional details about installing extensions, see Extension Marketplace. The Python extension is named Python and is published by Microsoft.

Python interpreter

  1. If there was an issue installing the Python interpreter, you can install Python 3.8 from the Microsoft Store. Along with the Python extension, you need to install a Python interpreter for development with Python. There are other options for installing the Python interpreter, such as directly from Python.org, and which interpreter you use is dependent on your specific needs. If you use the Python.org version, just make sure to uncheck the 'Install launcher for all users' box if you don't have admin access.

    Note: If you use the Microsoft Store installation option, be aware that some packages might not work well with this package; however, the packages listed below have been tested and work fine.

For additional information about using Python on Windows, see the VS Code Python documentation and Using Python on Windows at Python.org.

  1. Verify you Python installation by opening a cmd prompt and running the following code python --version. If the installation was successful, the output window shows the version of Python that you just installed.

Additional VS Code extensions

Pylance language server extension

Pylance is an extension that works alongside Python in Visual Studio Code to provide performant language support. Under the hood, Pylance is powered by Pyright, Microsoft's static type checking tool. Using Pyright, Pylance has the ability to supercharge your Python IntelliSense experience with rich type information, helping you write better code faster.

  1. Install the Pylance extension from the Visual Studio marketplace.
  2. Open a Python (.py) file and the Pylance extension will activate.
  3. Select Yes when prompted to make Pylance the default language server. This will update your preferences, which you can also do manually by adding 'python.languageServer': 'Pylance' to your settings.json file using the text editor.

Gather extension

The Gather extension adds the experimental Gather feature to the Python extension. With one button, you'll be able to select any notebook or Interactive Window cell and have Gather find and then copy all of the dependent code that was used to generate that cell's result into a new notebook or script.

  1. Install the Gather extension from Visual Studio marketplace.

Live Share extension

Visual Studio Live Share enables you to collaboratively edit and debug with others in real time, regardless what programming languages you're using or app types you're building. It allows you to instantly share your current project, and then as needed, share debugging sessions, terminal instances, localhost web apps, voice calls, and more! For additional details, see the documentation.

  1. Download and install the Visual Studio Live Share extension from the Visual Studio marketplace.
  2. Follow the guidance in the documentation about How-to: Collaborate using Visual Studio Code

Common Python packages

If you need to manually install the Python packages that would otherwise have been installed by the Coding Pack, you can do so using the following Python commands.

Be aware that these commands will install the packages into the global environment for your interpreter, because that's where the Coding Pack would have installed them. That said, a good option to consider is adding the packages to a virtual environment. For information about virtual environments, see the topic Using Python environments in VS Code.

Note: If you have problems running the Python commands above, you might need to make sure that the Python interpreter is on your PATH environment variable.

Settings and configuration

To help you get started quickly, the Coding Pack for Python sets a few key settings. If you need to configure them manually, you can use the following guidance.

Set default interpreter

  1. Open VS Code
  2. Within VS Code, open the Command Palette (ctrl+shift+p)
  3. Select Python: Select Interpreter
  4. Select the interpreter that you installed or that was installed by the Coding Pack

Set language server to pylance

  1. Install the Pylance extension and set it as the default language server as described in the section above.

Enable running scripts in PowerShell

  1. Open VS Code
  2. If a terminal is not already opened, select Terminal > New Terminal from the main toolbar
  3. Once the terminal has opened, enter the following command: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Uninstalling the Coding Pack for Python

If you need to uninstall (or repair) your Coding Pack for Python installation, you can use the following steps.

  1. Rerun the standalone installer.

  2. At the UI prompt, select Uninstall.

  3. Once you select uninstall, the following tasks will be performed:

    1. The folder with the Python interpreter and associated Python packages will be removed, including any user installed packages.
    2. The folder will be removed from the PATH environment variable.
    3. Any settings modified by the installation process will be reset.

Note: The uninstall process will not remove Visual Studio Code. At the end of the uninstall process, you can click the provided link to open 'Apps & features' to uninstall Visual Studio Code. If you decide to repair your installation, be aware that any other Python packages you might have installed will be removed as part of the repair process.

This tutorial walks you through enabling Windows Subsystem for Linux (WSL) and running Visual Studio Code in WSL using the Remote - WSL extension.

Prerequisites

You need Visual Studio Code installed.

Install the extension

The Remote - WSL extension enables you to run Visual Studio Code within the Windows Subsystem for Linux (WSL).

Prerequisite check

With the Remote - WSL extension installed, you will see a new Status bar item at the far left.

The Remote Status bar item can quickly show you in which context VS Code is running (local or remote) and clicking on the item will bring up the Remote - WSL commands.

Enable WSL

Windows Subsystem for Linux (WSL) is an optional feature on Windows 10. You can enable it through the Windows Features dialog or PowerShell.

Windows Features dialog

In the Windows search bar, type 'features' to bring up the Turn Windows Features on and off dialog. Scroll down and check Windows Subsystem for Linux.

Select OK and you will be prompted to restart Windows.

PowerShell

If you prefer, you can open PowerShell as an Administrator and type:

After the command runs, you will be prompted to restart Windows.

Check WSL

After restarting Windows, you can check that you have WSL enabled by opening a Command Prompt and typing 'wsl'.

WSL is enabled, but you haven't installed a Linux distribution yet.

Install a Linux distro

You install Linux distributions for WSL from the Microsoft Store. You can use the store app, or search for a Linux distro in the Windows search bar. Choose the Linux distribution you want to install (for example Ubuntu) and follow the prompts.

Select Install.

And when done, select Launch to get started. This will open a Linux terminal and complete the installation. You'll need to create a user ID and password since you are setting up a full Linux instance. You are now running Linux on Windows.

Python development

If you don't have Python already installed, run the following commands to install Python3 and pip, the package manager for Python, into your Linux installation.

And to verify, run:

Start with the canonical 'Hello World' app. Create a new folder called 'helloWorld' and then add a Python file that will print a message when run:

In a remote Linux environment (this WSL distro is technically another machine without UI, that just happens to be running locally on your computer), your development tools and experiences are pretty limited. You can run Vim in the terminal to edit your file, or you can edit the sources on the Windows side through the wsl$ mount:

The problem with this model is that the Python runtime, pip, or any conda packages for that matter, are not installed on Windows.

Visual Studio Python Update Pip

Remember, Python is installed in the Linux distro, which means if you're editing Python files on the Windows side, you can't run or debug them unless you install the same Python development stack on Windows. And that defeats the purpose of having an isolated Linux instance set up with all your Python tools and runtimes!

Visual

Run in WSL

In the WSL terminal, make sure you are in the helloWorld folder, and type in 'code .' to launch Visual Studio Code. The '.' argument tells VS Code to open the current folder.

Note: If this command does not work, you may need to restart your terminal, or you may not have added VS Code to your path when it was installed.

The first thing you'll see is a message about 'Installing VS Code Server' (the c7d83e57… number is the version of the VS Code Server that matches the client-side tools you just installed). VS Code is installing a small server on the Linux side that the desktop VS Code will then talk to. That server will then install and host extensions in WSL, so that they run in the context of the tools and frameworks installed in WSL. In other words, your language extensions will run against the tools and frameworks installed in WSL, not against what is installed on the Windows side, as it should for the proper development experience.

The next thing that happens is VS Code will start and open the helloWorld folder. You may see a quick notification telling you that VS Code is connecting to WSL, and you may be prompted to allow access to the Node.js-based server.

Now, when you hover over hello.py, you get the proper Linux path.

Integrated Terminal

Run Terminal > New Terminal (⌃` (Windows, Linux Ctrl+`)) to open a new terminal instance.

You'll start a new instance of the bash shell in WSL, again from VS Code running on Windows.

Visual Studio Code Python Tutorial

Tip: In the lower left corner of the Status Bar, you can see that you're connected to your WSL: Ubuntu instance.

Edit and debug

Installing the Python extension (and additional tools)

Click on hello.py to open it for editing. You will be prompted with an extension recommendation, in this case to install the Microsoft Python extension, which will give you rich editing and debugging experiences. Go ahead and select Install and reload if prompted.

To prove that the extension is installed in WSL, open the Extensions view again (⇧⌘X (Windows, Linux Ctrl+Shift+X)). You will see a section titled WSL: Ubuntu – Installed, and you can see any extensions that are installed on the WSL side.

Upon reload, you'll also get prompted telling you that the pylint linter is not installed. Linters are used to show errors and warnings in source code. Go ahead and select Install.

Now, when you edit your code, you get rich colorization and completions.

And when you save your file (⌘S (Windows, Linux Ctrl+S)), you'll get linting errors and warnings on the file.

Visual Studio Python Pip Path

Debugging

With your tools set up, let's take this one step further. Set a breakpoint on line 1 of hello.py by clicking in the gutter to the left of the line number or by putting the cursor on the line and pressing F9.

Now, press F5 to run your application. You will be asked how to run the application, and since this is a simple file, just choose Python File.

The app will start, and you'll hit the breakpoint. You can inspect variables, create watches, and navigate the call stack.

Press F10 to step and you'll see the output of the print statement in the debug console.

You get the full development experience of Visual Studio Code, using the Linux instance installed in WSL.

If you want to open another folder in WSL, open the File menu and choose Open Folder. You'll get a minimal file and folder navigator for the Linux file system, not the Windows file system.

If you want to switch back to the Windows, select the Show Local option and you'll get the standard Windows File Open dialog.

Ending your WSL connection

You can end your session in WSL and go back to running VS Code locally with File > Close Remote Connection.

Congratulations!

Congratulations, you've successfully completed this tutorial!

Next, check out the other Remote Development extensions.

Or get them all by installing the Remote Development Extension Pack.





Coments are closed