In this guide, we will walk you through setting up your development tools and configuring a Python virtual environment. This process is crucial because it isolates your project’s dependencies, ensuring that packages are managed on a per-project basis. You have several options for creating a Python virtual environment, including Conda, MiniConda, and Python’s built-in virtual environments. For simplicity and consistency, we will use Python’s built-in virtual environment. A virtual environment encapsulates all the dependencies required for your project. Without it, installing a package like OpenCV globally makes it available to every Python project on your system. However, if different projects require different package versions, global management quickly becomes problematic. By using a virtual environment, you can maintain unique, isolated installations for each project. Typically, you’ll list your dependencies in a file named requirements.txt, which can be shared via platforms like GitHub with the following command:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Using a requirements.txt file helps you maintain consistency and makes onboarding contributors easier since they can quickly set up their development environment.
Setting Up Your Tools
Begin by opening your preferred code editor and navigating to its Extensions view. In this lesson, we employ tools such as GitHub Copilot, GitHub Copilot Chat, BlackboxAI, and Tabnine. If these extensions are not already installed, search for them in the Extensions marketplace and install them accordingly. Upon installation, GitHub Copilot may prompt you to authenticate via GitHub. These extensions typically appear in the lower left-hand corner of your editor and provide AI-driven code suggestions and chat features, which can greatly enhance your productivity.Example: Flask Application Code Snippet
Consider the following sample code snippet from a Flask application. This snippet demonstrates how to handle update and delete operations within your virtual environment:Creating a Python Virtual Environment
Follow these steps to create and activate your Python virtual environment:-
Navigate to Your Project Directory:
Open your terminal and change the directory to your project folder. In our example, the project is called “image optimizer”. -
Create the Virtual Environment:
Execute the following command to generate a Python virtual environment named “venv”:Using a consistent name like “venv” simplifies project setup and is frequently included in .gitignore files. This command creates a folder named “venv” that houses all the necessary scripts, libraries, and the current Python interpreter (e.g., Python 3.12). Any package you install while the virtual environment is active will reside in this directory. -
Activate the Virtual Environment:
Within the “venv” folder, a directory called “bin” (or “Scripts” on Windows) contains the activation scripts. For Unix-based systems, activate your environment with:Once activated, your terminal prompt will change to show that you are now working within your virtual environment (commonly indicated by a “(venv)” prefix). Since the correct interpreter is now in use, you can simply run “python” instead of “python3”.
If you use a different shell, follow these commands:
- For C shell (csh):
- For Fish shell:
- On Windows (PowerShell):