How to set up a virtual environment in Python

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

Once virtualenv is installed, you can create a new virtual environment by running the following command:

virtualenv myenv

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.

Windows

myenv\Scripts\activate

macOS & Linux:

source myenv/bin/activate

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 requests package:

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:

deactivate

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.

Conclusion

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.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link