Python virtual environment is a tool that allows projects to run in an isolated environment to avoid any conflict of dependencies.
This is really helpful when you are working on multiple project on same system, these project can have dependency with different version. Virtual environment will allow to maintain the isolation and you will not run into unnecessary issues.
To use this, first we need to install virtualenv
module to the project using command
pip install virtualenv
In case you get similar warning
WARNING: The script virtualenv.exe is installed in 'C:\Users\username\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Add this to your PATH variable after that check the version of virtualenv to verify the installation.
C:\Users\username\virtualenv-demo>virtualenv --version
virtualenv 20.14.1 from C:\Users\username\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\virtualenv\__init__.py
Create a virtual environment using command:
virtualenv <name of the environment>
C:\Users\username\virtualenv-demo>virtualenv my_virtual_env
created virtual environment CPython3.9.13.final.0-64 in 3256ms
creator Venv(dest=C:\Users\username\virtualenv-demo\my_virtual_env, clear=False, no_vcs_ignore=False, global=False, describe=CPython3Windows)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\username\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\Local\pypa\virtualenv)
added seed packages: pip==22.0.4, setuptools==62.1.0, wheel==0.37.1
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
It will create a new directory in the project:
This directory has all the required libraries and executables which are required by python project.
To use the virtual environment we need to activate it first. To activate this we need to run a script <name of the environment>/bin/activate
On Windows System:
C:\Users\username\virtualenv-demo\> .\my_virtual_env\Scripts\activate
On Unix systems:
source \my_virtual_env\Scripts\activate
Once it is activated the name of the environment will appear at the front of the command prompt.
(my_virtual_env) PS C:\Users\username\virtualenv-demo>
Now you can install all the required dependencies. for example
pip install fastapi=0.78.0
Once you have all the required packages installed you can also create a list of packages using command
pip freeze > requirements.txt
It will create a new file requirement.txt with all the packages and versions. Which can be used to install all the libraries easily wherever you want.
Having a updated requirements.txt file always helps when you have to install the project on different environment.
Just use below command and your environment will be ready to run the application
pip install -r requirements.txt
Comment below how you find virtulenv
Happy Learning !
Top comments (0)