By: Arif Khan
Virtual env is a way, that you can separate different Python environments for different projects. Why would you do something like this?
Suppose if you have different projects relying on DJANGO or FLASK, and all of these projects may be using different version of DJANGO or FLASK. If you want to run all of your projects from Python Global Site Packages, it might possible that some of projects may run and some won’t, it will happen because Global Site Packages won’t be able to satisfy all applications requirements. So, it is recommended that each of your project should have its own environment in which they have all of their dependencies they need. Virtual env is allows us to make different Python environments.
Let’s go ahead and get started. I will show you how it works
Let’s check what packages are installed in our Global Site Packages first below command will display all installed packages installed in Global environment’s Site Packages folder.
pip list
If you already have virtualenv installed then you will see its name there, but if not then install it by executing below command.
pip install virtualenv
If you pip list again then you will be able to see it there.
Now let’s make venv with the help of virtualenv
Procedure is almost the same for linux and windows, but little difference in activation command. Make a project directory, cd into it and issue below command, this command will create virtual environment for you.
virtualenv <environment name>
In result windows will display following output in windows.
created virtual environment CPython3.7.6.final.0-64 in 21659ms
creator CPython3Windows(dest=D:\exmp\exmpenv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\arif.khan\AppData\Local\pypa\virtualenv)
added seed packages: pip==21.1.3, setuptools==57.1.0, wheel==0.36.2
activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Linux will display below message.
New python executable in <environment name/bin/python
Installing setuptools, pip…done.
To activate newly created environment in windows issue below command.
<environment name>\scripts\activate
Your <environment name> will be added to your os prompt in both (linux and windows), it will be turned into (<environment name>) drive letter>\ foldername. To deactivate it just write deactivate at os prompt.
To activate newly created environment in linux issue below command. To deactivate it just write deactivate at os prompt.
source <environment name>/bin/activate
Default contents of virtual environment in Windows and Linux
If you are in windows and if you write pip list in this environment then you will see only three packages (pip, setuptools, wheel), but in linux it will show only two packages (pip, setuptools). Whatever you install in this environment with pip command, those will be related to this environment only.
Now if you want build DJANGO application and need to install some packages e.g.
Pip install pytz==2021.1 (you can use ==version number to install specific version of any package, don’t use ==version number to install latest version)
Pip install django==2.2.7
Pip install pytz
Pip install pywin32
Pip install numpy
After installation of above packages you could see all of them in your environment with pip list command.
Installed packages distribution
For application distribution or if you want to use all above installed packages in another environment then you need to create requirements.txt file by freeze command. This command will create a file named requirements.txt in your project folder this file will contains all required packages to run your project properly, to install all contents of requirement.txt in any other environment you can use (pip install –r requirements.txt) –r means recursive.
pip freeze –local > requirements.txt
If you delete the project folder which contains the virtual environment then the virtual environment will deleted from your system permanently.
Using specific python version in virtual environment
If you want to use specific version of python e.g. python 2.7 / 2.6, then you have to specify the python path in environment creation command like.
virtualenv –p <python path> <environment name>
These are pretty much basics I shared with you guys, fell free to tell me if I missed anything.