简体   繁体   中英

How do I add a virtual environment to Visual Studio 2019?

I have upgraded from Visual Studio 2017 to 2019 (Community Edition). I also have installed the 'Python development' workload from the Visual Studio 2019 installer.

To add a virtual environment to a Python Project in Visual Studio, I right-click 'Python Environments' within the Python project (in the Solution Explorer View) and choose 'Add Environment...'. I opt for 'Virtual Environment', receiving the message:

"You will need to install a Python Interpreter before creating a virtual environment."

I have installed both the Anaconda 2019.03 and Python 3.7 interpreter, and I have access to both of them in Visual Studio.

The following steps worked for setting VS2019 with Anaconda3 already installed.

  • In the search bar, search for Add Environment, this brings up the VS Installer. Click the Python tab and you'll see a list of items that can be installed. There should only be a couple of them checked... it is unnecessary to install Python again.

  • When the Installer completes, open VS2019 and open Options from the Debug menu. If you aren't sure about where to show the "conda" executable path, the easiest way is to go to your "c:\user<usrname>\Anaconda3\Scripts\conda.exe" and select that. If you have already set it in your "Windows Advanced System settings" (yes, this is for Windows... there is probably a similar methodology for Linux) then check your environment variables for the path you've indicated in your environment for Conda.exe. (My choice was to forego the path and use only the IDE.)

  • Assuming you have Anaconda Navigator, and assuming it is open on Environments (left side), you can create a new environment called venv and it will create a Python environment for VS2019. You may need to activate it with a DOS prompt

conda activate venv

  • When you set it, VS2019 will spin for a bit while it configures you Python Environment in Visual Studio.

  • The environment choice in Anaconda3 is known as "base". Visual Studio, without Anaconda, sets up an environment called "venv". You should be able to open the Anaconda Navigator and locate your environment there. Because the venv is a separate environment, updating the base environment to use new tools should not affect VS2019. If nothing else, you can check requirements.txt to see which version is associated with the environment although it is easier to check in the Navigator. There are some advantages to using VS2019 if you have the Git configured. It will channel your work to your default git location.

  • The VS2019 configuration will take some time. The time it takes to setup Pycharm setup takes less time but you may find having the environment available in VS2019 worthwhile. I found it kept spinning until I restarted. When I restarted, there were a few additional steps.

  • Create a Python project - name and create it. In the Solution Explorer (right by default), you'll see a listing for Python Environment. If you created the venv environment in the above step, there will be a clean "venv" environment with only about a dozen python items to choose from. This is a clean environment and easier to control. It will exist side by side in the c:\user<usrname>\Anaconda3\envs folder with your "base" environment. You can tailor your requirements from there. If your project requires new packages, one of the default items is pip.

  • https://learn.microsoft.com/en-us/visualstudio/python/tutorial-working-with-python-in-visual-studio-step-05-installing-packages?view=vs-2022

  • From here there is a link to an interactive demo on adding packages: https://learn.microsoft.com/en-us/visualstudio/python/media/environments/install-python-packages-2022.gif?view=vs-2017&viewFallbackFrom=vs-2022

  • Code below is one way to move files into SQL Server

     import sys import csv import pandas as pd import pyodbc from sqlalchemy import create_engine import urllib import matplotlib.pyplot as plt dtype=np.int64 data1 = pd.read_csv (r'D:\Jsonfile.csv', sep=',', engine='python', encoding = "utf-16") df2 = pd.DataFrame(data1) params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;Trusted_Connection=yes;") conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params) engine = create_engine(conn_str) # create the table df2.to_sql("Jsonfile", engine)
  • To make this work on a clean environment you need to add sqlalchemy and a few more. When you type in the sqlalchemy under the PyPI, the top command will be: "Run command: pip install sqlalchemy" which is the correct item to click.

  • You may want to make note that the preferred method of working with Python is to use Visual Studio Code and the setup is well documented here

  • How can I set up a virtual environment for Python in Visual Studio Code?

Probably you have found some sort of solution by now.

However I got the virtual environments dialog working after repairing the Visual Studio 2019 installation.

You can repair a Visual Studio 2019 installation running the Visual Studio installer and choosing the repair command from the more button (as shown in the figure below).

Visual Studio 安装程序对话框

This is the breadcrumb that I followed:

Tools > Python > Python Environments > Add Environmemt

Add Environment... is at the top of the panel that opens when you get to > Python Environments

You can also use the 'chord' [<Ctrl> K , <Ctrl> '] to open the 'Python Environments' Panel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM