Visual Studio Code is one of the most efficient code compilers/interpreters. It is very promising because of the vast and widely available go-to extensions that help programmers.
This article is an elaborative detail about how we can run Python code, install Python libraries, and create a virtual environment in Visual Studio Code.
Write and Run Python Code in VS Code
To run Python in VS Code, just create a new .py file and write any Python Code you want.
Install Libraries in VS Code
If you are writing your .py script and you’re stuck somewhere seeing this red line in VS Code.
And when you are trying to run the program, you see the terminal with the following error,
This is because you are importing the module which is not installed.
There are 2 ways we can install the libraries in VS Code:
1. Using the pip command
Very straightforward, just install what packages you need.
Open Terminal in VS Code. The shortcut is to press Ctrl+Shift+`
And install the specific module you want to through pip. Most of the time the command is
pip install module-name
And hit ENTER.
Sit back and relax, and let the pip do its job of installation.
And now you can see that the red line is disappeared.
Also, the program is working just fine.
This means every time you want to install a specific library, just open the terminal, type in the command terminal, and hit Enter. The libraries are getting installed here globally, which means inside your system, you can run the code by importing it from any directory.
2. Using the virtual environment
This is one of the best and most practiced ways. Here the libraries you are installing will not be installed actually in your main system.
What is the virtual environment?
According to docs, a virtual environment is a Python environment such that the Python interpreter, libraries, and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.
In a very sober way, you actually are installing the libraries within those folders and within the environment. If you are trying to run the same code from any other .py file from any other directory, you will see that the library isn’t installed.
It is actually installed only for that environment, only for that directory where you are creating the virtual environment.
How to create a Virtual Environment?
Open the terminal in VS Code. The shortcut is to type in Ctrl+Shift+`.
Copy the folder path i.e. the path where you want to create a virtual environment. Note that, this should be the same folder where your .py file is located.
Type in the terminal.
python -m venv folder-path\venv
Don’t forget to add the \venv after the folder your specified.
Now open the terminal again by either clicking on New Terminal or pressing Ctrl+Shift+`
And now you can see you are in a virtual environment.
And now, here, type in the commands to install the library.
And hurray, the library is installed. You are good to go.
This is how we can install the library in VS code in two ways.
NOTE:
Not every time, in Python for the installation, the module name will be the same as the name we used to import. eg. Opencv – you have to type ‘pip install OpenCV-python’. In this case, just type in install ‘module’ in the search box of your web browser and go to the first official website of Python i.e. https://pypi.org/, where the module details are specified. Copy that and paste it in your cmd.
Also Read:
- Google Unveils Veo 2 and Imagen 3: A New Era of AI-Generated Content
- Imagination to Reality, Unlocking the Future: Genesis Physics Engine for 4D Simulation
- Simple Code to compare Speed of Python, Java, and C++?
- Falling Stars Animation on Python.Hub October 2024
- Most Underrated Database Trick | Life-Saving SQL Command
- Python List Methods
- Top 5 Free HTML Resume Templates in 2024 | With Source Code
- How to See Connected Wi-Fi Passwords in Windows?
- 2023 Merry Christmas using Python Turtle
- 23 AI Tools You Won’t Believe are Free
- Python 3.12.1 is Now Available
- Best Deepfake Apps and Websites You Can Try for Fun
- Amazon launched free Prompt Engineering course: Enroll Now
- 10 GitHub Repositories to Master Machine Learning
- Hello World in 35 Programming Languages
- How to Scrape Data From Any Website with Python?
- Become Job Ready With Free Harvard Computer Science course: Enroll Now
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Udacity Giving Free Python Course: Here is how to Enroll
- Love Babbar’s Income Revealed
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- Microsoft Giving Free Python Course in 2023: Enroll Now
- Top 5 Free Python Courses on YouTube in 2024
- Complete Python Roadmap for Beginners in 2024
- New secrets to Earn money with Python in 2024
- Connect with HR Directly – Job Hack
- Google offering free Python course: Enroll Today
- What is an AI Tool?