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.
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
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.
If you are using Mac OS, then use the following command to activate virtualenv
when you activate the virtual environment folder name in the terminal look like this
(YtTutorial) PS D:\Python Projects>
If you want to deactivate Virtual Environment,
Just type the following command
To delete the virtual environment,
Just delete the folder.