Python is most widely used programming language among developers. Do you want to setup a virtual environment in Python ? If yes then this tutorial is for your. Setting up a virtual environment in Python is an essential step for managing and isolating different Python projects. Virtual environments allow you to have separate project-specific dependencies, thus avoiding conflicts between packages and ensuring project stability. In this tutorial ,you will know you the process of setting up a virtual environment in Python.
Before we begin, make sure you have Python installed on your system. You can check if Python is installed by opening a terminal or command prompt and running the
python --version command. If Python is not installed, you have to download and install it from the official Python website.
Step to Set up a Virtual Environment in Python
Now let’s know all the steps to set up a virtual environment.
Step 1: Install virtualenv package
The first thing you need to do is install the
virtualenv package, which is used to create and manage virtual environments. Open your terminal or command prompt and run the following command:
pip install virtualenv
Step 2: Create a virtual environment
virtualenv is installed, you can create a new virtual environment by running the following command:
In this command,
myenv is the name of the virtual environment. You can choose any name you prefer according to your project.
Step 3: Activate the virtual environment
To start using the virtual environment, you need to activate it. The activation process varies based on your operating system.
macOS & Linux:
After running these commands, you will notice that your command prompt or terminal prompt has changed (your virtual envrionment name before the path), indicating that you are now working within the virtual environment.
Step 4: Install project-specific dependencies
Once inside the virtual environment, you can now install python packages and dependencies without affecting your system-wide Python installation. For example, you can use the following command to install the popular
pip install requests
You can install as many packages as you need using the same
pip install command.
Step 5: Deactivate the virtual environment
To exit the virtual environment, you need to deactivate it. Simply run the following command:
After deactivating the virtual environment, you will return to your system’s default Python environment.
Step 6: Using the virtual environment
To use the virtual environment again, you need to activate it as explained in step 3.
Step 7: Delete the virtual environment (optional)
If you no longer need a virtual environment and want to delete it, you can simply delete the directory where it is located, using your operating system’s file management tools.
By following these steps, you can effectively set up and manage virtual environments in Python. It is essential for developing multiple Python projects with different dependencies and ensuring a clean and isolated development environment.
Remember that each time you start working on a new project, it is recommended to create a separate virtual environment to avoid conflicts between packages and maintain project-specific dependencies.
In this post you have learned how to setup the virtual environment through steps. You have learned how to activate and deactive virtual envrionment on Windows and MacOS or Linux.