共用方式為


Quickstart: Clone a repository of Python code in Visual Studio

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Once you've installed Python support in Visual Studio, you can add the GitHub Extension for Visual Studio. The extension lets you easily clone a repository of Python code and create a project from it from within the IDE. You can always clone repositories on the command line as well, and then work with them in Visual Studio.

Install the GitHub Extension for Visual Studio

To work with GitHub repositories from within VS, you need to install the GitHub Extension for Visual Studio. To do so, run the Visual Studio installer, select Modify, and select the Individual components tab. Scroll down to the Code tools section, select GitHub extension for Visual Studio, and select Modify.

Selecting the GitHub extension in the VS installer

Work with GitHub in Visual Studio

  1. Launch Visual Studio.

  2. Select View > Team Explorer to open the Team Explorer window in which you can connect to GitHub or Azure Repos, or clone a repository. (If you don't see the Connect page shown below, select the plug icon on the top toolbar, which takes you to that page.)

    Team explorer window showing Azure Repos, GitHub, and cloning a repository

  3. Under Local Git Repositories, select the Clone command, then enter https://github.com/gregmalcolm/python_koans in the URL field.

  4. Enter a folder for the cloned files, and select the Clone button.

    Tip

    The folder you specify in Team Explorer is the exact folder to receive the cloned files. Unlike the git clone command, creating a clone in Team Explorer does not automatically create a subfolder with the name of the repository.

  5. When cloning is complete, the repository name appears in the Local Git Repositories list. Double-click that name to navigate to the repository dashboard in Team Explorer.

  6. Under Solutions, select New.

    Team explorer window, creating a new project from a clone

  7. In the New Project dialog that appears, navigate to the Python language (or search on "Python"), select From Existing Python Code.

  8. Specify a name for the project, set Location to the same folder as the repository, and select OK.

  9. In the wizard that appears, select Finish.

  10. Select View > Solution Explorer from the menu.

  11. In Solution Explorer, expand the python3 node, right-click contemplate_koans.py.

  12. Select Set as Startup File. This step tells Visual Studio which file it should use when running the project.

  13. Select Project > Koans Properties from the menu, select the General tab, and set Working Directory to "python3". This step is necessary because by default Visual Studio sets the working directory to the project root. Visual Studio doesn't set the location of the startup file in python3\contemplate_koans.py, which you can see in the project properties as well. The program code looks for a file koans.txt in the working folder, so without changing this value you see a runtime error.

    Setting the working directory for a Python project

  14. Press Ctrl+F5 or select Debug > Start without Debugging to run the program. If you see a FileNotFoundError for koans.txt, check the working directory setting as described in the previous step.

  15. When the program runs successfully, it displays an assertion error on line 17 of python3/koans/about_asserts.py. This is intentional: the program is designed to teach Python by having you correct all the intentional errors. (More details are found on Ruby Koans, which inspired Python Koans.)

    First output from the Python koans program

  16. Open python3/koans/about_asserts.py by navigating to it in Solution Explorer and double-clicking the file. By default, the line numbers don't appear in the editor. To change this, select Tools > Options, select Show all settings at the bottom of the dialog, then navigate to Text Editor > Python > General and select Line numbers:

    Turning on line number for Python files

  17. Correct the error by changing the False argument on line 17 to True. The line should read as follows:

    self.assertTrue(True) # This should be True
    
  18. Run the program again. If Visual Studio warns you about errors, respond with Yes to continue running the code. You'll see that the program passes through the first check and then stops on the next koan. Continue correcting the errors and run the program again as you want.

Important

In this Quickstart, you created a direct clone of the python_koans repository on GitHub. Such a repository is protected by its author from direct changes, so attempting to commit changes to the repository fails. In practice, developers instead fork such a repository to their own GitHub account, make changes there, and then create pull requests to submit those changes to the original repository. When you have your own fork, use its URL instead of the original repository URL used earlier.

Next steps

See also