Essential Python Commands For Virtual Environment.

Essential Python Commands For Virtual Environment.

 

Virtual Environment In Python

A virtual environment in Python is an isolated workspace that allows you to install and manage packages independently for each project. This isolation is essential because different projects often require different versions of the same packages, and managing them globally (system-wide) can lead to version conflicts. Virtual environments solve this problem by creating a separate environment with its own dependencies, Python binaries, and configurations.

 

Check Your Python Version

This command give your python version if you have installed python in your system.

python --version    # For python version
pip --version   # For pip version  

 

Create A Virtual Environment

To create a new virtual environment for your project.

cd your_project_dir   # Go to your project Dir
python3 -m venv your_venv_name   # it will create a new virtual environment for python3 in linux
python -m venv your_venv_name  # For window work for both python3 or 2
python3 -m venv django_ecommerce  

Note:  If venv not installed in your system then install it using bellow command

sudo apt install python3-venv   # For linux user

 

Activate Your Virtual Environment

To Activate your virtual environment in Linux/Mac

cd your_project_dir  # Where virtual env created
source virtual_env_name/bin/activate    # This will activate your virtual env   FOR Linux/Mac

To Activate venv in windows

cd your_project_dir  # Where virtual env created
YourENvName\Scripts\activate     # For windows

After activate your venv you will see your venv 

(django_ecommerce) shanikr@LAPTOP-SG055VUT:~/django_projects$   # Activated virtual env name before your username

 

Deactivate Your Virtual Environment

To deactivate virtial env

deactivate   # For activated virtual env

 

Check Installed Packages In Python

You can check your installed packages using following command

pip list  # It will give you all installed packages list

Result

Package                Version
---------------------- -------------
acme                   1.21.0
blinker                1.7.0
certbot                1.21.0
certbot-nginx          1.21.0
certifi                2020.6.20
chardet                4.0.0
click                  8.1.7
command-not-found      0.3
ConfigArgParse         1.5.3
configobj              5.0.6
cryptography           3.4.8

 

Check Specific Package version

pip show packagename
# if you dont know exact name you can follow this command 
pip list | grep ‘package’  # For linux user

 

pip show flask

Result

Name: Flask
Version: 3.0.2
Summary: A simple framework for building complex web applications.
Home-page:
Author:
Author-email:
License:
Location: /home/shanikr/.local/lib/python3.10/site-packages
Requires: blinker, click, itsdangerous, Jinja2, Werkzeug
Required-by:

 

To Uninstall package from your venv

By following command youcan uninstall your specific package

pip uninstall package_name

 

To create packages requirements.txt file

After working in local development you need to share your code for production or other developer. in this case you need to share your code as well as your all packages with their dependencies.

you can create requirements.txt file using this command

pip freeze > requirements.txt

 

To Install packages from requirements.txt file

You can use following command

pip install -r requirements.txt

 

Keep learning keep growing!

 

Related Posts
Realtime Usecase Of Python Decorator Using Custom File Based Caching.
Realtime Usecase Of Python Decorator Using Custom File Based Caching.

decorator is a kind of design pattern which enables you to add and modify of your functions or metho...

Python