bs4 (Beautiful Soup) for Python3 not working in visual studio - python-3.x

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

Related

Visual Code gives error that it cannot find Python on Mac Pro

I'm running a Mac Pro 2021 model version 12.6. For some reason, my vscode will not run Python anymore when I tried using a program. I switched over to the cloud-based version for a while and now switched back to the application. It says that it could not find Python in my files. I believe both VSCode and my Mac are updated up to date. I installed python 3.11.0 for Mac, but no progress. I also tried running the update shell.command and the certificate commands as a part of the download in the vscode terminal, but it did not help. I also reinstalled the Python
v2022.16.1 Vscode extension and restarted the program. I still get the same error.
Explained above.
Have you tried Shift + Command + P type "Python: Select interpreter" yet?
Usually, all installed Python interpreters show up here, and you can also enter a path to a Python interpreter on your system.

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.

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.

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

How to use pip with Visual Studio Code

I have python 3.6 installed, I have a python extension installed on Visual Studio code but I still can't use pip on Visual Studio code. It says it is not a recognised command. Any help please?
Update: I tried installing pip manually but a file in python2.7 keeps stopping. What's bothersome is that I uninstalled python 2.7 ages ago and I've currently removed every folder with it but python-V still says I have python2.7.6 installed.
I'm on windows 10
I found some solution on this website:
https://pip.pypa.io/en/stable/installing/
If you use Visual Studio Code, you can type the following command:
C:\> py -m pip
If everything is alright, you should see the list of commands, which you can use. They are listed in your terminal, as per below.
I also would advise you to check/select the version of Python you are using:
VSCode: There is no Pip installer available in the selected environment
I am pretty sure your problems of VSCode not finding the correct version of Python will be resolved if you add your ( Python 3.6 installation ) location to the system path.

Resources