A step by step guide
Recently, I have been diving into the world of python a bit more particularly webscraping, interaction with API etc. While I was looking to move from notebooks to code editors, a major problem I had was importing libraries. The solutions I found online were a little complex until I read a tutorial from the VS Code website and I thought to share that.
This write-up will be useful to you if you are trying to run python scripts in vscode or having issues importing python libraries. That been said, let get right into it!😀
Problem
Below is the error I encountered:
To follow along, you need to have vs code, python and python extension installed on your laptop.
Solution
First of all, when working on a project, you create a folder. This would help save all files associated with this project.
Open VS Code editor, navigate to folder destination and add folder.
Now that you have added your folder, create a virtual environment: A virtual environment helps isolate packages installation and reduce problems that may arise from conflicting package versions.
You do this by using the command palette(ctrl+shift+P), then type in and select ‘python:create environment’. A venv and conda will pop, then you select venv. You will be prompted to select a python interpreter after which the virtual environment will be created.
Next, you create your python file. A python file is created by adding a .py extension to the file name.
Next , I’m going to try to import the library so you can see the information it brings when I run the python file.
To import your library, create a new terminal by going to the command palette(ctrl+shift+p) and type ‘Python:create terminal’.
In your new terminal, type e.g “pip install pandas” or whatever library you want to install and press enter.
By the time we run our script again, it runs just fine.
And that’s done!! Now, you can go ahead and use the library.