How do I add a virtual environment to Visual Studio 2019? - python-3.x

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

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

Related

Visual Studio Code: highlighted imports in python

I use Visual Studio Code both on my work Windows laptop, and on my Ubuntu PC at home.
There's a functionality that I can see from my Windows vscode that I'd like to have on my Ubuntu one as well, but can't find whether it's a setting, extension, or something else.
The functionality is this: python imports are highlighted in green, and if they are not used, they are dimmed.
Windows screenshot, how I want it to work:
Ubuntu screenshot, how it currently is:
Does anyone know where I should look? I'm using the same colour theme on both, Monokai.
Thanks!
You will get the linting warnings by pip installing flake8.
You also need to add this setting to your settings.json file:
"python.linting.flake8Enabled": true
I fixed it by installing the extension Settings Sync and syncing settings and extensions of my two installations.
If it was an extension indeed, it probably was fixed by removing the Python Extension Pack and installing Python for VSCode.

I don't know how delete python2 in mac?

I used visual studio in mac air and I install python3.8.5 but already mac has python2
So I can't use python3.8 in visual studio..
I don't know how delete python2 in mac?
plz give me some tips..
You can change the default shell in use selection a new one, you can choose default shell in execution when using visual studio code:
You can customize your shell execution selecting the Select default shell, after you can select your current path to python3.
To discover where is your default python3 installation you can run the command:
which python3
#/usr/local/bin/python3
After that you can configure the default python3 path in your visual studio instance.
just select the terminal and override the terminal.integrated.shell.osx: environment variable.

Visual Studio Code with Python for Windows AND Anaconda

I'm trying to use Visual Studio Code with both Anaconda and Python for Windows.
I have both installed, and I have the visual studio code extensions for both.
But no matter what I do now the Conda Base gets activated -
(base) PS C:\Users
even though the Python for Windows is the selected version
or if I try to activate an environment I get a mix of both, the venv with python for windows and the conda base activated.
(venv_iocparser) (base) PS C:\Users\
The settings.json file in this folder only has python for windows in the 'python.pythonPath'; with no mention of conda, but still activates the conda base.
Does anyone know a good article that walks through setting VSCode up with both? With python for windows as the default?
Thank you!
Had a similar issue. Its most likely since you installed VS Code with Anaconda, for some reason or another it just assumes thats what env you want when starting it.
To get out of it just run $ conda deactivate and then activate whatever env you want after that. I fixed it by changing the PATH for the python to only point at the non-conda versions though. I also had uninstalled it all and reinstalled it all at one point though.

Getting an error while creating a briefcase on BeeWare

The WIX environment variable does not point to an install of the WiX Toolset.
Current value: WindowsPath('.')
While creating briefcase in BeeWare, you should have admin access because it downloads and install packages.
This issue is coming because briefcase is unable to download Wix Toolset and unable to point out Wix environment variable.
You can manually download Wix Toolset from here https://wixtoolset.org/releases/
install it and check your environment variables in system variables you can find Wix.
Now try to create briefcase after installation.
Close the terminal you're working on.
Install WIX toolkit form https://wixtoolset.org/
Install via GUI installer.
Make sure it has been added to your system variable path. (refer picture below)
Run briefcase create.
Hope this helps :)

bs4 (Beautiful Soup) for Python3 not working in visual studio

I am new to Python and I already get an error with the libraries, am running python 3 on visual studio community edition 2017. I already installed anaconda3 64bit with the installation process but the issue is when I call for the bs4 library (BeautifulSoup) it's returns an error telling me that the module does not exist as shown in the picture:
How can I install it correctly?
you need do add it to the virtual environment of your project
Tools -> Python -> Python Environments
It will open a sidebar to the right
Click on the "overview" select "packages" then search for "BeautifulSoup4" and click "install"
now it's in your project
You may also need to set the Python Environment on your solution.
Right-click the Python Environments node for a project in Solution Explorer and select Add/Remove Python Environments.
https://learn.microsoft.com/en-us/visualstudio/python/managing-python-environments-in-visual-studio?view=vs-2017

Resources