Redaguoti

Bendrinti naudojant


Use Python folders in Visual Studio search paths

In a typical Python program, the PYTHONPATH environment variable (or IRONPYTHONPATH, and so on) provides the default search path for module files. The from <name> import... or import <name> statements instruct Python to search the specific locations for files that match the specified <name>. The locations are searched in the following order:

  1. Python built-in modules
  2. The folder that contains the currently running Python code
  3. The "module search path" as defined by the applicable environment variable (For more information, see The Module Search Path and Environment variables in the core Python documentation.)

Visual Studio ignores the search path environment variable, even when the variable is set for the entire system. The reason is because use of the variable raises questions that can't be easily answered, such as:

  • Do the referenced modules expect an installation of Python 2.7, Python 3.6, or other version?
  • Should files found in the search path environment variable override standard library modules?
  • Is any override behavior expected and addressed, or could the action be malicious?

To assist developers, Visual Studio provides a way to specify search paths directly in both Python environments and projects. Code that you run or debug in Visual Studio receives search paths from the PYTHONPATH environment variable and equivalent variable. By adding search paths, Visual Studio inspects the libraries in the specified locations and builds IntelliSense databases for the libraries as needed. (In Visual Studio 2017 version 15.5 and earlier, constructing the database can take some time depending on the number of libraries).

Prerequisites

  • Visual Studio. To install the product, follow the steps in Install Visual Studio.

  • Python folders that you want to add to your search paths.

Add Python folders to search paths

Follow these steps to add a Python folder to Visual Studio search paths:

  1. In Solution Explorer, expand your Python project node, right-click Search Paths, and select Add Folder to Search Path:

    Screenshot that shows how to access the Add Folder to Search Path command on Search Paths for a Python project in Solution Explorer.

  2. In the dialog, browse to the location of the folder that you want to add to the recognized search paths.

  3. Select the folder, and then choose Select folder.

After folders are added to the search paths, Visual Studio uses those paths for any environment associated with the project.

Note

If your environment is based on Python 3 and you attempt to add a search path to Python 2.7 modules, you might see errors.

Add zip and egg files to search paths

You can add files with a .zip or .egg extension to your search paths with the Add Zip Archive to Search Path option. As with folders, the contents of these files are scanned and made available to IntelliSense.