Python Virtual Environment: How to Create, Activate, Use & Deactivate it

Virtual Environment is the best option when you install a lot of new, experimental python packages for your current project and you don't want to install those packages in the whole system. Virtual Environment is a copy of existing python installation which also contains old installed packages.

If you learn python tutorials, you may need to install a lot of new python packages for the first time and you don't know you gonna use them again in the future then a virtual environment is the best option to keep those packages files in a single folder.

Also, there are a lot of python packages like Scrapy (for web scraping), and Youtube Data API (to get API information from Google) recommended to install their package in a virtual environment.

There are a lot of best virtual environment tools available for Python like venv, Virtualenv, poetry,  pyflow, pipenv,  hatch and Conda in Anaconda(package management library).   I prefer virtualenv package.

python virtual environmentPin

 

Method 1: Easy Way

In your Windows command Panel, navigate to the Python app directory. Mine is C:\Users\webapptiv\Documents\flask_app>

Now type the following commands to install the virtual environment in the project directory.

py -m venv env

Here venv is the short name for virtual environment and env is a virtual environment name. You can any other name for your virtual environment.

Now it will add a new folder named env in your Python project directory.

To activate virtual environment, type the following command

env\Scripts\activate

Here env is the name of the environment directory.

It will turn on your virtual environment. so, the folder looks like

(env) C:\Users\webapptiv\Documents\flask_app>

To get out of the virtual environment, just type

deactivate

That's all.

Method 2: Using Virtualenv package

First, install a virtual environment (virtualenv) package on your system

For Windows OS users, Open a Window Command prompt and type the following command

pip install virtualenv

If you are using Mac OS, then the Terminal program is available in “/Applications/Utilities folder”.

Note: Before you try to run this command make sure you install python software on your system and it has pip package manager. To check if your system has pip package manager, run “pip -h” command in the terminal.

Then Create a Virtual Environment for the particular folder

To do that, navigate to the folder directory in the command prompt (like cd D:\Python Projects) and type the following command

virtualenv [Your_folder_name]

Note: Change the ‘[Your_folder_name]” to the actual folder name you want to install in the virtual environment. Ex: virtualenv YtTutorial (Actual folder is D:\Python Projects\YtTutorial)

Now you have to activate the virtual environment

use the following command to activate the virtual environment.

[Your_folder_name]\Scripts\activate

Example 

YtTutorial\Scripts\activate

If you are using Mac OS, then use the following command  to activate virtualenv

source YtTutorial/bin/activate

when you activate the virtual environment folder name in the terminal look like this

(YtTutorial) PS D:\Python Projects>

python virtual environment in vs codePin

If you want to deactivate Virtual Environment,

Just type the following command

deactivate

To delete the virtual environment,

Just delete the folder.

Reference:

About The Author

Scroll to Top
Share to...