How to setup up a virtual environment in Python ?

Created at 03-Feb-2023 , By samar

To set up a virtual environment in a python project, you have to first install pip in your system using sudo apt-get install python3-pip which is a python package manager that is used to install or uninstall python packages for your python project. You can install python packages globally or you can also install python packages for your specific virtual environment in a python project.

Now you can Install virtualenv python package to manage the virtual environment for the python projects using pip.

pip install virtualenv

Next, you have to create a virtual environment in your python project by running the following command. This command will create a virtual environment named MyProjectEnvt in the root of your python project directory.

virtualenv MyProjectEnvt

create virtual environment

Now you can activate the virtual environment by running the following command:

source MyProjectEnvt/bin/activate

activate virtual environment
Install required packages in the virtual environment. Now you can run the below command to install the python packages direct in your activated python project environment.

pip install [package_name]

pip install requests


pip install requests

To deactivate the virtual environment, run the following command. It will deactivate the current activated virtual environment.

deactivate

deactivate virtual environment
To delete the virtual environment, simply run rm -rf MyProjectEnvt to delete the MyProjectEnvt virtual environment from the python project. Here MyProjectEnvt is the environment name which may be different in your case.

rm -rf MyProjectEnvt

delete virtual environment

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.